2013-05-04 128 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方法中發生崩潰。