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

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Comments

1

Change the method updateScores to static as below:

 public static void updateScores(List<Score> score)

Comments

1
DatabaseHandler dbh = new DatabaseHandler();
dbh.updateScores(listScore);

2 Comments

Did this and get "The constructor DatabaseHandler() is undefined"
You updated your code after my post. So you have parameterized constructor in your DatabaseHandler. So better make updateScores as static method
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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.