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.

asked May 18, 2012 at 15:09

1 Answer 1

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.

answered May 18, 2012 at 15:15
Sign up to request clarification or add additional context in comments.

5 Comments

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.
You could use int byteArraySize = new File(mFileName).length(). The length method on a File returns the size in bytes.
Do you see anything wrong with the BufferedReader method? Or anything that would be exceptionally inefficient?
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: stackoverflow.com/questions/2492076/… You may want to use a StringBuilder instead of appending to a String.
Thank you, that was a good read and I now understand (and am using) StringBuilder instead!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.