2

so, i was trying to run my app and it should be show the notification after the first run. But then, it gave me an error like this:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at android.app.Notification$Builder.<init>(Notification.java:1951)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at android.support.v4.app.NotificationCompatApi21$Builder.<init>(NotificationCompatApi21.java:68)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at android.support.v4.app.NotificationCompat$NotificationCompatImplApi21.build(NotificationCompat.java:759)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:1559)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at apps.skripsi.petvac.navigationmenu.MainMenu.createButtonNotification(MainMenu.java:383)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at apps.skripsi.petvac.navigationmenu.MainMenu$DataBinatangOperation.onPostExecute(MainMenu.java:235)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at apps.skripsi.petvac.navigationmenu.MainMenu$DataBinatangOperation.onPostExecute(MainMenu.java:145)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at android.os.AsyncTask.finish(AsyncTask.java:632)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5253)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
12-10 12:01:03.512 3132-3132/apps.skripsi.petvac E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

and here's my code for showing the notification

public void createButtonNotification(Context context, String title, String content, String idb, String idv) {
    // Prepare intent which is triggered if the  notification button is pressed


    Intent intent = new Intent(getActivity(), MainPetInformation.class);
    intent.putExtra("IDBIN", idb);
    intent.putExtra("IDVAK", idv);


    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    int IntID = Integer.valueOf(idb);

    PendingIntent IntentAccept = PendingIntent.getActivity(getActivity(), IntID, intent, 0);

    PendingIntent IntentCancel = PendingIntent.getActivity(getActivity(), IntID, intent, 0);
    IntentCancel.cancel();


    // Building the notifcation
    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.logo) // notification icon
            .setContentTitle(title) // notification title
            .setContentText(content)
            .setSound(uri)// content text
            .addAction(R.drawable.accept, "Accept", IntentAccept) // accept notification button
            .addAction(R.drawable.cancel, "Cancel", IntentCancel); // cancel notification button


    mNotificationManager.notify(IntID, nBuilder.build());


}

the error stated it occured on this line:

mNotificationManager.notify(IntID, nBuilder.build());

please help:(

Devo Fauzan Rahman
  • 131
  • 1
  • 2
  • 10

1 Answers1

1

You should initialize the Notification Manager before you call this line.

mNotificationManager.notify(IntID, nBuilder.build());

Try this

mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
mNotificationManager.notify(IntID, nBuilder.build());

Happy Coding :)

Venkatesh Selvam
  • 1,382
  • 2
  • 15
  • 28
  • ummm...actually i use Fragment in this, i've tried getActivity() but still doesn't work – Devo Fauzan Rahman Dec 10 '15 at 05:28
  • R u get the value of int IntID = Integer.valueOf(idb); ? – Venkatesh Selvam Dec 10 '15 at 05:36
  • i've tried `mNotificationManager = (NotificationManager)context.getSystemService(getActivity().NOTIFICATION_SERVICE)` but i still got an error : `java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference` – Devo Fauzan Rahman Dec 10 '15 at 08:14