0

I've solved my own issue but I don't know why my first attempt didn't work and I was hoping someone could tell me why. I was also hoping if someone could tell me if my final solution is a "good" one or not (by this I mean, is it efficient)?

This was my first attempt at reading an input file I had previously created:

private byte[] mInputData;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_view);

    Intent myIntent = getIntent();

    mFilename = myIntent.getStringExtra("FILENAME");
    mSplitSeq = myIntent.getStringExtra("SPLIT_SEQ");

    try {
        fis = openFileInput(mFilename);
        fis.read(mInputData);
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

This is something I found online that actually worked:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_view);

    Intent myIntent = getIntent();

    mFilename = myIntent.getStringExtra("FILENAME");
    mSplitSeq = myIntent.getStringExtra("SPLIT_SEQ");

    try {
        fis = openFileInput(mFilename);
        BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
        String line = null, input="";
        while ((line = reader.readLine()) != null)
            mTimeStr += line;
        reader.close();
        fis.close();
        //fis.read(mInputData);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

The error I received with my first implementation was a NullPointerException when calling the fis.read(mInputData) function.

J2N
  • 321
  • 4
  • 19

1 Answers1

3

I'm pretty sure it's because mInputData is never initialized. You'd need a line in there like mInputData = new byte[1000];. Instead, you're telling read() to give data to a reference that equals null, a NullPointerException.

MattDavis
  • 5,158
  • 2
  • 23
  • 35
  • Ah, that makes sense. Is there a way to make the byte array dynamic? I guess the second solution is dynamic but it would be nice to be able to call a read function that reads all without looping. – J2N May 18 '12 at 15:19
  • You could use `int byteArraySize = new File(mFileName).length()`. The length method on a File returns the size in bytes. – MattDavis May 18 '12 at 15:26
  • Do you see anything wrong with the BufferedReader method? Or anything that would be exceptionally inefficient? – J2N May 18 '12 at 15:49
  • I think that using BufferedReader is a good way to read input and I'd stick with that method. However, take a look at the accepted answer to this question: http://stackoverflow.com/questions/2492076/android-reading-from-an-input-stream-efficiently You may want to use a StringBuilder instead of appending to a String. – MattDavis May 18 '12 at 15:58
  • Thank you, that was a good read and I now understand (and am using) StringBuilder instead! – J2N Jun 05 '12 at 19:09