Reputation: 341
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.
Upvotes: 0
Views: 1293
Reputation: 5178
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.
Upvotes: 3