2013-05-04 100 views
0

好吧,所以我有下面的代码,我不断收到运行时错误,我在考虑它在代码逻辑中的缺陷。我正在尝试使用setOneOtherPicture方法来选取图片并将其设置为一个数组,以便稍后调用以显示在showArtCollection方法中。我已经给出了两个参数whichpRef。有人可以帮我弄这个吗?谢谢。我的代码中的Java逻辑

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

什么例外?请发布整个异常消息。涉及哪些代码行?异常消息应该告诉你这一点,然后你必须通过代码中的注释或类似的东西来向我们表明这一点。 – 2013-05-04 22:54:48

+0

这个问题是非常相似的http://stackoverflow.com/questions/16377626/nonstatic-variable-pref-cannot-be-referenced-from-a-static-context/16378457#16378457 – Bill 2013-05-04 22:57:44

+0

这是一个家庭作业? – Bill 2013-05-04 22:58:25

回答

0

这种方法:

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

不应该被调用文件选择器,它甚至不应该创建一个新图片对象,而是应该只把县图片对象,你已经将该方法传入数组中。否则,你只是抛出pRef参数 - 没有任何意义。

0

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