Ziad Khan Ziad Khan - 2 months ago 13
Android Question

How to resolve non-static method errors from static context?

I am getting this, actually its running in an old version but not running in Android 2.2 this is the code... I don't what to replace it with or have an alternative.
So i have added the whole code to be able to understand the real. the problem i have seen it run on an older version of android studio.

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String Database_Name= "student.db";
public static final String Table_Name= "student_table";
public static final String COL_1= "id";
public static final String COL_2= "name";
public static final String COL_3= "surname";
public static final String COL_4= "marks";

public DatabaseHelper(Context context){
super(context, Database_Name,null,1);

public void onCreate(SQLiteDatabase db) {
db.execSQL("create table" + Table_Name + " (id integer primary key auto increment, name text, surname text, marks ineteger)" );

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists" + Table_Name);

public boolean insertData(String name, String surname, String marks){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();


long result = db.insert(Table_Name, null,contentValues);
if (result==-1) {
return false;
} else {
return true;


This error happens because you are mixing up static and non-static parts of your class.

Take this example:

public class Test {
    public String testVariable = "Honk";

    public static String methodTest() {
        return testVariable;

    public static String methodTest2() {
        Test test = new Test();
        return test.testVariable;

    public String methodTest3() {
        return testVariable;


This test will fail. You are attempting to get a non-static variable testVariable from a static context methodTest().

Accessed with:

String result = Test.methodTest();


This will work. You are making a new Test class instance, and you can access the variable testVariable normally.

Accessed with:

String result = Test.methodTest2();


This will work. You are accessing a non-static variable testVariable from a non-static context methodTest3().

Accessed with:

Test test = new Test(); 
String result = test.methodTest();


Without seeing the rest of your code, it is hard to give you a suggestion for what to change. The code you supplied in your question is unlikely to be the culprit.


Your issue is that you are accessing the ContentValues class statically...


You need to use the variable that you created: contentValues.