非静态变量pRef不能从静态上下文引用 [英] nonstatic variable pRef cannot be referenced from a static context
问题描述
我尝试在方法 setFavoritePicture(Picture pRef)
中设置图片。
这个方法应该设置一个最喜欢的图片在main方法中调用,但我不断得到一个编译器错误说非静态变量pRef不能从静态上下文引用
。我是相对新的java,所以任何帮助,你可以提供给我非常感谢
I'm trying to set a picture in a method setFavoritePicture (Picture pRef)
.
This method is supposed to set a favorite picture to be called in the main method, but i keep getting a compiler error saying nonstatic variable pRef cannot be referenced from a static context
. I'm relatively new to java so any help you could provide me would be very appreciated
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 ();
}
}
推荐答案
我看到的错误如下:
PhDsHouse。 setFavoritePicture(pRef);
其中 pRef
在 main
中定义?因此,您在该语句中收到错误。
PhDsHouse.setFavoritePicture (pRef);
where is pRef
defined in main
? Thus, you are getting error at that statement.
我想要创建新的 Picture
对象,然后将其分配给 PhDsHouse
使用 setFavoritePicture
。这是真的?如果是,您需要在 setFavoritePicture
之前执行 Picture pRef = new Picture();
I am guessing, you want to create new Picture
object and then assign it to PhDsHouse
using setFavoritePicture
. Is this true? If yes, you need to do something like Picture pRef = new Picture();
before your setFavoritePicture
... then you should be good.
此外,以下函数对我看起来很可疑。
Also, the following function looks very suspicious to me
public void setFavoritePicture (Picture pRef)
{
pRef = favPic;
}
应该是
public void setFavoritePicture (Picture favPic)
{
pRef = favPic;
}
因为我没有看到 favPic
已经在你的代码中被定义/初始化.... else当你访问
pRef $时,你会得到
NULL指针异常
c $ c> as favPic
是 NULL
,将分配给 pRef
。
because, I do not see where favPic
has been defined/initialized in your code....else you will get NULL pointer exceptions
when you access pRef
as favPic
is NULL
, which will be assigned to pRef
.
这篇关于非静态变量pRef不能从静态上下文引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!