1

I have some problems with System.NullReferenceException. When server do things and we first check user is not null and then he disconnect and server progress something and it try get user comes System.NullReferenceException. I have over 20 000 lines code so i need something small not like allways check is it null.. My server is multithread so sockets get connections and disconnects users alltime on backround so thats why this comes.. I want stop that progress when user disconnect. If i put everywhere "try/catch" is that goodway?

Example:

if (User != null)
{
    //do some things
    System.Threading.Thread.Sleep(1000); //now we have time disconnect (This only for get error)
    User.SendMessage("crash"); //<-- System.NullReferenceException... -.-
}
4
  • stackoverflow.com/questions/4660142/… Commented Feb 21, 2014 at 8:24
  • 1
    @SonerGönül your link does not explain how to solve this particular problem Commented Feb 21, 2014 at 8:25
  • 1
    20 000 lines code-->not much if you have proper architecture and if its all in one method time to refractor, there is no way out other than checking null to avoid null exceptions Commented Feb 21, 2014 at 8:30
  • @user3290224..Instead of sleep..Run your code as backgroundworker Commented Feb 21, 2014 at 8:35

3 Answers 3

4

It looks like the value of User is changing after the test but before the call to SendMessage. If your application is multithreaded I suspect that another thread is setting User to null whilst you are sleeping.

One option would be to grab User and check against it:

var user = User;
if (user != null)
{
    //do some things
    System.Threading.Thread.Sleep(1000);get error)
    user.SendMessage("crash"); // Need to be sure user is in a valid state to make call
}

This way you can be sure that you've got a valid reference. What you'll now need to do is ensure that user is in a valid state for you to call SendMessage.

UPDATE: Since you're keep to avoid adding try/catch blocks you could write a helper function:

void WithUser(Action<User> action)
{
  try
  {
     var user = User;
     if (user != null) action(user);
  }
  catch(Exception e)
  {
    // Log it...
  }
}

Now you can say:

WithUser(user=>
{
  System.Threading.Thread.Sleep(1000);
  user.SendMessage("crash");
});
Sign up to request clarification or add additional context in comments.

9 Comments

Hmmm.. is that var goodway because example User.GetClient() is called too so it returns too null if user disconnect
@user3290224 - I don't really understand your comment, but this is a reasonable way to handle properties becoming null in a multithread application. What you do need to do is ensure that the object is in a valid state when calling against it, so you'll probably have to surround it with a try/catch block.
try/catch for 20 000 lines? I mean there is too other functions that become null when user disconnect and server need it other times example that GetClient()
Sorry my slow answer because i goed sleep. Is that goodway use it with 20 000 codes line? There is many and long functions that uses many time User etc. Can "try" make my server slower?
It won't affect performance unless you have an exception. You need to stop worrying about how much code you've got (and 20,000 isn't that much) and start thinking about how you'll make your application bug free. It's not like you have to try\catch each line of code, is it...!
|
0

Who is setting User to null and where? While your thread sleeps, somebody is obviously setting the User variable to null, causing the crash.

Besides: "Magical sleeps" will not solve any problems. You need proper locking here.

2 Comments

Socket disconnect set user to null and my program is multithread so it can happend any time. That sleep is example because its hard try get that error because server do things in ms but that dont stop coming over 100 System.NullReferenceException every day ;/
Then you need to implement proper locking. Testing for null alone will not do in a multithreaded environment.
0

SendMessage is throwing null exception, check code in your SendMessage Method

if User goes null before call dont call SendMessage:-

 if (User != null)
 {
       User.SendMessage("crash"); //<-- No More System.NullReferenceException... -.-
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.