OutOfBoundsException while fetching data












0















Error



Code



        MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr

Cursor csr = myDBHlpr.getAllQuestions(getActivity());

mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));
Log.d("ZZZ", csr.getString(csr.getColumnIndex("MostMessagesSent")));


csr.close();


Just code for fetching SQLite data, but getting an out of bounds exception in the line. mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));



Null pointer



enter image description here



Database helper



public Cursor getAllQuestions(FragmentActivity usageSettings) {
return this.getWritableDatabase().query(TABLE_NAME,null,null,null,null,null,null);
}









share|improve this question




















  • 2





    Show output and/or possible error messages as properly formatted text in the question, not as image or by external link.

    – Michael Butscher
    Nov 12 '18 at 22:10
















0















Error



Code



        MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr

Cursor csr = myDBHlpr.getAllQuestions(getActivity());

mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));
Log.d("ZZZ", csr.getString(csr.getColumnIndex("MostMessagesSent")));


csr.close();


Just code for fetching SQLite data, but getting an out of bounds exception in the line. mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));



Null pointer



enter image description here



Database helper



public Cursor getAllQuestions(FragmentActivity usageSettings) {
return this.getWritableDatabase().query(TABLE_NAME,null,null,null,null,null,null);
}









share|improve this question




















  • 2





    Show output and/or possible error messages as properly formatted text in the question, not as image or by external link.

    – Michael Butscher
    Nov 12 '18 at 22:10














0












0








0








Error



Code



        MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr

Cursor csr = myDBHlpr.getAllQuestions(getActivity());

mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));
Log.d("ZZZ", csr.getString(csr.getColumnIndex("MostMessagesSent")));


csr.close();


Just code for fetching SQLite data, but getting an out of bounds exception in the line. mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));



Null pointer



enter image description here



Database helper



public Cursor getAllQuestions(FragmentActivity usageSettings) {
return this.getWritableDatabase().query(TABLE_NAME,null,null,null,null,null,null);
}









share|improve this question
















Error



Code



        MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr

Cursor csr = myDBHlpr.getAllQuestions(getActivity());

mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));
Log.d("ZZZ", csr.getString(csr.getColumnIndex("MostMessagesSent")));


csr.close();


Just code for fetching SQLite data, but getting an out of bounds exception in the line. mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));



Null pointer



enter image description here



Database helper



public Cursor getAllQuestions(FragmentActivity usageSettings) {
return this.getWritableDatabase().query(TABLE_NAME,null,null,null,null,null,null);
}






java android database sqlite android-studio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:27

























asked Nov 12 '18 at 22:01







user10643013















  • 2





    Show output and/or possible error messages as properly formatted text in the question, not as image or by external link.

    – Michael Butscher
    Nov 12 '18 at 22:10














  • 2





    Show output and/or possible error messages as properly formatted text in the question, not as image or by external link.

    – Michael Butscher
    Nov 12 '18 at 22:10








2




2





Show output and/or possible error messages as properly formatted text in the question, not as image or by external link.

– Michael Butscher
Nov 12 '18 at 22:10





Show output and/or possible error messages as properly formatted text in the question, not as image or by external link.

– Michael Butscher
Nov 12 '18 at 22:10












1 Answer
1






active

oldest

votes


















0














You are not moving to an actual row in the Cursor. Therefore when you try to get data you are before the first row and hence at the impossible to use offset of -1.



When a Cursor is returned by an SQLiteDatabase method, the Cursor is positioned at a position that is before the first row. This position is -1.




  • You can set a Cursor to before the first row using csr.moveToPosition(-1) if need be.


Assuming you want to loop through all rows then the following progressively moves to each row in turn until there are no more rows (generally Cursor moveTo???? methods return true if the move could be made otherwise they return false) (which is why the while loop works for all rows.)



    Cursor csr = myDBHlpr.getAllQuestions(getActivity());
while (csr.moveToNext()) {
mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));
}
csr.close();


However, that would then only show the very last row in the TextView. If this is an issue and you cannot resolve the issue, you should ask another question.




  • Note if the Cursor had no rows, then nothing would be done and the TextView would remain as it was, often you'd want to signify that no data was extracted. -


    • For example, applying this to your code you could have mMostMessagesSent.setText("No Data Extracted") before the while loop and therefore the TextView would indicate that no data was extracted, if no data was extracted.








