[英]Field is never assigned to, and will always have its default value null
When my program first starts, in the main method I create a new Menu which then goes to the menu class. 当我的程序第一次启动时,在main方法中,我创建一个新Menu,然后转到菜单类。
private static Shop shop;
private static Player player;
public static void Main(string[] args)
{
Menu menu = new Menu();
menu.start(menu, shop, player);
}
In my menu class, I have a method called start(). 在我的菜单类中,我有一个名为start()的方法。 Which prints out the options for the user.
从而为用户打印选项。
private Menu menu;
private Shop shop;
private Player player;
public void start(Menu menu, Shop shop, Player player)
{
this.menu = menu;
this.shop = shop;
Then when I compile my program, I choose to go to the shop and I get an error of 'Object reference not set to an instance of an object.'. 然后,当我编译程序时,我选择去商店,并收到“对象引用未设置为对象实例的错误”错误。 I think I know what I have to do, but I'm not quite sure on how you do it in the main class.
我想我知道我该怎么做,但是我不太确定您在主要班级中该如何做。
I think I have to add 我想我必须补充
Shop shop
as a parameter in the Main method. 作为Main方法中的参数。 Then declare
然后声明
this.shop = shop;
inside the method as I have in my other classes. 在方法中,就像我在其他课程中一样。
I have tried doing that, but it always comes up with an error. 我已经尝试过这样做,但是总是会出错。 Since it's the main method, would I have to do a different approach?
既然是主要方法,那么我是否需要采取其他方法?
You have not actually allocated those static references: 您实际上尚未分配这些静态引用:
private static Shop shop = new Shop();
private static Player player = new Player();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.