NulllReferenceException (Loading NPC)

10/09/2013 17:40 ~Crystaline#1
I was trying to load an NPC from a MySQL database, but I get NullReference in this code:

PHP Code:
private void LoadNpcs()
        {
            
Database.MySqlCommand command = new Conquer_Online_Server.Database.MySqlCommand(Conquer_Online_Server.Database.MySqlCommandType.SELECT);
            
command.Select("npcs").Where("mapid"ID);
            
Database.MySqlReader reader = new Conquer_Online_Server.Database.MySqlReader(command);
                while (
reader.Read())
                {
                    
INpc npc = new Network.GamePackets.NpcSpawn();
                    
npc.UID reader.ReadUInt32("id");
                    
npc.Type reader.ReadUInt16("lookface");
                    
npc.Kind = (Enums.NpcKind)reader.ReadByte("type");
                    
npc.reader.ReadUInt16("cellx"); ;
                    
npc.reader.ReadUInt16("celly");
                    
npc.MapID ID;
                    
AddNpc(npc);
                }
            
reader.Close();
        } 
Any idea why is it Null? Impulse 5165.
10/09/2013 19:22 Aceking#2
Could be the ID variable you are using as a parameter to search for.

Or one of the fields in the table is null that you are trying to read and assign to npc
10/09/2013 21:13 _DreadNought_#3
There is about 20 reasons for that to be caused, more info?
10/09/2013 21:18 Spirited#4
Put a breakpoint at the top of that method, then follow it down and step into the MySql wrapper. See where the program crashes (it should tell you as you step over the null reference). It probably helps to know what a null reference is.
Quote:
The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. NullReferenceException is common. It indicates that you are trying to access member fields, or function types, on an object reference that points to null.
For info on how to debug your server, use my tutorial (below).
[Only registered and activated users can see links. Click Here To Register...]
10/10/2013 02:20 ~Crystaline#5
After breakpointing, it says that "command" and "reader" are Null. I have no idea why is it null. I haven't experience this before, when I added loading the NPC through MySQL.
10/10/2013 07:15 Spirited#6
Quote:
Originally Posted by ~Crystaline View Post
After breakpointing, it says that "command" and "reader" are Null. I have no idea why is it null. I haven't experience this before, when I added loading the NPC through MySQL.
See if the constructor runs into any problems. It might be that it can't find the table, and thus something isn't being initialized in the constructor. Are you positive that the server is set up correctly? If you are, see what happens in the constructor. Step through it.
10/10/2013 15:04 12k#7
Is the variable "ID" static or a local variable? Not sure where that value is being defined.

#edit Nvm, I now see that the AddNPC is being called locally. Assume this is being called from an instance of a map class. Derp
10/10/2013 16:34 ~Crystaline#8
I don't know what to call this one, but anyway I name it as "caller" :D Okay, I tried breakpointing the "caller" which is "LoadNpcs();" and this one thing here appeared.

[Only registered and activated users can see links. Click Here To Register...]

And I think this is causing the problem.

Edit:

-LoadNPC is a method, which is not valid in the given context.
10/10/2013 17:40 Spirited#9
Quote:
Originally Posted by ~Crystaline View Post
I don't know what to call this one, but anyway I name it as "caller" :D Okay, I tried breakpointing the "caller" which is "LoadNpcs();" and this one thing here appeared.

[Only registered and activated users can see links. Click Here To Register...]

And I think this is causing the problem.

Edit:

-LoadNPC is a method, which is not valid in the given context.
I don't think that would be the problem. It wouldn't compile if there was a missing reference to a method. Try stepping into it using the tutorial I created. Step through each line and see exactly what isn't being initialized. You can mouse over the variables as I've mentioned in the tutorial.
10/15/2013 15:34 shadowman123#10
Which line is the Error Located in ?
10/17/2013 09:44 ~Crystaline#11
Quote:
Originally Posted by shadowman123 View Post
Which line is the Error Located in ?
PHP Code:
while (reader.Read())
            {
                
INpc npcs = new Network.GamePackets.NpcSpawn();
                
npcs.UID reader.ReadUInt32("id");
                
npcs.Type reader.ReadUInt16("lookface");
                
npcs.Kind = (Enums.NpcKind)reader.ReadByte("type");
                
npcs.reader.ReadUInt16("cellx");
                
npcs.reader.ReadUInt16("celly");
                
npcs.MapID ID;
                
AddNpc(npcs);
            } 
Line 351 which is while (reader.Read())
10/17/2013 14:36 .Beatz#12
Quote:
Originally Posted by ~Crystaline View Post
PHP Code:
while (reader.Read())
            {
                
INpc npcs = new Network.GamePackets.NpcSpawn();
                
npcs.UID reader.ReadUInt32("id");
                
npcs.Type reader.ReadUInt16("lookface");
                
npcs.Kind = (Enums.NpcKind)reader.ReadByte("type");
                
npcs.reader.ReadUInt16("cellx");
                
npcs.reader.ReadUInt16("celly");
                
npcs.MapID ID;
                
AddNpc(npcs);
            } 
Line 351 which is while (reader.Read())
Can you please screenshot the nullreference and post it here
10/17/2013 16:46 ~Crystaline#13
Quote:
Originally Posted by .Beatz View Post
Can you please screenshot the nullreference and post it here
[Only registered and activated users can see links. Click Here To Register...]
10/17/2013 17:21 .Beatz#14
Please link me to the source you are using, I will have a look through it and find out what's going wrong... Will be much easier this way
10/17/2013 18:32 ~Crystaline#15
[Only registered and activated users can see links. Click Here To Register...] <-- There.