由于某种原因,我似乎无法解决这个问题,给我一个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 :(得分:1)
newPoly
可能是null
newPoly.poly
可能是null
newPoly.poly.next
可能是null
newPoly.poly.next.term
可能是null
或
newPoly.poly.next.term.degree
可能是null
。
要避免NullPointerException
,您需要确保使用适当的值初始化所使用的任何成员。
答案 1 :(得分:1)
我认为问题在于:
newPoly.poly.next = newPoly.poly;
newPoly.poly = n;
首先你说,newPoly.poly.next = newPoly.poly;所以你将当前元素分配给下一个,这是递归的。然后你说newPoly.poly = n; 。所以你给newPoly分配了一个新元素。我认为垃圾收集器会删除newPoly元素,因为它被覆盖,因此您将丢失对newPoly元素的引用。这意味着当您以后访问它时会得到一个nullpointer异常。你可以解决这个问题:
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
}
}
答案 2 :(得分:0)
我可以告诉任何事物都不是空的,但是对于面向对象间接的四个'点'(something.something.something.something),你将会遇到很多这个问题。通常在一条线上有两个 - 三个'点'是你应该做的,但是因为那更多是关于设计而不是错误,我离题了。
找到这个问题的方法是:
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引用,我认为。
祝你好运!希望有所帮助。