Note that there are some explanatory texts on larger screens.

plurals
  1. POEncountering NullPointerException when trying to add polynoms
    primarykey
    data
    text
    <p>I need to add two polynomials, which is composed of two ints. For example, the coefficient and the exponent 3x^2 would be constructed using 3 and 2 as parameters. I am getting a NullPointerException but I can't figure out why. Any help would be appreciated!</p> <pre><code>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 &lt; 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 &gt; p2.data.exponent) { p2 = p2.next; } if (p1.data.exponent &lt; p2.data.exponent) { p1 = p1.next; } if (p1.next != null &amp;&amp; p2.next != null) { return polyAdd(p1, p2); } return new Node(null, null); } } </code></pre> <p>import javax.swing.JOptionPane;</p> <p>/** * * @author Bill Kraynek */ public class Polynomials {</p> <pre><code>/** * @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); } </code></pre> <p>} some lines are // because i dont have the second method yet</p> <p><a href="http://users.cis.fiu.edu/~kraynek/COP3337-assignments/Spring-2012/AssignmentFive-Polynomials-Spring-2012.html" rel="nofollow">http://users.cis.fiu.edu/~kraynek/COP3337-assignments/Spring-2012/AssignmentFive-Polynomials-Spring-2012.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload