我的代码中的Java逻辑

时间:2013-05-04 22:54:10

标签: java logic runtime-error

好的,所以我在下面有这个代码并且我一直遇到运行时错误,我认为这是代码逻辑中的一个缺陷。我正在尝试使用setOneOtherPicture方法来挑选图片并将其设置为一个数组,以便稍后调用以在showArtCollection方法中显示。我已经获得了两个参数whichpRef。有人可以帮我弄这个吗?感谢。

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

2 个答案:

答案 0 :(得分:0)

这种方法:

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

不应该调用FileChooser,它甚至不应该创建一个 new Picture对象,而应该只是将已经传入方法的pRef Picture对象放入数组中。否则你只是抛弃了pRef参数 - 毫无意义。

答案 1 :(得分:0)

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