【发布时间】: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;
(其他行并不重要,因为只是从第一行跟进。)
那么,我该如何解决这个问题?
【问题讨论】:
-
TicketPriceBuy或botConfig为空 -
botconfig 显然是空的。 TicketPriceBuy 不会抛出空引用,因为 int 的默认值为零
-
botConfig 从未初始化。