【问题标题】:NullReferenceException when trying to set int to an already existing int [duplicate]尝试将 int 设置为已经存在的 int 时出现 NullReferenceException [重复]
【发布时间】:2014-05-27 12:07:50
【问题描述】:

我试图将一个整数设置为一个已经存在的整数,它给了我一个 NullReferenceException。因此,整数是从 .json 文件中获取并设置的,然后我尝试将其设置为一个名为 price 的整数。

Configuration.BotInfo botConfig;
int price = botConfig.TicketPriceBuy;

这是 BotInfo 类

public class BotInfo
    {
        public string Username { get; set; }
        public string Password { get; set; }
        public string DisplayName { get; set; }
        public string ChatResponse { get; set; }
        public string LogFile { get; set; }
        public string BotControlClass { get; set; }
        public int MaximumTradeTime { get; set; }
        public int MaximumActionGap { get; set; }
        public string DisplayNamePrefix { get; set; }
        public int TradePollingInterval { get; set; }
        public string LogLevel { get; set; }
        public int TicketPriceBuy { get; set; }
        public int BackpackExpanderPriceBuy { get; set; }
        public int DecalToolPriceBuy { get; set; }
        public int KeyPriceBuy { get; set; }
        public int NameTagPriceBuy { get; set; }
        public int AustralianGoldPriceBuy { get; set; }
        public int TicketPriceSell { get; set; }
        public int BackpackExpanderPriceSell { get; set; }
        public int DecalToolPriceSell { get; set; }
        public int KeyPriceSell { get; set; }
        public int NameTagPriceSell { get; set; }
        public int AustralianGoldPriceSell { get; set; }
        public ulong[] Admins { get; set; }

然后我得到:

哦,出现错误:发生未知错误: System.NullReferenceException:对象引用未设置为 对象的实例。在 SteamBot.SimpleUserHandler.OnTradeAddItem(Item schemaItem, Item inventoryItem) 在 C:\SteamBot - JiaWei\SteamBot\UserHandler.cs:line 182 在 SteamTrade.Trade.FireOnUserAddItem(TradeEvent tradeEvent) 中 C:\SteamBot - JiaWei\SteamTrade\Trade.cs:line 640 at C:\SteamBot 中的 SteamTrade.Trade.Poll() - JiaWei\SteamTrade\Trade.cs:line 572 at SteamTrade.TradeManager.c__DisplayClass6.b__5() 在 C:\SteamBot - JiaWei\SteamTrade\TradeManager.cs:line 272.

第 182 行是:

int price = botConfig.TicketPriceBuy;

(其他行并不重要,因为只是从第一行跟进。)

那么,我该如何解决这个问题?

【问题讨论】:

  • TicketPriceBuybotConfig 为空
  • botconfig 显然是空的。 TicketPriceBuy 不会抛出空引用,因为 int 的默认值为零
  • botConfig 从未初始化。

标签: c# bots steam


【解决方案1】:

这与整数无关。看看你的两行:

Configuration.BotInfo botConfig;
int price = botConfig.TicketPriceBuy;

您声明了一个BotInfo 对象,但从不将其初始化为任何内容。所以botConfignull。然后您立即尝试取消引用该对象以从中提取值。但是你不能取消引用null

您需要初始化引用类型。例如:

Configuration.BotInfo botConfig = new Configuration.BotInfo();

要确认这一点,请在调试代码中放置一个断点。逐行通过调试器并检查您正在使用的对象的运行时值。这将帮助您更快地确定代码中的问题。

【讨论】:

    【解决方案2】:

    我认为您还没有在这里创建类的实例。要获取对象 botConfig 中的值,您需要先对其进行初始化。如果没有初始化,您将无法访问对象内的公共methodsproperties。您将获得空引用。

    Configuration.BotInfo botConfig = new Configuration.BotInfo();
    
    //some method which populate botConfig or below line
    botConfig.TicketPriceBuy = 123;
    
    //you will get the value
    int price = botConfig.TicketPriceBuy;
    

    【讨论】:

      【解决方案3】:

      我不确定你是否需要 null,但你可以通过创建一个可以为空的 integer 来解决这个问题:

      int? price = botConfig == null ? null : botConfig.TicketPriceBuy;
      

      【讨论】:

        猜你喜欢
        • 2021-08-25
        • 1970-01-01
        • 1970-01-01
        • 2013-04-26
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-28
        相关资源
        最近更新 更多