0

This is my code for database class (DatabaseHelper.java)

public Cursor getAllData(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
        return res;
    }

This is another class which is (MainSuggestion.java)

public class MenuSuggestion extends AppCompatActivity {
    DatabaseHelper myDb;
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_suggestion);
<br/>
        Cursor res = myDb.getAllData();
        if (res.moveToFirst()) {             
            TextView bmi = (TextView) findViewById(R.id.textView9);
            TextView kcal = (TextView) findViewById(R.id.textView10);
            bmi.setText(res.getString(3));
            kcal.setText(res.getString(4));
        }
    }
}

Can someone tell me, what's wrong with my code?

MoLab
  • 15
  • 7
  • Did you check that you actually have records in your cursor? – SuperFrog Oct 15 '15 at 14:41
  • are you seeing any error message? post it here. Also do db calls on background threads the simplest one is to use asynctasks. – dsharew Oct 15 '15 at 14:41
  • There is no error message, but when I run it using emulator, the emulator shows "Unfortunately, MnutriHealth has stopped". – MoLab Oct 15 '15 at 14:43
  • Then you need to post the stacktrace, else we don't have any way to help you. – Moonbloom Oct 15 '15 at 14:43
  • where is myDb initated? – nano_nano Oct 15 '15 at 14:59
  • The recursion and the myDb problems are it. You have to load a DB and you're recursively Overriding the `super` function you're calling. – David Pulse Oct 15 '15 at 15:09
  • 1
    there is no recursion :) – nano_nano Oct 15 '15 at 15:12
  • @stefanbeike did you run it? Overriding is check at all times. Compile, Run, etc. If you use the super class function name, with the Override as your annotation, it will cause recursion in the function where it is used. He should just rename the function and take out the recursion – David Pulse Oct 15 '15 at 15:23
  • Annotation is for the most part a Deprecation function. Thus, he deprecated he super function. Thereby delimiting in place of it, his function. You can read all anout them at (https://docs.oracle.com/javase/tutorial/java/annotations/index.html) – David Pulse Oct 15 '15 at 15:27
  • @David Pulse no believe us!!! it is no recursion.... I use that in all of my activeties too – nano_nano Oct 15 '15 at 17:12
  • Well, this is a future proof site. Tell why the super function is not deprecated when calling for the annotation of @Override. – David Pulse Oct 15 '15 at 17:13

2 Answers2

4

The exact reason this isn't working is because "myDb" is null on this line.

 Cursor res = myDb.getAllData();

However, I recommend you Running a Query with a CursorLoader. It might seem overkill, loading stuff from the database on the main thread like you are can lead to ANR errors.

JoeyJubb
  • 2,341
  • 1
  • 12
  • 19
0

Query data from database, you have to use:

this.getReadableDatabase();

You use getWritableDatabase() when you want to write, update, delete data into/from database.

Lay Leangsros
  • 9,156
  • 7
  • 34
  • 39
  • 1
    This is good as a recommendation but it is still possible to read from writable databse, so this could not be the cause for the error. – dsharew Oct 15 '15 at 14:47
  • @MoLab why dont you provide what you are asked? post the error if you want a solution. – dsharew Oct 15 '15 at 14:51
  • Is it the main activity? if not, did you add this class in Manifest ? – Lay Leangsros Oct 15 '15 at 14:52
  • @DegenSharew There is no error but when I run it using emulator, the emulator shows "Unfortunately, MnutriHealth has stopped". – MoLab Oct 15 '15 at 14:55
  • @MoLab check the log cat there is always an error logged there when app crashes this way. – dsharew Oct 15 '15 at 14:58
  • @MoLab, So try to uninstall old app on your emulator, and build and run a new one, it may cause from updating database structure. I guest, . * You should better give the error in Log cat – Lay Leangsros Oct 15 '15 at 14:58