简体   繁体   English

从缓冲读取器流全局读取

[英]reading from a bufferedreader stream globaly

so Im trying to write this class which is going to parse a file and read commands from it. 所以我试图写这个类来解析文件并从中读取命令。

I want the ctor to just open the stream and do nothing else. 我想让ctor打开流,什么也不做。 while I parse the file in other class methods. 而我在其他类方法中解析文件。 But I'm getting a nullpointerexception when I try to read the file in the methods I made. 但是,当我尝试使用自己制作的方法读取文件时,出现了nullpointerexception异常。

help will be apprecated :D 帮助将不胜感激:D

public class Parser {
private BufferedReader _input;
private String _command;

public Parser(String filename) throws FileNotFoundException {
    FileInputStream fstream = new FileInputStream(filename);

    BufferedReader _input = new BufferedReader(new InputStreamReader(fstream));
}

public boolean hasMoreCommands() throws IOException {

    String line;
    if ( (line = _input.readLine()) != null) {
        return true;
    } else {
        _input.close();
        return false;
    }
}

public void advance() throws IOException {
    String line;
    do {
        line = _input.readLine().trim();
    } while (line.equals("") || line.substring(0,2).equals(COMMENT_SIGN)); 

    String[] splittedLine = line.split(COMMENT_SIGN);
    _command = splittedLine[0];
    _command = _command.replace(" ", ""); 
}

my main for testing it + the exception trace 我的主要测试工具+异常跟踪

public static void main(String[] args) throws IOException {
    Parser input = null;
    input = new Parser("D:\\test.asm");
    System.out.println( input.hasMoreCommands());

} }

Exception in thread "main" java.lang.NullPointerException
at nand6.Parser.hasMoreCommands(Parser.java:40)
at nand6.Parser.main(Parser.java:116)

Have a look at this snippet of code 看看这段代码

public Parser(String filename) throws FileNotFoundException {
FileInputStream fstream = new FileInputStream(_filename);

BufferedReader _input = new BufferedReader(new InputStreamReader(fstream));
}

Change 更改

BufferedReader _input = new BufferedReader(new InputStreamReader(fstream));

to

_input = new BufferedReader(new InputStreamReader(fstream));

Your mistake : You are creating another local variable of type *BufferedReader _input* hence your class level variable is still null resulting in a NullPointerException 您的错误:您正在创建另一个* BufferedReader _input *类型的局部变量,因此您的类级别变量仍然为null,从而导致NullPointerException

You are basically defining a new BufferedReader object called "_input" inside your constructor. 您基本上是在构造函数中定义一个新的BufferedReader对象,称为“ _input”。 You think that after calling the constructor you instantiate "_input" outside the constructor. 您认为在调用构造函数之后,您在构造函数外部实例化了“ _input”。 But you are not, it refers to null. 但是您不是,它指的是null。 That's why you are getting NullPointerException. 这就是为什么您得到NullPointerException的原因。 Just remove "BufferedReader" in front of "_input" inside your constructor, in order to refer to correct object. 只需删除构造函数中“ _input”前面的“ BufferedReader”,即可引用正确的对象。

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

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