0

I am working on an android app that has to parse text from a file.

I have the following method in my Parser.java class:

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.

I pass in the following path info the File object is this:

"/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

Community
  • 1
  • 1
Anderology
  • 147
  • 1
  • 2
  • 9
  • 1
    It's because you continue after catching the `FileNotFoundException`, despite `buffer` never being set. – Andy Turner Apr 14 '16 at 14:51
  • Your buffer can not be initialized when the file cannot be found. Make sure you abort all file reading when that happens. – f1sh Apr 14 '16 at 14:52
  • Try to `return null;` in the `FileNotFoundException` – Cheese Bread Apr 14 '16 at 15:06
  • If you are running the code on an Android device, then why are you trying to read a file from your local machine? That path does not exist on the device. – OneCricketeer Apr 14 '16 at 15:16

2 Answers2

2

It looks like you are attempting to pass in a local (probably Windows) file path to your device or emulator.

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

This won't work. You have to have that file within your Android project. 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. And then, as pointed out in the comments, you don't correctly handle the exception and continue trying to read the stream anyway.

NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
  • Im using OS X sorry for not clarifying. I will still try this. – Anderology Apr 14 '16 at 15:02
  • 1
    Yes the OS doesn't matter, I was just providing an example. The point is, your resource needs to be located within your project package, either in your assets folder or your raw folder. – NoChinDeluxe Apr 14 '16 at 15:03
1

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. 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;
Pete B.
  • 3,188
  • 6
  • 25
  • 38