Reputation: 5
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());
}
}
Upvotes: 0
Views: 1731
Reputation: 377
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:
bye
Upvotes: 0
Reputation: 597076
I'd guess that p1.data
or p2.data
is null
, so you can't obtain the exponent from it.
Upvotes: 0
Reputation: 17321
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
.
Upvotes: 1