gpt4 book ai didi

java - 链表中的空指针异常

转载 作者:行者123 更新时间:2023-12-01 15:53:34 25 4
gpt4 key购买 nike

我正在制作一个程序,可以递归地将多项式(由链接列表表示)相加。但我不断在 if (p1.data.exp == p2.data.exp) {

行中收到空指针异常

我尝试通过将这两个术语设置为其中之一来进行调试,但我不断收到错误(这是否意味着它们都为空?)。我不明白这是如何发生的,因为如果它们为空,它应该在到达这部分之前返回。有人有建议吗?

主要:

public class Polynomial {

private Node poly;


public Polynomial(){
poly = new Node();
}

private Polynomial(Node node){
poly = node;
}
public void addTerm(int coef, int exp){
Term term = new Term(coef, exp);
Node node = new Node(term, null);

Node iterator = poly;

//if the list is empty just add the node
if (poly.next == null){
System.out.println("poly.next is null. adding node to first pos.");
poly.next = node;
return;
}

//if list isn't empty find the appropriate spot for it
while (iterator.next != null){
System.out.println("iterator.next != null...");
if (exp < iterator.next.data.exp){
System.out.println("\texp < iterator.next.data.exp");
node.next = iterator.next;
iterator.next = node;
return;
}
if (exp == iterator.next.data.exp){
System.out.println("\texp == iterator.next.data.exp");
iterator.next.data.coef += coef;
return;
}
iterator = iterator.next;
}

//if we get to this point then the list isn't empty
//and it doesn't fit inside the list, hence it must
//be added to the end of the list
System.out.println("list wasn't empty, didn't fit inside");
iterator.next = node;
return;
}

@Override
public String toString(){
Node iterator = poly;
String out = "";

if (poly.next == null){
return out;
}
while(iterator.next != null){
out += iterator.next.data.coef;
out += "*x^";
out += iterator.next.data.exp;
out += " + ";
iterator = iterator.next;
}
return out.substring(0, out.lastIndexOf('+'));
}



public Polynomial addPolynomial (Polynomial that){
Polynomial ret = new Polynomial();

Polynomial iterator = this;

return new Polynomial(addPolys(this.poly, that.poly));
}

public Node addPolys(Node p1, Node p2) {
// If P1 is null, just use P2
if (p1 == null) {
p1 = p2;
p2 = null;
}

// if P1 is still null, no P2 either so finished
if (p1 == null) return null;

Node ret = new Node();

// if P2 is null now, just one poly remains
if (p2 == null) {
ret.data = p1.data;
p1 = p1.next;
} else {

// do we combine nodes?
if (p1.data.exp == p2.data.exp) {
ret.data = new Term(p1.data.coef + p2.data.coef, p1.data.exp
+ p2.data.exp);
p1 = p1.next;
p2 = p2.next;
} else {
// we just copy a node

// make p1 the bigger exponent
if (p1.data.exp < p2.data.exp) {
Node t = p1;
p1 = p2;
p2 = t;
}

ret.data = p1.data;
p1 = p1.next;
}
}

ret.next = addPolys(p1, p2);
return ret;
}

/*
* Term
*/
private class Term implements Comparable{
int coef;
int exp;

public int getCoef() {
return coef;
}

public void setCoef(int coef) {
this.coef = coef;
}

public int getExp() {
return exp;
}

public void setExp(int exp) {
this.exp = exp;
}

public Term(int coef, int exp) {
this.coef = coef;
this.exp = exp;
}
public int compareTo(Object rhs){
Term that = (Term)rhs;
return this.exp - that.exp;
}
}//end Term


/*
* Node
*/
private class Node{
Term data;
Node next;

public Term getData() {
return data;
}

public void setData(Term data) {
this.data = data;
this.next = null;
}

public Node getNext() {
return next;
}

public void setNext(Node next) {
this.next = next;
}

public Node() {
this.data = null;
this.next = null;
}

public Node(Term data, Node next) {
this.data = data;
this.next = next;
}
}//end Node
}

测试:

public class Polynomials {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Polynomial p1 = new Polynomial();
Polynomial p2 = new Polynomial();
Polynomial p0 = new Polynomial();
p1.addTerm(1, 2);
p1.addTerm(1, 4);
p1.addTerm(1, 6);
p2.addTerm(1, 1);
p2.addTerm(1, 3);
p2.addTerm(1, 5);
System.out.println("p1 = " + p1.toString());
System.out.println("p2 = " + p2.toString());

System.out.println("Adding p1 to p2...");
p0 = p1.addPolynomial(p2);
System.out.println(p0.toString());
}
}

最佳答案

Node 对象中的一个data 字段未初始化。我的猜测是,这会将数据设置为空。

Node node = new Node(term, null);

因此,当您执行 p1.data.exponent 时,引用该对象的任何节点变量都会引发异常。

关于java - 链表中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5519242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com