Kesalahan NullPointerException dalam kode Java saya. (NullPointerException errors in my Java code.)


問題描述

Kesalahan NullPointerException dalam kode Java saya. (NullPointerException errors in my Java code.)

Do you guys see anything wrong with my code off the top of your head:

/** simulates the Josephus game by killing every other person
     until the winner is the only one left.
     @return The survivor of the game
   */
 public E startJosephus() {
     E item =head.data;
     if(head.next != null){
         if(head == head.previous)
             return item;
     }
     else{
         while(count>1){
             removeAfter(head);
             head =head.next;
         }
 }
     return item;



     }

Here is my full code: http://pastebin.com/S0kWwFFV

Here is my driver class as well: http://pastebin.com/Nb08Dtqk

I am getting NullPointerExceptions that seem to be stemming from this method here. Please help if you see anything clearly wrong with my code.

‑‑‑‑‑

參考解法

方法 1:

I didn't read through all your code but I found this:

 public void addFirst(E dataItem) {
                 Node<E> node = new Node <E>(dataItem, null, null);
                 // To be completed by the student

                 if (head == null) // list is empty
                      head = head.previous = node;

             else {
                node.next = head;
                head.previous = node;
                head = node;
             }
             count++;
     }

Possible culprit, 

 if (head == null) // list is empty
           head = head.previous = node;

In this statement, head.previous = node; is being done first, but head is still null.  NullPointerException is being thrown before head is set to anything.

If head is null you definitely don't want to do head.previous

方法 2:

Without more information, I'd have to guess head is probably null.

(by Eddie MacleodLews TherinSkip Head)

參考文件

  1. NullPointerException errors in my Java code. (CC BY‑SA 3.0/4.0)

#runtime #java #nullpointerexception #circular-list #compilation






相關問題

通過java運行的執行程序似乎超時? (Executed programs run through java seem to time out?)

Kesalahan NullPointerException dalam kode Java saya. (NullPointerException errors in my Java code.)

Hentikan proses anak ketika proses induk berhenti (Stop child process when parent process stops)

Атрымаць шлях выканання кансольнага скрыпта Python (Get the runpath of a python console-script)

選擇不包含主要類型 - 錯誤 (Selection does not contain main type - error)

在活動之間使用意圖錯誤 (using intents between activities error)

在運行時使用 Spring 在表中創建新字段 (Create new field into a table with Spring at runtime)

如何添加運行時超時以防止 java.net.SocketTimeoutException? (How do I add a runtime timeout to prevent java.net.SocketTimeoutException?)

我有什麼方法可以覆蓋 Java 中的系統屬性嗎? (Do I have any method to override System Properties in Java?)

如何計算此代碼的時間複雜度? (How can I compute the time complexity of this code?)

運行 .exe 文件生成 bt jar2exe 軟件時出現 java runtime environment not found 錯誤 (Getting java runtime enviroment not found error while running .exe file produced bt jar2exe software)

在運行時在 VBA 中顯示所有變量及其值 (Show all variables and their values in VBA during runtime)







留言討論