0

I made nonActivity class, and I use there other nonActivity Database class. When I call method from 1st class I have Error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

Here is a fragment of my code of both classes:

DataBase.java

 public DataBase(Context context) {
        super(context, DATABASE_NAME, null, TABLE_VERSION);
   }

and ObliczPodatki.java

public class ObliczPodatki extends Application
.
.
.
myDb = new DataBase(this.getApplicationContext());

and the full error message:

Process: com.example.nadgraniav01, PID: 11274
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:116)
        at com.example.nadgraniav01.ObliczPodatki.obliczPodatki(ObliczPodatki.java:43)
        at com.example.nadgraniav01.DodajNadgranieActivity$2.onClick(DodajNadgranieActivity.java:99)
        at android.view.View.performClick(View.java:6669)
        at android.view.View.performClickInternal(View.java:6638)
        at android.view.View.access$3100(View.java:789)
        at android.view.View$PerformClick.run(View.java:26145)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6863)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

if I use this instead of this.getApplicationContext() I am getting following erroe

Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference 
  • what if you use `this` instead of `this.getApplicationContext()` ? – Payam Asefi Jun 16 '19 at 17:40
  • `Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference` - the same error –  Jun 16 '19 at 17:41
  • Guess: you're trying to instantiate an activity yourself with `new ObliczPodatki()` - that won't work – laalto Jun 16 '19 at 17:44
  • @laalto I do it in Activity –  Jun 16 '19 at 17:46
  • I did in MainActivity in onCreate section `ObliczPodatki obliczPodatki = new ObliczPodatki();` –  Jun 16 '19 at 17:50

4 Answers4

0

Try this then:

 @Override 
 public void onCreate() {
  super.onCreate() 
  SQLiteHandler db = new SQLiteHandler(getApplicationContext());
  ....rest of db calls..
}
Payam Asefi
  • 2,677
  • 2
  • 15
  • 26
0

Use:

 myDb = new DataBase(getApplicationContext());

Or,

 myDb = new DataBase(Activityname.this);
0

getApplicationContext is not the context that you should use,

Use this instead

myDb = new DataBase(this);
Tashun Sandaru
  • 119
  • 1
  • 9
0

You have to initialise myDb variable in onCreate() method of ObliczPodatki class.

   myDb = new DataBase(getApplicationContext());
DHAVAL A.
  • 2,251
  • 2
  • 12
  • 27