0

I have the following method:

void setTexts()
{
    if (queueIn != null)
    {
        queueIn.text = countIn.ToString();
    }

    if (queueOut != null)
    {
        queueOut.text = waitingForPickup.ToString();
    }
}

I want it to do nothing if queueIn is null, but I keep getting a null reference exception saying queueIn is null. Why is it going into the if block when queueIn is null?

EDIT: the problem disappeared when I added a Debug.Log check, so it probably hadn't saved the previous dozen times or something. Thanks for your suggestions! I'm pretty new to C#.

M.Gavrin
  • 19
  • 3
  • 2
    My guess is that your `NullReferenceException` is coming from `countIn`. You aren't checking that. – Nathan A Jul 07 '16 at 16:37
  • are you sure it errors on line 3? Does waitingForPickup call queueIn? Are you SURE it says queueIn is the null reference? – Dispersia Jul 07 '16 at 16:38
  • 3
    This is where learning breakpoints and stepping becomes an invaluable skill. Learn how to properly troubleshoot, and these kind of silly problems become non-issues. –  Jul 07 '16 at 16:38
  • waitingForPickup does not call queueIn. It definitely errors on line 3, and countIn is not null. Monodevelop tells me the value of objects when I mouse over them, and it said queueIn was null and countIn was not. Also, I ran it again and the exception disappeared, so now I have no problem except for being a bit perturbed. – M.Gavrin Jul 07 '16 at 17:11

2 Answers2

1

You need to check all object deference points. In this case, countIn could be your offender.

Here's a possible solution to remove your exception.

void setTexts(){
    if (queueIn != null && countIn != null) {
        queueIn.text = countIn.ToString ();
    }
    if (queueOut != null && waitingForPickup != null){
        queueOut.text = waitingForPickup.ToString();
    }
}
Nathan A
  • 9,863
  • 4
  • 42
  • 57
  • What if countIn and waitingForPickup are primitive types (besides string/char)? He states queueIn is null, not countIn nor waitingForPickup. This isn't an answer. – Dispersia Jul 07 '16 at 16:43
  • @Dispersia I disagree. The only possible way a NullReferenceException could occur in his code is if countId was a reference type, so I believe it's a safe assumption to make. I also believe the op was wrong in stating that queueIn is null, because if that truely was the case, there would be no NullReferenceException because basic If conditions don't fail like that. The question itself indicates a novice programmer, hence I believe he may be incorrect in his assumptions. – Nathan A Jul 07 '16 at 16:47
  • countIn is an int, which I set the value of in the function that calls setTexts. Monodevelop told me it was queueIn that was null. – M.Gavrin Jul 07 '16 at 17:13
  • @NathanA Assumptions are what leads to incorrect answers. See? The OP even confirmed countIn is an integer. And no, there are ways it could be failing, what if the text property called an internal queueIn? If it's multi threaded and queueIn is invalidated after the check? There are plenty of ways. – Dispersia Jul 07 '16 at 18:05
0

You are calling ToString() on countIn and waitingForPickup - you need to check them too. E.g.:

void setTexts(){
    if (queueIn != null && countIn != null) {
        queueIn.text = countIn.ToString();
    }
    if (queueOut != null && waitingForPickup != null) {
        queueOut.text = waitingForPickup.ToString();
    }
}
Mureinik
  • 252,575
  • 45
  • 248
  • 283