4

I am new to Android programming and I am just beginning to learn, which I am learning little by little by tinkering around, as well as using the knowledge I have had with Java.

I want to create a notification class to hold all notification methods to call from at any time I'd like to make a notification occur. For some reason, I am having an exception while the program is creating a NotificationManager object to make a notification channel. This only seems to happen when making the notification class a separate class.

MainActivity Class:

package com.example.colombian.textingclasses;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        notifications n = new notifications(this);
    }


}

notifications class:

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

import android.support.v7.app.AppCompatActivity;


public class notifications {

    private Context context;

    public final String CHANNEL_ID = "customChannel";

    public notifications(Context c) {
        createNotificationChannel();
        context = c;
    }

    //creates a notification channel
    private void createNotificationChannel() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "MyChannel";
            String description = "Description";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
/*******Error happens below*******/
            NotificationManager nm = (NotificationManager) context.getSystemService(NotificationManager.class); //error
            nm.createNotificationChannel(channel);
        }
    }

}

Exception:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.colombian.textingclasses, PID: 16471
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.colombian.textingclasses/com.example.colombian.textingclasses.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.Class)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6940)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.Class)' on a null object reference
                      at com.example.colombian.textingclasses.notifications.createNotificationChannel(notifications.java:32)
                      at com.example.colombian.textingclasses.notifications.<init>(notifications.java:19)
                      at com.example.colombian.textingclasses.MainActivity.onCreate(MainActivity.java:14)
                      at android.app.Activity.performCreate(Activity.java:7174)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6940) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

Strange thing is, when the createNotificationChannel() method is within the MainActivity class, it works perfectly fine. It even seems to work if I make the notification class an inner class of MainActivity. I just do not understand why there is an exception when I copy it to a separate class to create an object from.

Keep in mind, this based on API 26+ or Android 8.0.0

__

P.S. I hope my question is specific enough. This is my first time asking a question on here so please let me know if there is something that needs more clarification. Thanks.

Josh B
  • 43
  • 3

1 Answers1

1

You are trying to create the notification channel before context is initialized. Initialize context before using as follows:

 public notifications(Context c) {
    context = c;
    createNotificationChannel();       
 }
Sagar
  • 23,903
  • 4
  • 62
  • 62