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

Ziad Khan

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


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

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

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

        ContentValues.put(COL_2,name);
        ContentValues.put(COL_3,surname);
        ContentValues.put(COL_4,marks);

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

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

methodTest

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

methodTest2

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

methodTest3

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

Resolution

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.

Edit

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

ContentValues.put(COL_2,name);

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to resolve this error?:non-static method maxOf(int,int) cannot be referenced from a static context

From Dev

Calling a non static method from a in a static context

From Java

non static method cannot be referenced from a static context

From Dev

Non-static method cannot be reference from a static context

From Dev

non-static method getIntent() cannot be referenced from a static context

From Dev

Call non-static method from static context

From Java

accessing non-static method from static context

From Dev

Calling a static method from a non-static context

From Dev

Java non-static method cannot be refenced from a static context

From Dev

"Non-static method cannot be referenced from static context" error

From Dev

Non-static method getSocketFactory cannot be referenced from a static context

From Dev

Non-static method cannot be refered from a static context JAVAFX

From Java

How to achieve static method from non-static interface method?

From Java

How to print a non-static method from a static method?

From Java

How to access global non static variable from static method

From Dev

How to enter a non static method from a static one

From Dev

how to reffer non static variable from static method?

From Dev

Trying to get my show-method to work (Non static method cannot be referenced from static context)

From Dev

Map.merge .. non-static method cannot be referenced from static context

From Dev

non-static method matcher(CharSequence) cannot be referenced from a static context

From Java

Why does String::isEmpty works when non-static method cannot be referenced from a static context?

From Dev

Non-Static method from a static context [Comparing two sets of class instances]

From Java

java - Non-static method 'getLogger' cannot be referenced from a static context

From Dev

non-static method count(int) cannot be referenced from a static context

From Dev

Non-static method cannot be referenced from a static context when flattening map java 8

From Java

Non static field cannot be referenced from a static context- Main method

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Dev

Flutter: non-static method registerWith(Registrar) cannot be referenced from a static context

Related Related

  1. 1

    How to resolve this error?:non-static method maxOf(int,int) cannot be referenced from a static context

  2. 2

    Calling a non static method from a in a static context

  3. 3

    non static method cannot be referenced from a static context

  4. 4

    Non-static method cannot be reference from a static context

  5. 5

    non-static method getIntent() cannot be referenced from a static context

  6. 6

    Call non-static method from static context

  7. 7

    accessing non-static method from static context

  8. 8

    Calling a static method from a non-static context

  9. 9

    Java non-static method cannot be refenced from a static context

  10. 10

    "Non-static method cannot be referenced from static context" error

  11. 11

    Non-static method getSocketFactory cannot be referenced from a static context

  12. 12

    Non-static method cannot be refered from a static context JAVAFX

  13. 13

    How to achieve static method from non-static interface method?

  14. 14

    How to print a non-static method from a static method?

  15. 15

    How to access global non static variable from static method

  16. 16

    How to enter a non static method from a static one

  17. 17

    how to reffer non static variable from static method?

  18. 18

    Trying to get my show-method to work (Non static method cannot be referenced from static context)

  19. 19

    Map.merge .. non-static method cannot be referenced from static context

  20. 20

    non-static method matcher(CharSequence) cannot be referenced from a static context

  21. 21

    Why does String::isEmpty works when non-static method cannot be referenced from a static context?

  22. 22

    Non-Static method from a static context [Comparing two sets of class instances]

  23. 23

    java - Non-static method 'getLogger' cannot be referenced from a static context

  24. 24

    non-static method count(int) cannot be referenced from a static context

  25. 25

    Non-static method cannot be referenced from a static context when flattening map java 8

  26. 26

    Non static field cannot be referenced from a static context- Main method

  27. 27

    Non-static method cannot be referenced from a static context in java 8 streams

  28. 28

    Non-static method cannot be referenced from a static context in java 8 streams

  29. 29

    Flutter: non-static method registerWith(Registrar) cannot be referenced from a static context

HotTag

Archive