-1

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
bi0phaz3
  • 195
  • 2
  • 21

3 Answers3

3

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.

David
  • 176,566
  • 33
  • 178
  • 245
0

I think you have not created instance of class here. To get values in you object botConfig you need to initialize it first. Without initialization you cannot get access to public methods and properties within the object. You will get null reference instead.

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;
Hassan
  • 5,104
  • 2
  • 20
  • 32
0

I am not sure if you need null, but you can fix this by making a nullable integer:

int? price = botConfig == null ? null : botConfig.TicketPriceBuy;
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
codebased
  • 6,488
  • 7
  • 42
  • 78