While loop reading a file running on an extra time












0















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.










share|improve this question

























  • 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
















0















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.










share|improve this question

























  • 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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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

















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












2 Answers
2






active

oldest

votes


















0














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();





share|improve this answer


























  • 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





















-1














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






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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();





    share|improve this answer


























    • 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


















    0














    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();





    share|improve this answer


























    • 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
















    0












    0








    0







    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();





    share|improve this answer















    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();






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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





















    • 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















    -1














    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






    share|improve this answer




























      -1














      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






      share|improve this answer


























        -1












        -1








        -1







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 4:06









        Linden SwanLinden Swan

        22




        22






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas