尝试添加多项时遇到NullPointerException

时间:2012-03-28 17:19:03

标签: java

我需要添加两个多项式,它由两个整数组成。例如,系数和指数3x ^ 2将使用3和2作为参数构造。我得到一个NullPointerException但我无法弄清楚为什么。任何帮助将不胜感激!

public class Polynomial {

private Node poly;

public Polynomial() {
}

private Polynomial(Node p) {
    poly = p;
}

private class Term {

    int coefficient;
    int exponent;

    private Term(int coefficient, int exponent) {
        this.coefficient = coefficient;
        this.exponent = exponent;
    }
}

private class Node {

    private Term data;
    private Node next;

    private Node(Term data, Node next) {
        this.data = data;
        this.next = next;
    }
}

public void addTerm(int coeff, int exp) {
    Node pointer = poly;
    if (pointer.next == null) {
        poly.next = new Node(new Term(coeff, exp), null);
    } else {
        while (pointer.next != null) {
            if (pointer.next.data.exponent < exp) {
                Node temp = new Node(new Term(coeff, exp), pointer.next.next);
                pointer.next = temp;
                return;
            }

            pointer = pointer.next;
        }
        pointer.next = new Node(new Term(coeff, exp), null);
    }

}

public Polynomial polyAdd(Polynomial p) {
    return new Polynomial(polyAdd(this.poly, p.poly));

}

private Node polyAdd(Node p1, Node p2) {
    if (p1 == p2) {
        Term adding = new Term(p1.data.coefficient + p2.data.coefficient,
                p1.data.exponent);
        p1 = p1.next;
        p2 = p2.next;
        return new Node(adding, null);
    }
    if (p1.data.exponent > p2.data.exponent) {
        p2 = p2.next;

    }
    if (p1.data.exponent < p2.data.exponent) {
        p1 = p1.next;

    }
    if (p1.next != null && p2.next != null) {
        return polyAdd(p1, p2);


    }
    return new Node(null, null);
}
}

导入javax.swing.JOptionPane;

/ **  *  * @author Bill Kraynek  * / 公共类多项式{

/**
 * @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(5, 2);
    p1.addTerm(4, 5);
    p1.addTerm(3, 3);
    p1.addTerm(1, 2);
    p1.addTerm(5, 6);
    p2.addTerm(3, 8);
    p2.addTerm(2, 5);
    p2.addTerm(1, 3);
    Polynomial p3 = p1.polyAdd(p2);
    JOptionPane.showMessageDialog(null, "p1 is " + p1 + "\np2 is " + p2 + "\np1+p2 is " + p3);
 // Polynomial p4 = p1.polyMultiply(p2);
//    JOptionPane.showMessageDialog(null, "p1 is " + p1 + "\np2 is " + p2 + "\np1*p2 is " + p4);
//    Polynomial p5 = p2.polyMultiply(p2);
    //JOptionPane.showMessageDialog(null, "p2 is " + p2 + "\np2*p2 is " + p5);
  //  Polynomial p6 = p0.polyMultiply(p2);
    //JOptionPane.showMessageDialog(null, "p0 is " + p0 + "\n" + "p2 is " + p2 + "\np0*p2 is " + p6);
    Polynomial p7 = p0.polyAdd(p2);
    JOptionPane.showMessageDialog(null, "p0 is " + p0 + "\n" + "p2 is " + p2  + "\np0+p2 is " + p7);
    p1 = p1.polyAdd(p2);
    JOptionPane.showMessageDialog(null, "After p1 = p1+p2  p1 is " + p1);
  //  p2 = p2.polyMultiply(p2);
    JOptionPane.showMessageDialog(null,"After p2 = p2*p2  p2 is " + p2);
}

} 有些行是//因为我还没有第二种方法

http://users.cis.fiu.edu/~kraynek/COP3337-assignments/Spring-2012/AssignmentFive-Polynomials-Spring-2012.html

1 个答案:

答案 0 :(得分:2)

  

节点指针= poly;

由于poly在调用默认的多项式构造函数时未实例化,因此无法访问它。但你的下一行是:

  

if(pointer.next == null){

抛出空指针。所以你应该检查那里或禁用默认构造函数。

但这只是一个存在空指针异常的情况。如果你能提供你的主要课程,那将会非常有帮助。

// edit:如果您将非默认构造函数设为public并使用该构造函数,则可以使用您的代码:

    Polynomial p = new Polynomial(new Node(new Term(3, 2), null));
    Polynomial p2 = new Polynomial(new Node(new Term(3, 2), null));
    p.polyAdd(p2);

没有任何例外。但我会考虑重新设计它,因为它不必要地复杂化。

相关问题