title: java.lang.NullPointerException: Attempt to invoke virtual method '.getPackageName()' on a null object reference
body: <p>I'm trying to get lists from a db and suddenly this error appear.</p>
  
  <p>Errors are from here:</p>
  
  <pre><code>TinyDB tinydb = new TinyDB(MyApp.getContext());
  
  SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);
  </code></pre>
  
  <p>Here's the MyApp's class</p>
  
  <pre><code>public class MyApp extends Application {
   private static MyApp instance;
  
   public static MyApp getInstance() {
      return instance;
   }
  
   public static Context getContext(){
      return instance;
      // or return instance.getApplicationContext();
   }
  
   @Override
   public void onCreate() {
      instance = this;
      super.onCreate();
   }
  }
  </code></pre>
  
  <p>I have no idea how to fix this right now.</p>
  
  <p>EDIT:</p>
  
  <blockquote>
    <p>Process: com.example.pangelyn, PID: 7750
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.pangelyn/com.example.pangelyn.SwipeLeft}:
    java.lang.NullPointerException: Attempt to invoke virtual method
    'java.lang.String android.content.Context.getPackageName()' on a null
    object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String
    android.content.Context.getPackageName()' on a null object reference
            at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
            at android.app.Activity.getLocalClassName(Activity.java:5854)
            at android.app.Activity.getPreferences(Activity.java:5897)
            at com.example.pangelyn.SwipeLeft.(SwipeLeft.java:28)
            at java.lang.Class.newInstance(Native Method)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
            at android.app.ActivityThread.-wrap11(Unknown Source:0) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
            at android.os.Handler.dispatchMessage(Handler.java:106) 
            at android.os.Looper.loop(Looper.java:164) 
            at android.app.ActivityThread.main(ActivityThread.java:6494) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)</p>
  </blockquote>
  
  <p>EDIT 2:</p>
  
  <pre><code>public class SwipeLeft extends AppCompatActivity {
  
  float x1,x2,y1,y2;
  
  List&lt;GroupModel&gt; lisSiswaModel = new ArrayList&lt;&gt;();
  
  TinyDB tinydb = new TinyDB(MyApp.getContext());
  
  SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_groups);
  
      RecyclerView recyclerView = findViewById(R.id.recyclerView_group);
  
      Editor prefsEditor = mPrefs.edit();
      Gson gson = new Gson();
      GroupModel groupModel1 = new GroupModel();
      groupModel1.setName("School");
      String json1 = gson.toJson(groupModel1);
      prefsEditor.putString("groups", json1);
      prefsEditor.commit();
  
      List&lt;String&gt; json2 = tinydb.getListString("groups");
  
      for (String string : json2) {
          GroupModel groupModel2 = gson.fromJson(string, GroupModel.class);
          lisSiswaModel.add(groupModel2);
      }
  
      recyclerView.setLayoutManager(new LinearLayoutManager(this));
      GroupAdapter adapter = new GroupAdapter(this, lisSiswaModel);
      recyclerView.setAdapter(adapter);
  }
  
  public boolean onTouchEvent(MotionEvent touchEvent){
      switch(touchEvent.getAction()){
          case MotionEvent.ACTION_DOWN:
              x1 = touchEvent.getX();
              y1 = touchEvent.getY();
              break;
          case MotionEvent.ACTION_UP:
              x2 = touchEvent.getX();
              y2 = touchEvent.getY();
              if(x1 &gt; x2 + 250){
                  Intent i = new Intent(SwipeLeft.this, MainActivity.class);
                  startActivity(i);
                  overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
              }
              break;
      }
      return false;
  }
  }
  </code></pre>