share|improve this answer


























  • gettng a nullpointer exception

    – user10643013
    Nov 13 '18 at 17:50











  • added a log.d line to check and it showed null... anyreason why?

    – user10643013
    Nov 13 '18 at 17:50











  • the textview is showing blank so i put the log.D code

    – user10643013
    Nov 13 '18 at 18:02











  • At a guess the null pointer would be that you haven't instantiated/set mMostMessagesSent (via an appropriate use of findViewById). That's assuming that you didn't remove the line MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr (if you removed the line add it back in)

    – MikeT
    Nov 13 '18 at 19:14













  • The data retrieved is null... But the table exists with 16rows... Count of the rows are 16

    – user10643013
    Nov 15 '18 at 5:27











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%2f53270739%2foutofboundsexception-while-fetching-data%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You are not moving to an actual row in the Cursor. Therefore when you try to get data you are before the first row and hence at the impossible to use offset of -1.



When a Cursor is returned by an SQLiteDatabase method, the Cursor is positioned at a position that is before the first row. This position is -1.




  • You can set a Cursor to before the first row using csr.moveToPosition(-1) if need be.


Assuming you want to loop through all rows then the following progressively moves to each row in turn until there are no more rows (generally Cursor moveTo???? methods return true if the move could be made otherwise they return false) (which is why the while loop works for all rows.)



    Cursor csr = myDBHlpr.getAllQuestions(getActivity());
while (csr.moveToNext()) {
mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));
}
csr.close();


However, that would then only show the very last row in the TextView. If this is an issue and you cannot resolve the issue, you should ask another question.




  • Note if the Cursor had no rows, then nothing would be done and the TextView would remain as it was, often you'd want to signify that no data was extracted. -


    • For example, applying this to your code you could have mMostMessagesSent.setText("No Data Extracted") before the while loop and therefore the TextView would indicate that no data was extracted, if no data was extracted.








share|improve this answer


























  • gettng a nullpointer exception

    – user10643013
    Nov 13 '18 at 17:50











  • added a log.d line to check and it showed null... anyreason why?

    – user10643013
    Nov 13 '18 at 17:50











  • the textview is showing blank so i put the log.D code

    – user10643013
    Nov 13 '18 at 18:02











  • At a guess the null pointer would be that you haven't instantiated/set mMostMessagesSent (via an appropriate use of findViewById). That's assuming that you didn't remove the line MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr (if you removed the line add it back in)

    – MikeT
    Nov 13 '18 at 19:14













  • The data retrieved is null... But the table exists with 16rows... Count of the rows are 16

    – user10643013
    Nov 15 '18 at 5:27
















0














You are not moving to an actual row in the Cursor. Therefore when you try to get data you are before the first row and hence at the impossible to use offset of -1.



When a Cursor is returned by an SQLiteDatabase method, the Cursor is positioned at a position that is before the first row. This position is -1.




  • You can set a Cursor to before the first row using csr.moveToPosition(-1) if need be.


Assuming you want to loop through all rows then the following progressively moves to each row in turn until there are no more rows (generally Cursor moveTo???? methods return true if the move could be made otherwise they return false) (which is why the while loop works for all rows.)



    Cursor csr = myDBHlpr.getAllQuestions(getActivity());
while (csr.moveToNext()) {
mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));
}
csr.close();


However, that would then only show the very last row in the TextView. If this is an issue and you cannot resolve the issue, you should ask another question.




  • Note if the Cursor had no rows, then nothing would be done and the TextView would remain as it was, often you'd want to signify that no data was extracted. -


    • For example, applying this to your code you could have mMostMessagesSent.setText("No Data Extracted") before the while loop and therefore the TextView would indicate that no data was extracted, if no data was extracted.








share|improve this answer


























  • gettng a nullpointer exception

    – user10643013
    Nov 13 '18 at 17:50











  • added a log.d line to check and it showed null... anyreason why?

    – user10643013
    Nov 13 '18 at 17:50











  • the textview is showing blank so i put the log.D code

    – user10643013
    Nov 13 '18 at 18:02











  • At a guess the null pointer would be that you haven't instantiated/set mMostMessagesSent (via an appropriate use of findViewById). That's assuming that you didn't remove the line MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr (if you removed the line add it back in)

    – MikeT
    Nov 13 '18 at 19:14













  • The data retrieved is null... But the table exists with 16rows... Count of the rows are 16

    – user10643013
    Nov 15 '18 at 5:27














0












0








0







You are not moving to an actual row in the Cursor. Therefore when you try to get data you are before the first row and hence at the impossible to use offset of -1.



When a Cursor is returned by an SQLiteDatabase method, the Cursor is positioned at a position that is before the first row. This position is -1.




  • You can set a Cursor to before the first row using csr.moveToPosition(-1) if need be.


Assuming you want to loop through all rows then the following progressively moves to each row in turn until there are no more rows (generally Cursor moveTo???? methods return true if the move could be made otherwise they return false) (which is why the while loop works for all rows.)



    Cursor csr = myDBHlpr.getAllQuestions(getActivity());
