Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

java - nonstatic variable pRef cannot be referenced from a static context

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...