繁体   English   中英

为什么我的对象保持为空?

[英]Why is my object staying null?

由于某种原因,我似乎无法解决此问题,从而给了我NullPointerException。 我打印出poly.term.degree时没有任何错误,然后将poly.next设置为poly,然后在尝试打印看上去应该相同的poly.next.term.degree时得到nullPointerException。 我知道这是不完整的代码,但是我认为这是我的主要问题。

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

        }

同样,这些类的构造函数和所有内容都在下面。

      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 &&
        other instanceof Term &&
        coeff == ((Term)other).coeff &&
        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:
     * <pre>
     *     <coeff> <degree>
     *     <coeff> <degree>
     *     ...
     *     <coeff> <degree>
     * </pre>
     * with the guarantee that degrees will be in descending order. For example:
     * <pre>
     *      4 5
     *     -2 3
     *      2 1
     *      3 0
     * </pre>
     * which represents the polynomial:
     * <pre>
     *      4*x^5 - 2*x^3 + 2*x + 3 
     * </pre>
     * 
     * @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);
        }
    }

newPoly可能为null

newPoly.poly可能为null

newPoly.poly.next可能为null

newPoly.poly.next.term可能为null

要么

newPoly.poly.next.term.degree可能为null

为了避免NullPointerException ,您需要确保使用正确的值初始化任何使用的成员。

我认为问题出在这里:

        newPoly.poly.next = newPoly.poly;
        newPoly.poly = n;

首先,您说的是newPoly.poly.next = newPoly.poly; 因此,您可以将当前元素分配给下一个递归元素。 然后,您说newPoly.poly = n; 因此,您将一个新元素分配给newPoly。 我认为垃圾收集器会删除newPoly元素,因为它已被覆盖,因此您将丢失对newPoly元素的引用。 这意味着以后再访问它时,将得到一个nullpointer异常。 您可以这样解决:

    newPoly.poly.next = n;
    //and dont forget to set the next pointer of the new elemnt to 0
    n.next = NULL;

只需将新元素分配给下一个元素。 编辑@hendersawn

您可以对列表进行排序。 见下文:

sort(Node head_p){ //do not change the head, or you will lose the beginning.
Node tmp_p; 
Node curr_p = head_p; 
while(curr_p != NULL){ 
if(curr_p.poly.term.degree < curr_p.next.poly.term.degree) //either degree is smaller or greater 
{//swap 
    tmp_p = curr_p; //save first element
    curr_p = curr_p.next; //set first element to second
    //now the 2 element is the actual third element so we need 
    //to put the first between the second and the third
    tmp_p.next = curr_p.next; //set next of first to third
    curr_p.next = tmp_p; //set second element to the first that we saved before
}
curr_p = curr_p.next; //move to next element...rinse repeat
}
}

我显然不能说是零,但是如果有四个“点”(面向对象间接),您将经常遇到这个问题。 通常,您只需要在一行上放置两个-三个“点”,但是由于这更多的是设计而不是错误,因此,我离开了。

找到此问题的方法是:

  1. 在该行的右边放置一个断点,看看那里有什么变量,哪个是null或
  2. 对每个组件执行一个System.out.println,以查看哪个组件损坏了它,然后从那里向后工作。 例如:System.out.println(newPoly.poly.next.term.degree); //给我一个NullPointerException

System.out.println(newPoly);

System.out.println(newPoly.poly);

System.out.println(newPoly.poly.next);

System.out.println(newPoly.poly.next.term);

因为nullPointerException只会在其中之一上抛出(不能为度,否则该语句将只显示“ null”

如果我必须打赌,那可能是newPoly.poly.next ,它为空

线路:

newPoly.poly.next = newPoly.poly;
newPoly.poly = n;

从表面上看,它们似乎是造成您麻烦的元凶,因为您要分配newPoly.poly的“下一个”,但是随后您又重新分配了newPoly.poly,并丢失了对.next的旧引用。

祝好运! 希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM