简体   繁体   English

实现的方法获取NullPointerException

[英]Implemented method getting NullPointerException

I have an implemented method which visits the node of a binary tree. 我有一个访问二叉树节点的实现方法。 I have a word class which implements this through an interface called TreeComparable. 我有一个单词类,它通过一个称为TreeComparable的接口来实现。

Here is the visit method: 这是访问方法:

@Override
public void visit() {
    System.out.printf("%-15s%-7s", getWord(), count);
    pw.printf("%-15s%-7s", getWord(), count);
    ObjectListNode p = list.getFirstNode();
    while (p != null) {
        System.out.print(((LinePosition) p.getInfo()).getLineNumber() + "-" + ((LinePosition) p.getInfo()).getPosition() + "  ");
        pw.print(((LinePosition) p.getInfo()).getLineNumber() + "-" + ((LinePosition) p.getInfo()).getPosition() + "  ");
        p = p.getNext();
    }
    System.out.println();
    pw.println();
}

I am getting errors with the PrintWriter Object. 我在使用PrintWriter对象时遇到错误。 I am getting a nullPointerException. 我收到一个nullPointerException。 I have a default constructor that is part of the class that is implementing this visit method. 我有一个默认构造函数,它是实现此visit方法的类的一部分。

This is the default constructor being used when creating a word object. 这是创建单词对象时使用的默认构造函数。

public Word(PrintWriter pw) {
    this.pw = pw;
}

Everything works if I comment out trying to write to the text file: 如果我注释掉尝试写入文本文件,则一切正常:

@Override
public void visit() {
    System.out.printf("%-15s%-7s", getWord(), count);
    //pw.printf("%-15s%-7s", getWord(), count);
    ObjectListNode p = list.getFirstNode();
    while (p != null) {
        System.out.print(((LinePosition) p.getInfo()).getLineNumber() + "-" + ((LinePosition) p.getInfo()).getPosition() + "  ");
        //pw.print(((LinePosition) p.getInfo()).getLineNumber() + "-" + ((LinePosition) p.getInfo()).getPosition() + "  ");
        p = p.getNext();
    }
    System.out.println();
    //pw.println();
}

however, for this assignment I need to print out the outputs to a text file. 但是,对于此分配,我需要将输出打印到文本文件中。 Why is the printwriter object not getting passed in correctly?Thanks for any input! 为什么printwriter对象无法正确传递?感谢您的任何输入!

EDIT: Here are a few places where I tried to call the constructor: 编辑:这是我尝试调用构造函数的一些地方:

public class Query {

PrintWriter pw;

public Query(PrintWriter pw) {
    this.pw = pw;
}

public void performQuery(ObjectBinaryTree t) {

    Scanner userInput = new Scanner(System.in);
    System.out.println("Search for word: ");
    pw.println("Search for word: ");
    Word word = new Word(pw);
    String input = userInput.next();

    do {

        word = new Word(input);

        if (t.searchBST(word) != null) {
            ObjectTreeNode p = t.searchBST(word);
            ObjectListNode q = ((Word) p.getInfo()).getList().getFirstNode();

            System.out.printf("%-15s%-5s", ((Word) p.getInfo()).getWord(), ((Word) p.getInfo()).getCount());
            pw.printf("%-15s%-5s", ((Word) p.getInfo()).getWord(), ((Word) p.getInfo()).getCount());
            while (q != null) {
                System.out.print(((LinePosition) q.getInfo()).getLineNumber() + "-" + ((LinePosition) q.getInfo()).getPosition() + "  ");
                q = q.getNext();
            }
            System.out.println("\nType 1 to exit, or press enter for new search: ");
            pw.println("\nType 1 to exit, or press enter for new search: ");
            input = userInput.next();
            continue;
        }

        else
            System.out.print("Word Not Found");
            System.out.println("\nType 1 to exit, or press enter for new search: ");
            pw.print("Word Not Found");
            pw.println("\nType 1 to exit, or press enter for new search: ");
            input = userInput.next();
            continue;

    } while (!input.equals("1"));
}

}

and: 和:

public class Driver {

public static void main(String[] args) throws FileNotFoundException {
    PrintWriter pw = new PrintWriter(new File("csis.txt"));

    Word word = new Word(pw);
    Xref xref = new Xref(pw);
    Query query = new Query(pw);
    xref.readWords();

    System.out.println();
    pw.println();
    query.performQuery(xref.getBinaryTree());

    pw.close();

}

} }

EDIT: The visit() method is part of this interface (not sure if this helps but...): 编辑:visit()方法是此接口的一部分(不确定是否有帮助,但是...):

public interface TreeComparable {
int compareTo(Object o);
void operate(Object o);
void visit();
}

The problem is here: 问题在这里:

String input = userInput.next();

do {

    word = new Word(input);

You should be getting a compile-time error about that, but evidently Word has a constructor that takes a String rather than a PrintWriter , and that constructor isn't setting pw correctly. 您应该会遇到一个编译时错误,但是显然Word具有一个使用String而不是PrintWriter构造函数,并且该构造函数未正确设置pw This assignment is throwing away the (correct) instantiation above: 此分配放弃了上面的(正确)实例化:

Word word = new Word(pw);

It looks like you might be misunderstanding what is and isn't a static member of an object. 看来您可能误解了对象的静态成员是和不是。 All of the Word objects you instantiate with a PrintWriter carry around a non-null value for pw , but the rest have null unless you fix it. 您使用PrintWriter实例化的所有Word对象都带有pw的非null值,但其余的均为null除非您对其进行修复。 My guess is that your Word(String) constructor ignores pw and expects it to be set by some other call -- it won't. 我的猜测是您的Word(String)构造函数将忽略pw并期望通过其他调用来设置它-不会。

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

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