While loop reading a file running on an extra time
Reading from a CSV file trying to get each line.
It will read all lines but run an extra time causing the `String' array to try and pull from a null Reference.
the Data is 6 lines with 4 elements split with a ','.
public void setReadingArray(File localFile){
Log.e("MyActivity","Reading into Array");
InputStream is = null;
try {
is = new FileInputStream(localFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(DailyReadingActivity.this,"Could not find file Exception: "+ e, Toast.LENGTH_LONG).show();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8")));
try {
String line = reader.readLine();
while (line != null) {
Log.d("MyActivity", " Creating dailyReader ");
//Split by ,
String tokens;
tokens = line.split(",");
//Read Data
DailyReading dailyReading = new DailyReading();
dailyReading.setBookName(tokens[0]);
dailyReading.setChapter(tokens[1]);
dailyReading.setBookName2(tokens[2]);
dailyReading.setChapter2(tokens[3]);
listReadings.add(dailyReading);
Log.d("MyActivity", " Just Created " + dailyReading);
line = reader.readLine();
}
} catch (IOException e) {
Log.wtf("MyActivity", " Error Reading Data File on Line " + e);
e.printStackTrace();
}
try {
Log.d("MyActivity", " Close File 'is' ");
is.close();
} catch (IOException e) {
e.printStackTrace();
}
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader
New Code i have Changed to. Still having the same Issue.
try {
String line = "";
DailyReading dailyReading = new DailyReading();
while ((line = reader.readLine()) != null) {
Log.d("MyActivity", " Creating dailyReader "+ line);
//Split by ,
String tokens;
tokens = line.split(",");
//Read Data
// DailyReading dailyReading = new DailyReading();
dailyReading.setBookName(tokens[0]);
dailyReading.setChapter(tokens[1]);
dailyReading.setBookName2(tokens[2]);
dailyReading.setChapter2(tokens[3]);
listReadings.add(dailyReading);
// Log.d("MyActivity", " Just Created " + dailyReading);
}
} catch (IOException e) {
Log.wtf("MyActivity", " Error Reading Data File on Line " + e);
e.printStackTrace();
}
The Readout is:
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader
as you can see it is running that extra time at the bottom that gives me the error, the While != null does not seem to work.
java android while-loop
add a comment |
Reading from a CSV file trying to get each line.
It will read all lines but run an extra time causing the `String' array to try and pull from a null Reference.
the Data is 6 lines with 4 elements split with a ','.
public void setReadingArray(File localFile){
Log.e("MyActivity","Reading into Array");
InputStream is = null;
try {
is = new FileInputStream(localFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(DailyReadingActivity.this,"Could not find file Exception: "+ e, Toast.LENGTH_LONG).show();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8")));
try {
String line = reader.readLine();
while (line != null) {
Log.d("MyActivity", " Creating dailyReader ");
//Split by ,
String tokens;
tokens = line.split(",");
//Read Data
DailyReading dailyReading = new DailyReading();
dailyReading.setBookName(tokens[0]);
dailyReading.setChapter(tokens[1]);
dailyReading.setBookName2(tokens[2]);
dailyReading.setChapter2(tokens[3]);
listReadings.add(dailyReading);
Log.d("MyActivity", " Just Created " + dailyReading);
line = reader.readLine();
}
} catch (IOException e) {
Log.wtf("MyActivity", " Error Reading Data File on Line " + e);
e.printStackTrace();
}
try {
Log.d("MyActivity", " Close File 'is' ");
is.close();
} catch (IOException e) {
e.printStackTrace();
}
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader
New Code i have Changed to. Still having the same Issue.
try {
String line = "";
DailyReading dailyReading = new DailyReading();
while ((line = reader.readLine()) != null) {
Log.d("MyActivity", " Creating dailyReader "+ line);
//Split by ,
String tokens;
tokens = line.split(",");
//Read Data
// DailyReading dailyReading = new DailyReading();
dailyReading.setBookName(tokens[0]);
dailyReading.setChapter(tokens[1]);
dailyReading.setBookName2(tokens[2]);
dailyReading.setChapter2(tokens[3]);
listReadings.add(dailyReading);
// Log.d("MyActivity", " Just Created " + dailyReading);
}
} catch (IOException e) {
Log.wtf("MyActivity", " Error Reading Data File on Line " + e);
e.printStackTrace();
}
The Readout is:
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader
as you can see it is running that extra time at the bottom that gives me the error, the While != null does not seem to work.
java android while-loop
It seems to me that the structure of the file is not what you expect it to be - your comment about it "skips the second line for some reason" goes along with that. Use a debugger or add 'print' statements to find what's actually inline
each time round the loop.
– another-dave
Jan 1 at 13:41
i realised why it was skipping it was because i was calling line = reader.readLine(); twice so it would skip every seccond one but then it wouldnt error out that way.
– Linden Swan
Jan 2 at 1:04
add a comment |
Reading from a CSV file trying to get each line.
It will read all lines but run an extra time causing the `String' array to try and pull from a null Reference.
the Data is 6 lines with 4 elements split with a ','.
public void setReadingArray(File localFile){
Log.e("MyActivity","Reading into Array");
InputStream is = null;
try {
is = new FileInputStream(localFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(DailyReadingActivity.this,"Could not find file Exception: "+ e, Toast.LENGTH_LONG).show();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8")));
try {
String line = reader.readLine();
while (line != null) {
Log.d("MyActivity", " Creating dailyReader ");
//Split by ,
String tokens;
tokens = line.split(",");
//Read Data
DailyReading dailyReading = new DailyReading();
dailyReading.setBookName(tokens[0]);
dailyReading.setChapter(tokens[1]);
dailyReading.setBookName2(tokens[2]);
dailyReading.setChapter2(tokens[3]);
listReadings.add(dailyReading);
Log.d("MyActivity", " Just Created " + dailyReading);
line = reader.readLine();
}
} catch (IOException e) {
Log.wtf("MyActivity", " Error Reading Data File on Line " + e);
e.printStackTrace();
}
try {
Log.d("MyActivity", " Close File 'is' ");
is.close();
} catch (IOException e) {
e.printStackTrace();
}
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader
New Code i have Changed to. Still having the same Issue.
try {
String line = "";
DailyReading dailyReading = new DailyReading();
while ((line = reader.readLine()) != null) {
Log.d("MyActivity", " Creating dailyReader "+ line);
//Split by ,
String tokens;
tokens = line.split(",");
//Read Data
// DailyReading dailyReading = new DailyReading();
dailyReading.setBookName(tokens[0]);
dailyReading.setChapter(tokens[1]);
dailyReading.setBookName2(tokens[2]);
dailyReading.setChapter2(tokens[3]);
listReadings.add(dailyReading);
// Log.d("MyActivity", " Just Created " + dailyReading);
}
} catch (IOException e) {
Log.wtf("MyActivity", " Error Reading Data File on Line " + e);
e.printStackTrace();
}
The Readout is:
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader
as you can see it is running that extra time at the bottom that gives me the error, the While != null does not seem to work.
java android while-loop
Reading from a CSV file trying to get each line.
It will read all lines but run an extra time causing the `String' array to try and pull from a null Reference.
the Data is 6 lines with 4 elements split with a ','.
public void setReadingArray(File localFile){
Log.e("MyActivity","Reading into Array");
InputStream is = null;
try {
is = new FileInputStream(localFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(DailyReadingActivity.this,"Could not find file Exception: "+ e, Toast.LENGTH_LONG).show();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8")));
try {
String line = reader.readLine();
while (line != null) {
Log.d("MyActivity", " Creating dailyReader ");
//Split by ,
String tokens;
tokens = line.split(",");
//Read Data
DailyReading dailyReading = new DailyReading();
dailyReading.setBookName(tokens[0]);
dailyReading.setChapter(tokens[1]);
dailyReading.setBookName2(tokens[2]);
dailyReading.setChapter2(tokens[3]);
listReadings.add(dailyReading);
Log.d("MyActivity", " Just Created " + dailyReading);
line = reader.readLine();
}
} catch (IOException e) {
Log.wtf("MyActivity", " Error Reading Data File on Line " + e);
e.printStackTrace();
}
try {
Log.d("MyActivity", " Close File 'is' ");
is.close();
} catch (IOException e) {
e.printStackTrace();
}
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader
New Code i have Changed to. Still having the same Issue.
try {
String line = "";
DailyReading dailyReading = new DailyReading();
while ((line = reader.readLine()) != null) {
Log.d("MyActivity", " Creating dailyReader "+ line);
//Split by ,
String tokens;
tokens = line.split(",");
//Read Data
// DailyReading dailyReading = new DailyReading();
dailyReading.setBookName(tokens[0]);
dailyReading.setChapter(tokens[1]);
dailyReading.setBookName2(tokens[2]);
dailyReading.setChapter2(tokens[3]);
listReadings.add(dailyReading);
// Log.d("MyActivity", " Just Created " + dailyReading);
}
} catch (IOException e) {
Log.wtf("MyActivity", " Error Reading Data File on Line " + e);
e.printStackTrace();
}
The Readout is:
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader Test,1,Test2,1
D/MyActivity: Creating dailyReader Test,2,Test2,2
D/MyActivity: Creating dailyReader Test,3,Test2,3-4
D/MyActivity: Creating dailyReader
as you can see it is running that extra time at the bottom that gives me the error, the While != null does not seem to work.
java android while-loop
java android while-loop
edited Jan 2 at 1:21
Linden Swan
asked Jan 1 at 10:14
Linden SwanLinden Swan
22
22
It seems to me that the structure of the file is not what you expect it to be - your comment about it "skips the second line for some reason" goes along with that. Use a debugger or add 'print' statements to find what's actually inline
each time round the loop.
– another-dave
Jan 1 at 13:41
i realised why it was skipping it was because i was calling line = reader.readLine(); twice so it would skip every seccond one but then it wouldnt error out that way.
– Linden Swan
Jan 2 at 1:04
add a comment |
It seems to me that the structure of the file is not what you expect it to be - your comment about it "skips the second line for some reason" goes along with that. Use a debugger or add 'print' statements to find what's actually inline
each time round the loop.
– another-dave
Jan 1 at 13:41
i realised why it was skipping it was because i was calling line = reader.readLine(); twice so it would skip every seccond one but then it wouldnt error out that way.
– Linden Swan
Jan 2 at 1:04
It seems to me that the structure of the file is not what you expect it to be - your comment about it "skips the second line for some reason" goes along with that. Use a debugger or add 'print' statements to find what's actually in
line
each time round the loop.– another-dave
Jan 1 at 13:41
It seems to me that the structure of the file is not what you expect it to be - your comment about it "skips the second line for some reason" goes along with that. Use a debugger or add 'print' statements to find what's actually in
line
each time round the loop.– another-dave
Jan 1 at 13:41
i realised why it was skipping it was because i was calling line = reader.readLine(); twice so it would skip every seccond one but then it wouldnt error out that way.
– Linden Swan
Jan 2 at 1:04
i realised why it was skipping it was because i was calling line = reader.readLine(); twice so it would skip every seccond one but then it wouldnt error out that way.
– Linden Swan
Jan 2 at 1:04
add a comment |
2 Answers
2
active
oldest
votes
because you are trying to make new instance of your pojo class(DailyReading) inside while loop
put below code out of the while loop block
DailyReading dailyReading = new DailyReading();
and remove code below inside the while loop
line = reader.readLine();
I have Removed the DailyReading dailyReading = new DailyReading(); from the While Loop. Still having this error. i have moved the line = reader.readLine(); to the While loop as other people have done on the internet. while ((line = reader.readLine()) != null) {
– Linden Swan
Jan 2 at 1:11
add a comment |
OverLooking the Whole problem from the beginning found that i had Hit ENTER in the txt file causing it to read that as a line... i was not aware of this being possible. thanks for helping me out though. Another-Dave pointed me in the right direction
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994641%2fwhile-loop-reading-a-file-running-on-an-extra-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
because you are trying to make new instance of your pojo class(DailyReading) inside while loop
put below code out of the while loop block
DailyReading dailyReading = new DailyReading();
and remove code below inside the while loop
line = reader.readLine();
I have Removed the DailyReading dailyReading = new DailyReading(); from the While Loop. Still having this error. i have moved the line = reader.readLine(); to the While loop as other people have done on the internet. while ((line = reader.readLine()) != null) {
– Linden Swan
Jan 2 at 1:11
add a comment |
because you are trying to make new instance of your pojo class(DailyReading) inside while loop
put below code out of the while loop block
DailyReading dailyReading = new DailyReading();
and remove code below inside the while loop
line = reader.readLine();
I have Removed the DailyReading dailyReading = new DailyReading(); from the While Loop. Still having this error. i have moved the line = reader.readLine(); to the While loop as other people have done on the internet. while ((line = reader.readLine()) != null) {
– Linden Swan
Jan 2 at 1:11
add a comment |
because you are trying to make new instance of your pojo class(DailyReading) inside while loop
put below code out of the while loop block
DailyReading dailyReading = new DailyReading();
and remove code below inside the while loop
line = reader.readLine();
because you are trying to make new instance of your pojo class(DailyReading) inside while loop
put below code out of the while loop block
DailyReading dailyReading = new DailyReading();
and remove code below inside the while loop
line = reader.readLine();
edited Jan 1 at 15:54
answered Jan 1 at 15:47
AmirAmir
161214
161214
I have Removed the DailyReading dailyReading = new DailyReading(); from the While Loop. Still having this error. i have moved the line = reader.readLine(); to the While loop as other people have done on the internet. while ((line = reader.readLine()) != null) {
– Linden Swan
Jan 2 at 1:11
add a comment |
I have Removed the DailyReading dailyReading = new DailyReading(); from the While Loop. Still having this error. i have moved the line = reader.readLine(); to the While loop as other people have done on the internet. while ((line = reader.readLine()) != null) {
– Linden Swan
Jan 2 at 1:11
I have Removed the DailyReading dailyReading = new DailyReading(); from the While Loop. Still having this error. i have moved the line = reader.readLine(); to the While loop as other people have done on the internet. while ((line = reader.readLine()) != null) {
– Linden Swan
Jan 2 at 1:11
I have Removed the DailyReading dailyReading = new DailyReading(); from the While Loop. Still having this error. i have moved the line = reader.readLine(); to the While loop as other people have done on the internet. while ((line = reader.readLine()) != null) {
– Linden Swan
Jan 2 at 1:11
add a comment |
OverLooking the Whole problem from the beginning found that i had Hit ENTER in the txt file causing it to read that as a line... i was not aware of this being possible. thanks for helping me out though. Another-Dave pointed me in the right direction
add a comment |
OverLooking the Whole problem from the beginning found that i had Hit ENTER in the txt file causing it to read that as a line... i was not aware of this being possible. thanks for helping me out though. Another-Dave pointed me in the right direction
add a comment |
OverLooking the Whole problem from the beginning found that i had Hit ENTER in the txt file causing it to read that as a line... i was not aware of this being possible. thanks for helping me out though. Another-Dave pointed me in the right direction
OverLooking the Whole problem from the beginning found that i had Hit ENTER in the txt file causing it to read that as a line... i was not aware of this being possible. thanks for helping me out though. Another-Dave pointed me in the right direction
answered Jan 2 at 4:06
Linden SwanLinden Swan
22
22
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994641%2fwhile-loop-reading-a-file-running-on-an-extra-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It seems to me that the structure of the file is not what you expect it to be - your comment about it "skips the second line for some reason" goes along with that. Use a debugger or add 'print' statements to find what's actually in
line
each time round the loop.– another-dave
Jan 1 at 13:41
i realised why it was skipping it was because i was calling line = reader.readLine(); twice so it would skip every seccond one but then it wouldnt error out that way.
– Linden Swan
Jan 2 at 1:04