-1

我正在尝试在方法中设置图片setFavoritePicture (Picture pRef)。此方法应该设置要在 main 方法中调用的最喜欢的图片,但我一直收到编译器错误说nonstatic variable pRef cannot be referenced from a static context。我对 java 比较陌生,所以您可以为我提供的任何帮助将不胜感激

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 ();


 }

}

4

1 回答 1

1

我看到的错误如下:

PhDsHouse.setFavoritePicture (pRef);在哪里pRef定义main?因此,您在该声明中遇到错误。

我猜,您想创建新Picture对象,然后将其分配给PhDsHouseusing setFavoritePicture。这是真的?如果是,您需要在...Picture pRef = new Picture();之前做一些事情setFavoritePicture,那么您应该很好。

此外,以下功能对我来说看起来很可疑

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

这应该是

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

因为,我看不到favPic您的代码中已定义/初始化的位置....否则,当您按原样NULL pointer exceptions访问时,您会得到,这将被分配给.pReffavPicNULLpRef

于 2013-05-04T20:06:04.860 回答