Multiplying polynomials using a node class
<p>I have created a data structure to to reperesent polynomials using a homegrown node class. I have methods to add and subtract two polynomials of length n. I am trying to add a multiplication method which will return the product of the two polynomials. I am almost finished the problem. At this point I am trying to create a third linked list (using the Node class) in order to store and return the product of the two polynomials. My problem is just coding this solution. I am getting null pointer exception.</p>

<p>Here is my complete code for the Node class:</p>

<pre><code>    public class Node{

//Fields
public int data1;
public int data2;
public Node next;



//constructors
public Node(){
    data1 = 0;
    data2 = 0;
    next = null;
}


public Node(int d1){
    data1 = d1;
    data2 = 0;
    next = null;
}

public Node(int d1, Node n){
    data1 = d1;
    data2 = 0;
    next = n;
}

public Node(int d1, int d2){
    data1 = d1;
    data2 = d2;
    next = null;
}
public Node(int d1,int d2, Node n){
    data1 = d1;
    data2 = d2;
    next = n;
}

//Methods

//Fetch data
public int getData1(){
    return data1;
}

public int getData2(){
    return data2;
}

//store Data
public void setData1(int d){
    data1 = d;
}

public void setData2(int d){
    data2 = d;
}

public int addData1(Node n2){
    data1 = data1 + n2.data1;
    return data1;
}

public int addData2(Node n2){
    data2 = data2 + n2.data2;
    return data2;
}

public void subData1(Node n2){
    data1 = data1 - n2.data1;
}

public int multiplyData1(Node n2){
    data1 = data1*n2.data1;
    return data1;
}

//getNext
public Node getNext(){
    return next;
}

//Get data of next node
public int getNextData1(){
    return next.data1;
}

public int getNextData2(){
    return next.data2;
}

//Store Link
public void setNext(Node n){
    next = n;
}

public boolean containsLink(){
    if(this.next != null){
        return true;
    }else{
        return false;
    }
}

public static void displayAll(Node head){
    for( ; head != null; head = head.next ){
        System.out.printf("%d, %d\n", head.data1, head.data2);
    }
}

public static int numOfNonZeroData1(Node n){
    int count = 0; 
    for( ; n != null ; n = n.next ){
        if(n.data1 != 0){
            count++;
        }
    }
    return count;
}

public static int numOfNonZeroData2(Node n){
    int count = 0; 
    for( ; n != null ; n = n.next ){
        if(n.data2 != 0){
            count++;
        }
    }
    return count;
}

public static int listLength(Node head){
    int counter = 0;
    for(; head!=null; head = head.next){
        counter++;
    }
    return counter;
}

//copy list [Recursive method found on website StackOverflow.com]
public Node copyData1( Node p ) {
    if( p == null )
        return null; 
    else 
        return new Node(p.data1, copyData1(p.next)); 
}

public Node copyData2( Node p ) {
    if( p == null )
        return null; 
    else 
        return new Node(p.data2, copyData2(p.next)); 
}

//===============================================================


public static void toPolynomial(Node head){
    int order = Node.listLength(head);
    int i = 0;
    int increment = Node.numOfNonZeroData2(head);
    for( ; head != null; head = head.next){
        if(head.data2 != 0 &amp;&amp; head.data1 != 0){
            if(i &gt;= 0 &amp;&amp; i &lt; order){
                System.out.printf("%dx^%d", head.data1, head.data2);
                i++;
                if(i &lt; increment &amp;&amp; head.data1 &gt;= 0){
                    System.out.print("+");      //case integer is positive
                }else if(i != increment &amp;&amp; head.data1 &lt;= 0){
                    System.out.println(" ");    //case integer is negative
                }
            }

        }
    }
    System.out.println();
}




public static Node mergeLists(Node n1, Node n2){
    if ( n1 == null) 
        return n2;
    else if ( n2 == null) 
        return n1;
    else {
        n1.next = mergeLists( n1.next, n2 );   
        return n1;
    }
}


public static Node addPolynomials(Node n1, Node n2) {
    Node x = n1;
    Node y = n2;
    for(x = n1; x != null; x = x.next){
        for(y = n2; y != null; y = y.next){
            if(x.getData2() == y.getData2()){
                x.addData1(y);
                System.out.println("Added " + (x.data1 - y.data1) + " and " + y.data1);
            }

        }
    }
    System.out.println("Add completed");
    return x;
}


public static Node subtractPolynomials(Node n1, Node n2){
    Node x = n1;
    Node y = n2;
    for(x = n1; x != null; x = x.next){
        for(y = n2; y != null; y = y.next){
            if(x.getData2() == y.getData2()){
                x.subData1(y);
                System.out.println("Subtracted " + (x.data1 - y.data1) + " and " + y.data1);
            }

        }
    }
    System.out.println("Subtract completed");
    return x;
}


public static Node multiplyPolynomials(Node n1, Node n2){

    Node x = n1;
    Node y = n2;
    Node z = new Node();
    for(x = n1; x != null; x = x.next){
        for(y = n2; y != null; y = y.next){
            z.data1 = x.multiplyData1(y); // error is here
            z.data2 = x.addData2(y);
            //System.out.println("Multiplied " + (x.data1 - y.data1) + " and " + y.data1);
            z = z.next;
        }
    }
    System.out.println("Multiplication completed");
    return x;
}
</code></pre>

<p>}</p>

<p>Any Ideas? </p>
