0

So my code basically sets up a linked list. Each ticket object either stores the reference to another ticket object, or null. The .getNext() method gets the reference to the next object in the list. current is the object representing the start of the list and the while loops go through the list changing current until the conditions change. Finally, it sets current to the Ticket that was passed as a parameter.

public void AddLowPTicket(Ticket ti) // doesnt check if front == null because AddTicket already does
{
    Ticket current = front;
    while(current.getPrio() == Priority.High && current != null) // cycles/skips through the list as long as Priority == High.
    {
        current = current.getNext();
    }
    current.Print(); // *THIS WORKS*
    while(current != null && current.getPrio() == Priority.Low) //  *NullReferenceException: Obj ref not set to an instance of an obj.*
    {
        current = current.getNext();
    }
    current = ti;
}

This is the Print method for the Ticket object. It prints the local variables just fine, which means they aren't null.

public void Print()
{
    Console.WriteLine("{0}\nPriority:{1}", m_description, m_prio == Priority.High ? "High" : "Low");
}

Why does it crash if current is not null, and none of its variables are either.

Rob
  • 25,569
  • 15
  • 73
  • 87
N.Campos
  • 25
  • 3
  • 1
    `current.getPrio() == Priority.High && current != null` Try to swap left and right. – AlexD Feb 02 '17 at 01:18
  • @AlexD: How that will solve the issue? @N.Campos: what is happening inside `getPrio()` – sujith karivelil Feb 02 '17 at 01:20
  • 1
    Are you *sure* it's happening on the line you're saying it is? Looks like it's probably happening on the first `while` - which would be solved by AlexD's comment. What does your debugger say? Wait for it to hit the exception and inspect the relevant variables. – Rob Feb 02 '17 at 01:21
  • do some breakpoitnt. see if current.getPrio() has a thing in it – jace Feb 02 '17 at 01:24
  • 1
    @un-lucky If there are only `PriorityHigh` items, `while` condition seems to cause a null-reference exception, no? – AlexD Feb 02 '17 at 01:25
  • I got it to work, thanks! – N.Campos Feb 02 '17 at 01:31
  • @un-lucky Because the doubled-up boolean operators aren't, strictly speaking, the boolean operators from math -- rather than evaluating both sides, then doing what you'd expect, if the result can be determined early (say, because the result is always false when the first one is false) it is. That means that the second half might not even get evaluated, which is probably the whole point of the null-check – Fund Monica's Lawsuit Feb 02 '17 at 01:34

1 Answers1

-1

Hi guys thanks very much for your time! I guess it didn't build properly (somehow?). After adding then removing some lines then rebuilding/running it it ran properly! Sorry, and thanks again!

N.Campos
  • 25
  • 3