-3

I'm a beginner when it comes to Android app development. I have finished some Udemy courses, but then I realized that the Android Docs might actually be better for building a good foundation on app development. I'm trying to build my own app now but I encountered this problem.

My Class

public class deleteInfluencer extends AppCompatActivity {

private Repository instanceOfRepository;

public static final int EDIT_BLOGGER_ACTIVITY_REQUEST_CODE = 1;

deleteInfluencer(Application application) {
    super();
    instanceOfRepository = new Repository(application);
}

void callDeleteInfluencer(int id) {
    instanceOfRepository.deleteInfluencerCalledFromRepository(id);
}

void callUpdateInfluencer() {
    Intent intent = new Intent(this, EditBloggerActivity.class);
    startActivityForResult(intent, EDIT_BLOGGER_ACTIVITY_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == EDIT_BLOGGER_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
        int id = 0;
        Tables tables = new Tables(
                id, // Blogger ID
                data.getStringExtra(EditBloggerActivity.EXTRA_EDIT_REPLY_NAME),
                data.getStringExtra(EditBloggerActivity.EXTRA_EDIT_REPLY_ADDRESS),
                data.getStringExtra(EditBloggerActivity.EXTRA_EDIT_REPLY_MOBILE_NUMBER),
                data.getStringExtra(EditBloggerActivity.EXTRA_EDIT_REPLY_FB),
                data.getStringExtra(EditBloggerActivity.EXTRA_EDIT_REPLY_IG),
                data.getStringExtra(EditBloggerActivity.EXTRA_EDIT_REPLY_EMAIL));
        instanceOfRepository.updateInfluencerCalledFromRepository(tables);
    } else {
        Toast.makeText(this, "Save Failed! fill out all fields", Toast.LENGTH_LONG).show();

    }
}
}

LOGCAT

2020-02-28 16:10:06.071 4258-4258/com.kurtsample.digitalcirclesprototype E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kurtsample.digitalcirclesprototype, PID: 4258
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:142)
    at android.content.ComponentName.<init>(ComponentName.java:130)
    at android.content.Intent.<init>(Intent.java:6082)
    at com.kurtsample.digitalcirclesprototype.deleteInfluencer.callUpdateInfluencer(deleteInfluencer.java:31)
    at com.kurtsample.digitalcirclesprototype.Adapter$2.onClick(Adapter.java:95)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    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:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • check this: https://stackoverflow.com/questions/28515049/android-content-context-getpackagename-on-a-null-object-reference – Rafael Feb 28 '20 at 08:33
  • I didn't intend for it to be an Activity. I wanted it to be a class that houses methods I need to update data in my database. – Kurt Chua Feb 28 '20 at 09:14

1 Answers1

0

Replace this in Intent intent = new Intent(this, EditBloggerActivity.class); with deleteInfluencer.this. So you will have Intent intent = new Intent(deleteInfluencer.this, EditBloggerActivity.class);

Also, a few things for you to note:

  1. Activity classes should not have constructors. If you want something to act like a constructor, you can use the life-cycle methods.
  2. Your class names should begin with a capital letter and have the word 'Activity', thus deleteInfluencer should beDeleteInfluencerActivity
  • I actually intended this class to only house the methods I need to be able to update data in my database. I never intended it to be a full on Activity. If this class is already considered an activity is there any way to change that into what I intended it to be? – Kurt Chua Feb 28 '20 at 09:10