while (csr.moveToNext()) {
mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));
}
csr.close();


However, that would then only show the very last row in the TextView. If this is an issue and you cannot resolve the issue, you should ask another question.




  • Note if the Cursor had no rows, then nothing would be done and the TextView would remain as it was, often you'd want to signify that no data was extracted. -


    • For example, applying this to your code you could have mMostMessagesSent.setText("No Data Extracted") before the while loop and therefore the TextView would indicate that no data was extracted, if no data was extracted.








share|improve this answer















You are not moving to an actual row in the Cursor. Therefore when you try to get data you are before the first row and hence at the impossible to use offset of -1.



When a Cursor is returned by an SQLiteDatabase method, the Cursor is positioned at a position that is before the first row. This position is -1.




  • You can set a Cursor to before the first row using csr.moveToPosition(-1) if need be.


Assuming you want to loop through all rows then the following progressively moves to each row in turn until there are no more rows (generally Cursor moveTo???? methods return true if the move could be made otherwise they return false) (which is why the while loop works for all rows.)



    Cursor csr = myDBHlpr.getAllQuestions(getActivity());
while (csr.moveToNext()) {
mMostMessagesSent.setText(csr.getString(csr.getColumnIndex("MostMessagesSent")));
}
csr.close();


However, that would then only show the very last row in the TextView. If this is an issue and you cannot resolve the issue, you should ask another question.




  • Note if the Cursor had no rows, then nothing would be done and the TextView would remain as it was, often you'd want to signify that no data was extracted. -


    • For example, applying this to your code you could have mMostMessagesSent.setText("No Data Extracted") before the while loop and therefore the TextView would indicate that no data was extracted, if no data was extracted.









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 2:02

























answered Nov 13 '18 at 1:49









MikeTMikeT

15.8k112642




15.8k112642













  • gettng a nullpointer exception

    – user10643013
    Nov 13 '18 at 17:50











  • added a log.d line to check and it showed null... anyreason why?

    – user10643013
    Nov 13 '18 at 17:50











  • the textview is showing blank so i put the log.D code

    – user10643013
    Nov 13 '18 at 18:02











  • At a guess the null pointer would be that you haven't instantiated/set mMostMessagesSent (via an appropriate use of findViewById). That's assuming that you didn't remove the line MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr (if you removed the line add it back in)

    – MikeT
    Nov 13 '18 at 19:14













  • The data retrieved is null... But the table exists with 16rows... Count of the rows are 16

    – user10643013
    Nov 15 '18 at 5:27



















  • gettng a nullpointer exception

    – user10643013
    Nov 13 '18 at 17:50











  • added a log.d line to check and it showed null... anyreason why?

    – user10643013
    Nov 13 '18 at 17:50











  • the textview is showing blank so i put the log.D code

    – user10643013
    Nov 13 '18 at 18:02











  • At a guess the null pointer would be that you haven't instantiated/set mMostMessagesSent (via an appropriate use of findViewById). That's assuming that you didn't remove the line MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr (if you removed the line add it back in)

    – MikeT
    Nov 13 '18 at 19:14













  • The data retrieved is null... But the table exists with 16rows... Count of the rows are 16

    – user10643013
    Nov 15 '18 at 5:27

















gettng a nullpointer exception

– user10643013
Nov 13 '18 at 17:50





gettng a nullpointer exception

– user10643013
Nov 13 '18 at 17:50













added a log.d line to check and it showed null... anyreason why?

– user10643013
Nov 13 '18 at 17:50





added a log.d line to check and it showed null... anyreason why?

– user10643013
Nov 13 '18 at 17:50













the textview is showing blank so i put the log.D code

– user10643013
Nov 13 '18 at 18:02





the textview is showing blank so i put the log.D code

– user10643013
Nov 13 '18 at 18:02













At a guess the null pointer would be that you haven't instantiated/set mMostMessagesSent (via an appropriate use of findViewById). That's assuming that you didn't remove the line MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr (if you removed the line add it back in)

– MikeT
Nov 13 '18 at 19:14







At a guess the null pointer would be that you haven't instantiated/set mMostMessagesSent (via an appropriate use of findViewById). That's assuming that you didn't remove the line MainData myDBHlpr = new MainData(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr (if you removed the line add it back in)

– MikeT
Nov 13 '18 at 19:14















The data retrieved is null... But the table exists with 16rows... Count of the rows are 16

– user10643013
Nov 15 '18 at 5:27





The data retrieved is null... But the table exists with 16rows... Count of the rows are 16

– user10643013
Nov 15 '18 at 5:27


















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%2f53270739%2foutofboundsexception-while-fetching-data%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

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."