2017-02-02 22 views
0

所以我的代碼基本上設置了一個鏈表。每個票證對象或者存儲對另一個票證對象的引用,或者存儲空值。 .getNext()方法獲取對列表中下一個對象的引用。 current是表示列表開始的對象,while循環通過列表更改當前的列表直到條件改變。最後,它將電流設置爲作爲參數傳遞的票證。我得到一個NullReferenceException但對象不是空的

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; 
} 

這是Ticket對象的Print方法。它打印局部變量就好了,這意味着它們不是null。

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

爲什麼如果current不是null並且它的變量都不是null,它會崩潰。

+1

'current.getPrio()== Priority.High && current!= null'嘗試左右交換。 – AlexD

+0

@AlexD:這將如何解決問題? @ N.Campos:'getPrio()'裏面發生了什麼' –

+1

你確定*它發生在你說的那一行嗎?看起來它可能發生在第一個「while」上 - 這可以通過AlexD的評論來解決。你的調試器說什麼?等待它發生異常並檢查相關變量。 – Rob

回答

-2

嗨,大家非常感謝您的時間!我想它沒有正確構建(不知何故?)。添加後刪除一些行,然後重建/運行它,它運行正常!對不起,再次感謝!

相關問題