0

好的,所以我在下面有这段代码,并且我不断收到运行时错误,我认为这是代码逻辑中的一个缺陷。我正在尝试使用该setOneOtherPicture方法来选择一张图片并将其设置为一个数组,以便稍后调用以在该showArtCollection方法中显示。我得到了两个参数,which并且pRef. 有人可以帮我弄这个吗?谢谢。

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

public void showArtCollection ()
  {

  ArtWall aWall = new ArtWall(600,600);
  aWall.copyPictureIntoWhere(favPic,250,100);
  aWall.copyPictureIntoWhere(pRef,51,330);
  aWall.copyPictureIntoWhere(pRef,151,330);
  aWall.copyPictureIntoWhere(pRef,351,280);

  aWall.show();

 }

public void setOneOtherPicture (int which, Picture pRef)
 {

 this.picArray [which] = new Picture (FileChooser.pickAFile ());
 }

  public static void main (String [] args)
   {
     House PhDsHouse = new House ("Mad PH.D.");
     Picture favPic = new Picture ();
     Picture pRef = new Picture ();
     PhDsHouse.setOneOtherPicture (0, pRef);
     PhDsHouse.setOneOtherPicture (1, pRef);
     PhDsHouse.setOneOtherPicture (2,pRef);
     PhDsHouse.showArtCollection ();
   }
4

2 回答 2

1

你的House类有几个字段,你的main方法有同名的局部变量。也许这些应该被发送到构造函数中?否则,这些字段为空,导致showArtHouse方法崩溃。

于 2013-05-04T22:59:41.577 回答
0

这种方法:

public void setOneOtherPicture (int which, Picture pRef)
{
 this.picArray [which] = new Picture (FileChooser.pickAFile ());
}

不应该调用 FileChooser,它甚至不应该创建一个的Picture 对象,而应该只是将您已经传递给方法的 pRef Picture 对象放入数组中。否则你只是把 pRef 参数扔掉——没有意义。

于 2013-05-04T22:56:48.867 回答