2014-09-20 298 views
0

出於某種原因,我似乎無法解決這給我一個NullPointerException。我沒有任何錯誤地打印poly.term.degree,然後將poly.next設置爲等於poly,然後在嘗試打印出應該看起來相同的poly.next.term.degree時得到nullPointerException。我知道這是不完整的代碼,但我認爲這是我的主要問題。爲什麼我的對象保持空?

public Polynomial add(Polynomial p) 
{ 
    Polynomial newPoly = new Polynomial(); 

    newPoly.poly = new Node(0,0,null); 

    Polynomial myCurr = this; 

    Polynomial otherCurr = p;  

    while(myCurr.poly != null) 
    { 

     int myDeg = myCurr.poly.term.degree; 
     int otherDeg = p.poly.term.degree; 

     float myCo = myCurr.poly.term.coeff; 
     float otherCo = otherCurr.poly.term.coeff; 

     if(myDeg == otherDeg) 
     { 
      System.out.println("degrees "+myDeg + " and "+ otherDeg+ " are equal, creating new node..."); 

      Node n = new Node(myCo+otherCo,p.poly.term.degree, newPoly.poly.next); 

      System.out.println(newPoly.poly.term.degree); 

      newPoly.poly.next = newPoly.poly; 
      newPoly.poly = n; 

      System.out.println(newPoly.poly.next.term.degree); // Gives me a NullPointerException 

     } 

此外,這些類的構造函數和一切都在下面。

 package poly; 

     import java.io.*; 
     import java.util.StringTokenizer; 

/** 
* This class implements a term of a polynomial. 
* 
* @author runb-cs112 
* 
*/ 
class Term { 
    /** 
    * Coefficient of term. 
    */ 
    public float coeff; 

    /** 
    * Degree of term. 
    */ 
    public int degree; 

    /** 
    * Initializes an instance with given coefficient and degree. 
    * 
    * @param coeff Coefficient 
    * @param degree Degree 
    */ 
    public Term(float coeff, int degree) { 
     this.coeff = coeff; 
     this.degree = degree; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#equals(java.lang.Object) 
    */ 
    public boolean equals(Object other) { 
     return other != null && 
     other instanceof Term && 
     coeff == ((Term)other).coeff && 
     degree == ((Term)other).degree; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#toString() 
    */ 
    public String toString() { 
     if (degree == 0) { 
      return coeff + ""; 
     } else if (degree == 1) { 
      return coeff + "x"; 
     } else { 
      return coeff + "x^" + degree; 
     } 
    } 
} 

/** 
* This class implements a linked list node that contains a Term instance. 
* 
* @author runb-cs112 
* 
*/ 
class Node { 

    /** 
    * Term instance. 
    */ 
    Term term; 

    /** 
    * Next node in linked list. 
    */ 
    Node next; 

    /** 
    * Initializes this node with a term with given coefficient and degree, 
    * pointing to the given next node. 
    * 
    * @param coeff Coefficient of term 
    * @param degree Degree of term 
    * @param next Next node 
    */ 
    public Node(float coeff, int degree, Node next) { 
     term = new Term(coeff, degree); 
     this.next = next; 
    } 

} 

/** 
* This class implements a polynomial. 
* 
* @author runb-cs112 
* 
*/ 
public class Polynomial { 

    /** 
    * Pointer to the front of the linked list that stores the polynomial. 
    */ 
    Node poly; 

    /** 
    * Initializes this polynomial to empty, i.e. there are no terms. 
    * 
    */ 
    public Polynomial() { 
     poly = null; 
    } 

    /** 
    * Reads a polynomial from an input stream (file or keyboard). The storage format 
    * of the polynomial is: 
    * <pre> 
    *  <coeff> <degree> 
    *  <coeff> <degree> 
    *  ... 
    *  <coeff> <degree> 
    * </pre> 
    * with the guarantee that degrees will be in descending order. For example: 
    * <pre> 
    *  4 5 
    *  -2 3 
    *  2 1 
    *  3 0 
    * </pre> 
    * which represents the polynomial: 
    * <pre> 
    *  4*x^5 - 2*x^3 + 2*x + 3 
    * </pre> 
    * 
    * @param br BufferedReader from which a polynomial is to be read 
    * @throws IOException If there is any input error in reading the polynomial 
    */ 
    public Polynomial(BufferedReader br) throws IOException { 
     String line; 
     StringTokenizer tokenizer; 
     float coeff; 
     int degree; 

     poly = null; 

     while ((line = br.readLine()) != null) { 
      tokenizer = new StringTokenizer(line); 
      coeff = Float.parseFloat(tokenizer.nextToken()); 
      degree = Integer.parseInt(tokenizer.nextToken()); 
      poly = new Node(coeff, degree, poly); 
     } 
    } 
+0

什麼行引發錯誤信息? – IQAndreas 2014-09-20 14:55:49

+0

表示'System.out.println(newPoly.poly.next.term.degree); //給我一個NullPointerException' 他留下了對它的評論 – WillBD 2014-09-20 14:56:12

回答

0

我認爲問題就出在這裏:

 newPoly.poly.next = newPoly.poly; 
     newPoly.poly = n; 

起初,你說,那newPoly.poly.next = newPoly.poly;因此您將當前元素分配給下一個,這是遞歸的。然後你說newPoly.poly = n; 。所以你給newPoly分配一個新元素。我認爲垃圾收集器刪除了newPoly元素,因爲它被覆蓋,所以你失去了對newPoly元素的引用。這意味着當你稍後訪問它時會得到一個空指針異常。你可以像這樣解決這個問題:

newPoly.poly.next = n; 
    //and dont forget to set the next pointer of the new elemnt to 0 
    n.next = NULL; 

只是將新元素分配給下一個元素。 編輯 @hendersawn

您可以對列表進行排序。見下:

sort(Node head_p){ //do not change the head, or you will lose the beginning. 
Node tmp_p; 
Node curr_p = head_p; 
while(curr_p != NULL){ 
if(curr_p.poly.term.degree < curr_p.next.poly.term.degree) //either degree is smaller or greater 
{//swap 
    tmp_p = curr_p; //save first element 
    curr_p = curr_p.next; //set first element to second 
    //now the 2 element is the actual third element so we need 
    //to put the first between the second and the third 
    tmp_p.next = curr_p.next; //set next of first to third 
    curr_p.next = tmp_p; //set second element to the first that we saved before 
} 
curr_p = curr_p.next; //move to next element...rinse repeat 
} 
} 
+0

非常感謝!這對我非常有幫助...看到了進步讓我如此開心哈哈。儘管如此,它還是讓我倒退了多項式 - 你知道我可以添加哪些其他代碼來緩解這種情況嗎?我正在嘗試,但每當我做到這一點時,都會帶我回到原來的一個 – hendersawn 2014-09-20 19:18:48

+0

很高興我能幫上忙!如果我正確理解你的話,請參閱我的編輯答案以獲取詳細信 – Koto 2014-09-20 21:04:54

0

沒有什麼明顯空,我可以告訴,但有四個「點」面向對象的間接(something.something.something.something),你會遇到這樣的問題很多。通常在一條線上的兩個「點」是你應該做的,但因爲這更多的是關於設計而不是錯誤,所以我離題了。

找到這個問題將是方式之一:

  1. 將斷點就在這條線,看看這些變量在那裏,哪一個爲空或
  2. 做一個的System.out .println爲每個組件查看哪一個打破它,並從那裏向後工作。例如: System.out.println(newPoly.poly.next.term.degree); //給我一個NullPointerException

System.out.println(newPoly);

System.out.println(newPoly.poly);

System.out.println(newPoly.poly.next);

System.out.println(newPoly.poly.next.term);

因爲NullPointerException異常只會得到這些之一拋出(它不能是程度,否則該聲明將剛剛打印出'null'

如果我打賭,我會說這是可能newPoly.poly.next是空

行:

newPoly.poly.next = newPoly.poly; 
newPoly.poly = n; 

從表面上看似乎是他們將是你麻煩的罪魁禍首,因爲你分配你的newPoly.poly的'next',但是你重新分配你的newPoly.poly,並且失去那個舊的對.next的引用,我想。

祝你好運!希望有所幫助。

1

newPoly可能null

newPoly.poly可能null

newPoly.poly.next可能null

newPoly.poly.next.term可能null

newPoly.poly.next.term.degree可能是null

要避免NullPointerException,您需要確保使用的任何成員都使用適當的值進行初始化。