简体   繁体   English

字段从未分配给它,并且将始终具有其默认值null

[英]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.

相关问题 警告:永远不会将字段分配给,并且始终将其默认值设置为null - Warning: Field is never assigned to, and will always have its default value null 字段'xxx'永远不会分配给,并且将始终具有其默认值null - Field 'xxx' is never assigned to, and will always have its default value null 永远不会将字段xxx分配给,并始终将其默认值设置为null - Field xxx is never assigned to, and will always have its default value null 字段…永远不会分配给它,并且其默认值始终为null - Field … is never assigned to, and will always have its default value null 该字段永远不会分配给它,并且其默认值始终为null - The Field Is Never Assigned To And Will Always Have Its Default Value null 字段“ FIELD”从未分配,并且将始终具有其默认值 - field 'FIELD' is never assigned to, and will always have its default value 私有类中的公共字段:“Field [FieldName]永远不会分配给,并且将始终具有其默认值null” - Public field in private class: “Field [FieldName] is never assigned to, and will always have its default value null” 警告“字段 .... 从未分配给并且将始终具有默认值 null” - Warning "Fields .... is never assigned to and will always have its default value null" 字段从未分配给它,并且将始终具有其默认值 - Field is never assigned to, and will always have its default value 字段modifyDate永远不会分配给,并且始终具有其默认值0 - Field modifyDate is never assigned to, and will always have its default value 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM