Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to set a picture in a method setFavoritePicture (Picture pRef). This method is supposed to set a favorite picture to be called in the main method, but i keep getting a compiler error saying nonstatic variable pRef cannot be referenced from a static context. I'm relatively new to java so any help you could provide me would be very appreciated

public class House
{
 String owner;
 Picture pRef;
 Picture [] picArray;
 Picture favPic;

 public void showArtCollection ()
  {

   ArtWall aWall = new ArtWall(600,600);
   aWall.copyPictureIntoWhere(favPic,250,100);
   aWall.copyPictureIntoWhere(picArray[0],51,330);
   aWall.copyPictureIntoWhere(picArray[1],151,330);
   aWall.copyPictureIntoWhere(picArray[2],351,280);
   aWall.show();

  }



 public House (String param)
 {

  this.owner = param;
  this.picArray = new Picture [3];
  this.favPic = new Picture (FileChooser.pickAFile ());
  this.picArray [0] = new Picture (FileChooser.pickAFile ());
  this.picArray [1] = new Picture (FileChooser.pickAFile ());
  this.picArray [2] = new Picture (FileChooser.pickAFile ());




 }

public void setFavoritePicture (Picture pRef)
{
 pRef = favPic;
}

public void setOneOtherPicture (int which,Picture pRef)
{

}


public void swapGivenOtherWithFavorite (int which)
 {
  Picture tempSaver;
  tempSaver = pRef;
  pRef = picArray [which];
  picArray [which] = tempSaver;
 }


public void addPicture (Picture pictureAdded)
{
 pRef = pictureAdded;


}

public void showPicture ()
{

 picArray [0].explore ();
 picArray [1].explore ();
 picArray [2].explore ();
 favPic.explore ();


}


public static void main (String [] args)
 {
  House PhDsHouse = new House ("Mad PH.D.");
  PhDsHouse.setFavoritePicture (pRef);
  PhDsHouse.swapGivenOtherWithFavorite (2);
  PhDsHouse.showArtCollection ();


 }

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
143 views
Welcome To Ask or Share your Answers For Others

1 Answer

The errors which I see are as follows:

PhDsHouse.setFavoritePicture (pRef); where is pRef defined in main? Thus, you are getting error at that statement.

I am guessing, you want to create new Picture object and then assign it to PhDsHouse using setFavoritePicture. Is this true? If yes, you need to do something like Picture pRef = new Picture(); before your setFavoritePicture... then you should be good.

Also, the following function looks very suspicious to me

public void setFavoritePicture (Picture pRef)
{
 pRef = favPic;
}

Should this be

 public void setFavoritePicture (Picture  favPic)
    {
     pRef = favPic;
    }

because, I do not see where favPic has been defined/initialized in your code....else you will get NULL pointer exceptions when you access pRef as favPic is NULL, which will be assigned to pRef.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...