2

I have the following code:

public void SetUser(User user)
{
   string streetNumber = "";

   if (user.Address.StreetNo != null)
      streetNumber = user.Address.StreetNo.ToString();
   else
     streetNumber = "";
}

I get the ever popular

Object reference not set to an instance of an object.

issue.

3
  • First check if user is not null then user.Address not null and only then StreeNo... Commented Aug 19, 2011 at 8:21
  • where does the stacktrace point you to? are you sure that "user" will not be null? Sorry but I can't tell from your code. It might be the Address of the user is null it might be the StreeetNo - it might be almost everything in there Commented Aug 19, 2011 at 8:22
  • Thanks for the comments guys, they were write and go along with the answer curt has provided. The Stack did show the user exsisted but I wasn't doing the check. Commented Aug 19, 2011 at 9:09

5 Answers 5

3
public void SetUser(User user)
{
   string streetNumber = "";

   if (user != null && user.Address != null && user.Address.StreetNo != null) {
      streetNumber = user.Address.StreetNo.ToString();
   }
}

Taking into account @CKoenig's suggestion, the following throws an exception if the user or user.Address are null:

public void SetUser(User user)
{

   if (user == null) {
       throw new System.ArgumentNullException("user", "user cannot be null");
   }

   if (user.Address == null) {
       throw new System.ArgumentNullException("Address", "Address cannot be null");
   }
   
   string streetNumber = "";

   if (user.Address.StreetNo != null) {
      streetNumber = user.Address.StreetNo.ToString();
   }
}
Sign up to request clarification or add additional context in comments.

10 Comments

I would recommend throwing if either is null - the method is named "SetUser" if it can't because there is nothing to set it should not pretent to do.
The method name has to be wrong. It doesn't do anything like the method body describes.
@Inuyasha OP could have simplified their method leaving only this part which seems to be throwing an exception
@Curt: I suggest using ArgumentNullException at least for the case where user is null, since this is a more specific exception type.
@Curt : you still throwing an exception when user != null, does this make any sense?
|
2

Either user is null, or user.Address is null. You need to test them too.

3 Comments

No, you missed this clause: if (user.Address.StreetNo != null). He is already checking if user.Address.StreetNo is null. The problem is that this test itself will throw the exception if either of user or user.Address are null.
so sorry- you are right ... better think first ... but alas then there will be 10 answers ;)
Thanks guys +1 because you were right accepted curts because he provided code with answer.
2
public void SetUser(User user) 
{    
   string streetNumber = String.Empty;     

   if (user!= null 
       && user.Address != null 
       && user.Address.StreetNo != null)       
   {
         streetNumber = user.Address.StreetNo.ToString();    
   }
}

Comments

1

Check your stacktrace and:

  • user
  • user.Address
  • user.Address.StreetNo

with an if ... == null then ...

Comments

1
if (user != null
    && user.Address != null
    && user.Address.StreetNo != null)
{
    // ...
}

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.