Forum C# Multi-threading? Object reference not set to instance..

Multi-threading? Object reference not set to instance..

Postby robscherer123 » June 3rd, 2013, 3:18 pm

Hello! I've been getting a few of these errors for several months now since I've began working on my game. The error is "Object reference not set to an instance of an object". This error is basically the same as the "cannot access a property or method of a null object reference" in AS3 I assume?

Anyway, I get the errors very seldom and they don't seem to have much negative effect at least that I've ever noticed. Nonetheless, I would like to get rid of them. I've used the ErrorLog to track down where in my code I am receiving them from and it seems to be a fairly odd place.

First of all, it's happening inside of the GotMessage function for one of my calls "enemyHit", which fires when an enemy is attacked. The only objects I mention on the lines that the error is occuring on is the player object that is passed into the function and "enemy_array[i]" which is the enemy that was hit. Nowhere inside that call is the player or "enemy_array[i]" ever set to null, so I'm not sure why I am getting that error. Unless I could be reading something wrong and it's a dumb mistake, but so far that doesn't seem to be the issue. =P

Inside my main gameloop is the only time an enemy object from the enemy_array should ever be set to null.

So one of my possible ideas, is that.. Is it possible for the player to disconnect; be set to null; then the GotMessage function fire and throw an error because player is null? Or is it also possible for the GotMessage function to fire and at the SAME TIME, the gameloop be executing as well? The problem is strange to me because this is how the error seems to be appearing. (Simple example)

Code: Select all
//Simple example
player.x += enemy_array[i].x;
player.x += enemy_array[i].x;
player.x += enemy_array[i].x; //error happens here for example
player.x += enemy_array[i].x;



So I don't know why I would be getting errors in the middle of that code instead of on the first line. Maybe it's some stupid mistake and I'm overlooking something, but I'm not sure. I'm not nearly as experienced in C# as I am AS3, so maybe it's some type of multi-threading thing where multiple lines of code are running at the same time? Or maybe that's not the problem. Some help or insight would be much appreciated though. Thanks!
robscherer123
Paid Member
 
Posts: 313
Joined: December 12th, 2012, 8:46 pm

Re: Multi-threading? Object reference not set to instance..

Postby Henrik » June 4th, 2013, 8:32 am

Are you modifying the contents of enemy_array in other places?
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Multi-threading? Object reference not set to instance..

Postby robscherer123 » June 6th, 2013, 8:49 pm

No, I should never be setting any enemies from the enemy_array to null anywhere except for on one line, which is in the main game loop whenever an enemy's health goes below 0.

I just happened to get the error while testing on the dev server (me being the only one playing). This is an error that very very rarely happens, but nonetheless is still reoccurring. It's quite weird to me. Here is a screenshot of exactly what I'm talking about. Not much more to it than what is pictured in the screenshot.


On line 1036 whenever I mention "enemy_array[i].Name", the enemy has somehow gone null. But I check in the if statement a couple lines before it to make sure "enemy_array[i]" is not null, and I also mention its ".name" there too, and I don't get an error. Keep in the mind that the "doDamageToPlayer" function is only a few lines long, and all it does is take the given enemies power and subtract it from the players health, etc, nothing more than than that.

If you take a look at the debug data directly below, you can see enemy_array[1] has gone null. While it's not pictured in the screenshot, "i" in the variables list is set to 1 when the error is thrown. So I'm not sure how enemy_array[1] got set to null in those few lines. Either that, or "i" somehow changed, but I'm not sure that would be possible because it shouldn't be able to change outside the scope of the for loop.

Maybe I'm just going crazy, but this is pretty weird to me. Hard to fix too since it's happens so rarely..

View full size image below.
Here is a link to the full size image http://oi39.tinypic.com/rlgjma.jpg

Image
robscherer123
Paid Member
 
Posts: 313
Joined: December 12th, 2012, 8:46 pm


Return to C#



cron