1

I am getting the following error from my code

'Cannot make a static reference to the non-static method getReadableDatabase() from the type SQLiteOpenHelper'

in the Database.java file. If I use Eclipse to solve the error then an error happens in the FavouriteScreen.java file. Anyone know how to solve it?

FavouritesScreen.java

// Get a Cursor for the list items
Cursor listCursor = Database.GetFavouritesList();
startManagingCursor(listCursor);

// set the custom list adapter
setListAdapter(new MyListAdapter(this, listCursor));

and

Database.java

public static Cursor GetFavouritesList(){
try
{
return(getReadableDatabase().rawQuery("SELECT SocietyName FROM Favourites",null));
}
catch(SQLiteException e)
{
Log.e("Favourites", e.toString());
}
return null;
}
Bastien Jansen
  • 8,756
  • 2
  • 35
  • 53
Ryaller
  • 77
  • 3
  • 12
  • static `Cursor`, uhm, for what jesus god? I suggest you rather use static method for getting instance of dbhelper(singleton design pattern) than `Cursor`. This sounds like not correct designated app logic. – Simon Dorociak Apr 09 '13 at 12:09

3 Answers3

2

The error is that you are doing something not allowed! Solutions:

  1. Put all the code for getReadableDatabase() in the function being sure not to call non-static functions.

  2. Make getReadableDatabase() static.

  3. Make getReadableDatabase() non-static and change how you call it:

    Database database = new Database();
    Cursor listCursor = database.GetFavouritesList();
    
Neil Townsend
  • 6,024
  • 5
  • 35
  • 52
0

The problem is, that you are trying to use a non-static method within your static method. The getReadableDatabase() should also be static, for this should work.

ArchiFloyd
  • 1,274
  • 13
  • 20
0

getReadableDatabase() is a instance method, not a class method. You need an instance, e.g:

public static Cursor GetFavouritesList(SQLiteOpenHelper helper){
try
{
return(helper.getReadableDatabase().rawQuery("SELECT SocietyName FROM Favourites",null));
}
catch(SQLiteException e)
{
Log.e("Favourites", e.toString());
}
return null;
}
asgoth
  • 35,552
  • 12
  • 89
  • 98