title: Why is my object staying null?
body: <p>For some reason, I can't seem to fix this giving me a NullPointerException. I print out poly.term.degree without any errors, and then set poly.next equal to poly and then get a nullPointerException when I try to print out poly.next.term.degree which should seemingly be the same. I'm aware this is incomplete code but I think this is my main issue.</p>
  
  <pre><code>public Polynomial add(Polynomial p)
  {   
      Polynomial newPoly = new Polynomial();
  
      newPoly.poly = new Node(0,0,null);
  
      Polynomial myCurr = this;
  
      Polynomial otherCurr = p;       
  
      while(myCurr.poly != null)
      {
  
          int myDeg = myCurr.poly.term.degree;
          int otherDeg = p.poly.term.degree;
  
          float myCo = myCurr.poly.term.coeff;
          float otherCo = otherCurr.poly.term.coeff;
  
          if(myDeg == otherDeg)
          {
              System.out.println("degrees "+myDeg + " and "+ otherDeg+ " are equal, creating new node...");
  
              Node n = new Node(myCo+otherCo,p.poly.term.degree, newPoly.poly.next);
  
              System.out.println(newPoly.poly.term.degree);
  
              newPoly.poly.next = newPoly.poly;
              newPoly.poly = n;
  
              System.out.println(newPoly.poly.next.term.degree); // Gives me a NullPointerException
  
          }
  </code></pre>
  
  <p>Also, the constructors and everything for these classes is below.</p>
  
  <pre><code>      package poly;
  
        import java.io.*;
        import java.util.StringTokenizer;
  
  /**
   * This class implements a term of a polynomial.
   * 
   * @author runb-cs112
   *
   */
   class Term {
      /**
      * Coefficient of term.
      */
      public float coeff;
  
      /**
       * Degree of term.
       */
      public int degree;
  
      /**
       * Initializes an instance with given coefficient and degree.
       * 
       * @param coeff Coefficient
       * @param degree Degree
       */
      public Term(float coeff, int degree) {
          this.coeff = coeff;
          this.degree = degree;
      }
  
      /* (non-Javadoc)
       * @see java.lang.Object#equals(java.lang.Object)
       */
      public boolean equals(Object other) {
          return other != null &amp;&amp;
          other instanceof Term &amp;&amp;
          coeff == ((Term)other).coeff &amp;&amp;
          degree == ((Term)other).degree;
      }
  
      /* (non-Javadoc)
       * @see java.lang.Object#toString()
       */
      public String toString() {
          if (degree == 0) {
              return coeff + "";
          } else if (degree == 1) {
              return coeff + "x";
          } else {
              return coeff + "x^" + degree;
          }
      }
  }
  
  /**
   * This class implements a linked list node that contains a Term instance.
   * 
   * @author runb-cs112
   *
   */
  class Node {
  
      /**
       * Term instance. 
       */
      Term term;
  
      /**
       * Next node in linked list. 
       */
      Node next;
  
      /**
       * Initializes this node with a term with given coefficient and degree,
       * pointing to the given next node.
       * 
       * @param coeff Coefficient of term
       * @param degree Degree of term
       * @param next Next node
       */
      public Node(float coeff, int degree, Node next) {
          term = new Term(coeff, degree);
          this.next = next;
      }
  
  }
  
  /**
   * This class implements a polynomial.
   * 
   * @author runb-cs112
   *
   */
  public class Polynomial {
  
      /**
       * Pointer to the front of the linked list that stores the polynomial. 
       */ 
      Node poly;
  
      /** 
       * Initializes this polynomial to empty, i.e. there are no terms.
       *
       */
      public Polynomial() {
          poly = null;
      }
  
      /**
       * Reads a polynomial from an input stream (file or keyboard). The storage format
       * of the polynomial is:
       * &lt;pre&gt;
       *     &lt;coeff&gt; &lt;degree&gt;
       *     &lt;coeff&gt; &lt;degree&gt;
       *     ...
       *     &lt;coeff&gt; &lt;degree&gt;
       * &lt;/pre&gt;
       * with the guarantee that degrees will be in descending order. For example:
       * &lt;pre&gt;
       *      4 5
       *     -2 3
       *      2 1
       *      3 0
       * &lt;/pre&gt;
       * which represents the polynomial:
       * &lt;pre&gt;
       *      4*x^5 - 2*x^3 + 2*x + 3 
       * &lt;/pre&gt;
       * 
       * @param br BufferedReader from which a polynomial is to be read
       * @throws IOException If there is any input error in reading the polynomial
       */
      public Polynomial(BufferedReader br) throws IOException {
          String line;
          StringTokenizer tokenizer;
          float coeff;
          int degree;
  
          poly = null;
  
          while ((line = br.readLine()) != null) {
              tokenizer = new StringTokenizer(line);
              coeff = Float.parseFloat(tokenizer.nextToken());
              degree = Integer.parseInt(tokenizer.nextToken());
              poly = new Node(coeff, degree, poly);
          }
      }
  </code></pre>