2013-05-04 75 views
-1

我試圖在方法setFavoritePicture (Picture pRef)中設置圖片。 這種方法應該設置一個最喜歡的圖片被調用的主要方法,但我不斷收到編譯器錯誤說nonstatic variable pRef cannot be referenced from a static context。我是比較新的Java的所以任何幫助,您可以提供我將非常讚賞非靜態變量pRef無法從靜態上下文中引用

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


} 

}

+0

我不能重現你的編譯器錯誤(雖然承認我必須發明所有缺失的類)。上面的代碼中的哪一行是錯誤? – 2013-05-04 18:50:17

+0

@DuncanJones Jones我只是修復了代碼,因爲我意識到這一塊遺漏了,錯誤來了'PhDsHouse.setFavoritePicture(pRef);' – Bradley 2013-05-04 18:53:43

回答

1

,我看到的是如下的錯誤:

PhDsHouse.setFavoritePicture (pRef);其中在main定義pRef ?因此,您在該陳述中出現錯誤。

我在猜測,您要創建新的Picture對象,然後使用setFavoritePicture將它分配給PhDsHouse。這是真的?如果是的話,你需要在setFavoritePicture之前做Picture pRef = new Picture();之類的事情......那麼你應該很好。

此外,下面的功能看起來很可疑我

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

如果這是

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

,因爲我看不到的地方favPic已經定義/在你的代碼初始化做... .else您將獲得NULL pointer exceptions當您訪問pRef作爲favPicNULL,它將被分配到pRef

相關問題