0

I'm trying to get lists from a db and suddenly this error appear.

Errors are from here:

TinyDB tinydb = new TinyDB(MyApp.getContext());

SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);

Here's the MyApp's class

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

I have no idea how to fix this right now.

EDIT:

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)

EDIT 2:

public class SwipeLeft extends AppCompatActivity {

float x1,x2,y1,y2;

List<GroupModel> lisSiswaModel = new ArrayList<>();

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<String> 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 > 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;
}
}
lugiorgi
  • 473
  • 1
  • 4
  • 12
Nodoodle
  • 57
  • 2
  • 8

2 Answers2

1

The fix is to move this inside the onCreate of the activity(as it says, context is null):

public class SwipeLeft extends AppCompatActivity {
    float x1,x2,y1,y2;

    List<GroupModel> lisSiswaModel = new ArrayList<>();

    TinyDB tinydb = new TinyDB(MyApp.getContext());

    SharedPreferences mPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_groups);
        mPrefs = getPreferences(MODE_PRIVATE);
        ...more code
    }
    ...more code
}
Andrei T
  • 2,985
  • 3
  • 21
  • 28
  • Hmm I don't think that worked. It still throw the same exception. – Nodoodle Feb 10 '20 at 15:46
  • 1
    Well, I tested it in both cases and that works. Not sure if you have other places where you do the same. I am telling you from the perspective of the SharePrefs. – Andrei T Feb 10 '20 at 15:47
  • 1
    Just try to remove all the code after the mPrefs line and see if it works. If it works - in my case works without issues - you can build it from there and see if it crashes in other places. – Andrei T Feb 10 '20 at 15:50
  • I removed everything after the mPrefs line and it still crashes. Pretty sure the problem is the null from the MyApp.getContext(). Because when I removed that tinydb line it doesn't crash anymore. – Nodoodle Feb 10 '20 at 15:54
  • 1
    I do not know what you do in the TinyDb as I do not see the code, but getContext does not crash, as the app is initialized first so context is not null. Now, if what you say it is true, and it was crashing in TinyDb, then your problems were multiple. However, MyApp.getContext was not one of them. If you want to test, put back your code with mPrefs. – Andrei T Feb 10 '20 at 15:56
  • 1
    You can put an assert like that in onCreate: private Context appContext = MyApp.getContext(); and then in onCreate you can just check assert appContext!=null; – Andrei T Feb 10 '20 at 16:00
  • https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo/blob/master/TinyDB.java this is the TinyDB actually. And yeah, I've checked the context, it really isn't null. – Nodoodle Feb 10 '20 at 16:37
0

I guess is crashing because of this TinyDB tinydb = new TinyDB(MyApp.getContext()); initialize your objects inside of your onCreate()?

public class SwipeLeft extends AppCompatActivity {

float x1,x2,y1,y2;

List<GroupModel> lisSiswaModel = new ArrayList<>();

TinyDB tinydb;

SharedPreferences mPrefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_groups);
    tinydb = new TinyDB(MyApp.getContext());
    mPrefs = getPreferences(MODE_PRIVATE);

Also I'd change your MyApp class to this one :

public class MyApp extends Application {

    private static MyApp mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
    }

    public static MyApp getContext() {
        return mContext;
    }
}

Do not put the mContext = instance before super.onCreate()

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148