Search code examples
c#botssteam

NullReferenceException when trying to set int to an already existing int


I tried to set an integer to an already existing integer, and it gave me a NullReferenceException. So, the integer is fetched and set from a .json file, then I tried to set that to an integer called price.

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

This is the BotInfo Class

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; }

But then I get:

Oh, there was an error: Unknown error occurred: System.NullReferenceException: Object reference not set to an instance of an object. at SteamBot.SimpleUserHandler.OnTradeAddItem(Item schemaItem, Item inventoryItem) in C:\SteamBot - JiaWei\SteamBot\UserHandler.cs:line 182 at SteamTrade.Trade.FireOnUserAddItem(TradeEvent tradeEvent) in C:\SteamBot - JiaWei\SteamTrade\Trade.cs:line 640 at SteamTrade.Trade.Poll() in C:\SteamBot - JiaWei\SteamTrade\Trade.cs:line 572 at SteamTrade.TradeManager.<>c__DisplayClass6.b__5() in C:\SteamBot - JiaWei\SteamTrade\TradeManager.cs:line 272.

Line 182 is:

int price = botConfig.TicketPriceBuy;

(The other lines don't really matter because of just following up from this first one.)

So, how would I fix this?


Solution

  • This has nothing to do with the integer. Look at your two lines:

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

    You declare a BotInfo object, but never initialize it to anything. So botConfig is null. Then you immediately try to de-reference that object to pull a value from it. But you can't de-reference null.

    You need to initialize reference types. For example:

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

    To confirm this, place a breakpoint in your code of debugging purposes. Step through the debugger line by line and examine the runtime values of the objects you're using. This will help you determine problems in your code much faster.