2014-09-20 141 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,您需要确保使用的任何成员都使用适当的值进行初始化。