简体   繁体   English

尝试调用BufferedReader.readLine()时应用崩溃

[英]app crashed when trying to call BufferedReader.readLine()

I am working on an android app that has to parse text from a file. 我正在研究必须解析文件中文本的android应用。

I have the following method in my Parser.java class: 我的Parser.java类中具有以下方法:

private String getText(String fileName) {
        BufferedReader buffer = null;
        File file = new File(fileName);
        try{
           buffer  = new BufferedReader( new FileReader(file));
        }
        catch (FileNotFoundException e){
            System.out.println("Could not find file" + fileName);
        }

        String everything = null;
        try{
            StringBuilder builder = new StringBuilder();
            String line = null;

            while ((line = buffer.readLine()) != null){
                builder.append(line);
                builder.append(System.lineSeparator());
                line = buffer.readLine();
            }
            everything = builder.toString();
            //buffer.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if ((buffer != null)) {
                try {
                    buffer.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return everything;
    }

I am getting an issue whenever there is a call to the buffer.readLine() method within the while loop. 每当在while循环中调用buffer.readLine()方法时,我都会遇到问题。

I pass in the following path info the File object is this: 我通过以下路径信息传递File对象是这样的:

"/Users/aa/Desktop/parser.txt"

Now I have looked at numerous posts on stack and online in order to try and solve this, and have tried using a few solutions from this and this but have had no luck. 现在,我已经看了看堆了许多帖子,并在网上以试图解决这个问题,并使用从几个解决方案已经尝试这个这个 ,但有没有运气。 This is a snippet from the error stack trace that I get. 这是我得到的错误堆栈跟踪的摘录。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.BufferedReader.readLine()' on a null object reference
at com.example.allanaraujo.parser.Parser.getText(Parser.java:40)
at com.example.allanaraujo.parser.Parser.parse(Parser.java:19)
at com.example.allanaraujo.parser.OpenDocument.uploadButton(OpenDocument.java:62)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

I am sure that the path of the file is correct because I checked it while debugging. 我确信该文件的路径是正确的,因为我在调试时检查了它。 I am not sure what else I should be considering here. 我不确定在这里还要考虑什么。

EDIT: I am developing on OS X not windows or Linux 编辑:我在OS X而不是Windows或Linux上进行开发

It looks like you are attempting to pass in a local (probably Windows) file path to your device or emulator. 您似乎正在尝试将本地(可能是Windows)文件路径传递到设备或仿真器。

"/Users/aa/Desktop/parser.txt"

This won't work. 这行不通。 You have to have that file within your Android project. 您必须在Android项目中包含该文件。 You can put it in your assets folder and access it like this: 您可以将其放在资产文件夹中,并按以下方式访问它:

AssetManager manager = context.getAssets();
InputStream input = manager.open("parser.txt");

Or you can put it in your raw folder and access it like this: 或者,您可以将其放在原始文件夹中并按以下方式访问它:

InputStream input = context.getResources().openRawResource(R.raw.parser);

The reason you are getting the null on your BufferedReader is because the system can't find that file path in your Android project. 之所以在BufferedReader上获取空值,是因为系统无法在Android项目中找到该文件路径。 And then, as pointed out in the comments, you don't correctly handle the exception and continue trying to read the stream anyway. 然后,如注释中所指出的,您将无法正确处理该异常并继续尝试读取流。

A couple of things: 有两件事:

  while ((line = buffer.readLine()) != null){
     builder.append(line);
     builder.append(System.lineSeparator());
     line = buffer.readLine();  //remove this line
   }

You need to remove the commented line, as you are calling the readLine twice. 您需要删除注释行,因为您要调用readLine两次。 When you do get the buffer properly initialized, you will skip lines. 当您确实正确初始化了缓冲区后,您将跳过行。

In order to protect against a null pointer exception you should: 为了防止出现空指针异常,您应该:

if(buffer == null) return "buffer not ready";  //or something like that.
String everything = null;

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

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