0

I'm making a program that can recursively add Polynomials (represented by Linked Lists) together. But I keep getting a null pointer exception in the line if (p1.data.exp == p2.data.exp) {

I've tried debugging by setting both terms to either one or the other, but I keep getting the error (does this mean that they are both null?). I don't see how this can happen since it should return before getting to this part if they are null. Does anyone have suggestions?

Main:

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
}

Test:

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());
    }
}
2
  • 1
    p1 and p2 may be non-null, but what about p1.data and p2.data? And, BTW, there is no (p1.data.exponent == p2.data.exponent) in the code you posted. Commented Apr 1, 2011 at 21:56
  • FYI, it's not really removed since you can still look at the previous versions and look at the code anyways. Commented Apr 3, 2011 at 6:40

3 Answers 3

1

One of the data fields in the Node object is not initialized. My guess is that this is setting the data to null.

Node node = new Node(term, null);

So any node variable that references that object will throw an exception when you do p1.data.exponent.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, this does look like the culprit. Any ideas on how to solve it?
@dtbravo: Make sure there is a Term object for the node before you access it or disallow the creation of a Node without a Term object.
0

in this part

if (p1.data.exp == p2.data.exp)

property 'data' or 'exp' is null.

I'll give an advice write your code more protective:

  1. use getter and setter method;
  2. don't use chain of references (eg, p1.data.exp);
  3. test 'null' case;
  4. add log infromation;

bye

2 Comments

exp being null wont cause an NPE... Looks like data is the culprit (as mentiined elsewhere).
it's true, I've answered too fast
0

I'd guess that p1.data or p2.data is null, so you can't obtain the exponent from it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.