1

I'm writing an Android app and am getting this error, but I'm not sure why. Can someone help me understand why I'm getting this error?

Cannot make a static reference to the non-static method updateScores(List<Score>) from the type DatabaseHandler

Here's the relevant code.

public class ScoreList extends SherlockFragmentActivity {
    List<Score> listScore = new ArrayList<Score>();
    public void updateListView() {
        listViewScore.setAdapter(new ScoreListAdapter(ctx,
                R.layout.score_row_item, listScore));
        DatabaseHandler.updateScores(listScore);    
    }
}

Here's the DatabaseHandler class. I tried making the function static, but it won't work that way due to errors.

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "scoreKeeper";
    private static final String TABLE_GAMES = "games";    
    private static final String KEY_NAME = "name";
    private static final String KEY_CHANGE = "scoreChange";
    private static final String KEY_TOTAL = "scoreTotal";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_GAMES_TABLE = "CREATE TABLE " + TABLE_GAMES + "("
                + KEY_NAME + " INTEGER PRIMARY KEY," + KEY_CHANGE + " TEXT,"
                + KEY_TOTAL + " TEXT" + ")";
        db.execSQL(CREATE_GAMES_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_GAMES);
        onCreate(db);
    }

    public void addScore(Score score) {
        SQLiteDatabase db = this.getWritableDatabase();    
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, score.getName()); 
        values.put(KEY_CHANGE, score.getScoreChange());
        values.put(KEY_TOTAL, score.getScoreTotal());       

        // Inserting Row
        db.insert(TABLE_GAMES, null, values);
        db.close(); 
    }

    public List<Score> getAllScores() {
        List<Score> scoreList = new ArrayList<Score>();
        String selectQuery = "SELECT  * FROM " + TABLE_GAMES;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Score score = new Score("","","");
                score.setName(cursor.getString(0));
                score.setScoreChange(cursor.getString(1));
                score.setScoreTotal(cursor.getString(2));
                // Adding contact to list
                scoreList.add(score);
            } while (cursor.moveToNext());
        }

        return scoreList;       
    }

    public void updateScores(List<Score> score) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_GAMES);
        onCreate(db);

        ContentValues values = new ContentValues();
        for(int i = 0; i < score.size(); i++){
            values.put(KEY_NAME, score.get(i).getName());
            values.put(KEY_CHANGE, score.get(i).getScoreChange());     
            values.put(KEY_TOTAL, score.get(i).getScoreTotal());    
        }       
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
GrilledCheese
  • 311
  • 3
  • 8
  • 13
  • possible duplicate of [Cannot Make Static Reference to Non-Static Method](http://stackoverflow.com/questions/4969171/cannot-make-static-reference-to-non-static-method) – JasonMArcher Nov 16 '14 at 06:23

7 Answers7

2
public void updateScores(List<Score> score)

change to

public static void updateScores(List<Score> score) 

Why?

Because you are using static call

DatabaseHandler.updateScores(listScore)
Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45
2

Because you are accessing the method with static reference

DatabaseHandler.updateScores(listScore);   

means with class name..

You have to make a instance of DatabaseHandler class and use that method.

Or make a updateScores as static method like,

static public void updateScores()

But I have a doubt you have more codes in DatabaseHandler Class. (As its sqlite database helper class you are using Activity context in this class) so better to make instance of DatabaseHandler class and use method updateScores() with instance.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Can you give me an example of the first one? I had already tried making the method static and as you guessed, I have other code that stops working when I do it. – GrilledCheese Feb 07 '13 at 06:31
  • Ok, In your application activity 's on `create()` write `DatabaseHandler dbHelper = new DatabaseHandler(this);` And in `updateListView()` just write `dbHelper.updateScores(listScore);` Make sure `DatabaseHandler dbHelper` is a global variable in `ScoreList`. – user370305 Feb 07 '13 at 06:50
1

Change the method updateScores to static as below:

 public static void updateScores(List<Score> score)
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
1
DatabaseHandler dbh = new DatabaseHandler();
dbh.updateScores(listScore);
Xavier DSouza
  • 2,861
  • 7
  • 29
  • 40
1

You can either make the Method

public void updateScores(List score)

as static or you can make an object of **DatabaseHandler ** in the caller class:

ScoreList

Milind Jindal
  • 176
  • 1
  • 7
1

Error message tell you exectly problem: you reference non-static method in static manner. There are two possibilities: 1. You want have static method (see other answers) 2. You want have non-static method. Then use this caller snippet:

DatabaseHandler helper = new DatabaseHandler();
helper.updateScores(listScore);
xvorsx
  • 2,322
  • 2
  • 18
  • 19
1

Error is because of you are calling the non static method using class name .to overcome this error you have to do the any one of the following things 1 . make your method updateScores as static or 2 . create an instance of DatabaseHandler and call using that object call the method.

calling method using class name requires method to be a static

Manohar Bomma
  • 311
  • 1
  • 3
  • 17