flutter_local_notifications icon indicating copy to clipboard operation
flutter_local_notifications copied to clipboard

FlutterLocalNotificationsPlugin.scheduleNotification java.lang.NullPointerException - Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference

Open Tom3652 opened this issue 6 months ago • 8 comments

Describe the bug

The app is crashing in release mode, i have never encountered this issue in debug (my app is live on the store).

Here is the full trace.txt.

To Reproduce I am calling :

  Future<void> scheduleNotification(int createdAt, String folderName,
      {String? title, String? content}) async {
    bool? accepted = await _flutterNotificationManager
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.requestNotificationsPermission();
    if (accepted != null && accepted) {
      accepted = await _flutterNotificationManager
          .resolvePlatformSpecificImplementation<
              AndroidFlutterLocalNotificationsPlugin>()
          ?.requestExactAlarmsPermission();
      if (accepted != null && accepted) {
        await _flutterNotificationManager.periodicallyShow(
           createdAt,
            title ?? "Rappel",
            content ?? "Le dossier $folderName n'est toujours pas envoyé",
            RepeatInterval.daily,
            _notificationDetails(),
            androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle);
        debugPrint("Periodically showing notification");
      }
    }
  }

Expected behavior The app should not crash because of null pointer in Java

Sample code to reproduce the problem

Tom3652 avatar Jun 27 '25 08:06 Tom3652

I get the same exception on Android too

'''bash

PlatformException: PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setupNotificationChannel(SourceFile:24) at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(SourceFile:19) at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(SourceFile:1) at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(SourceFile:13) at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(SourceFile:397) at a3.g.o(SourceFile:21) at D.u.run(SourceFile:155) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7996) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:553) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) ) File "flutter_local_notifications_plugin.dart", line 223, in FlutterLocalNotificationsPlugin.show File "requirement.page.dart", line 44, in RequirementPage.build.. ... (1 additional frame(s) were not displayed)

NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference File "SourceFile", line 24, in setupNotificationChannel File "SourceFile", line 19, in createNotification File "SourceFile", line 1, in showNotification File "SourceFile", line 13, in show File "SourceFile", line 397, in onMethodCall ... (10 additional frame(s) were not displayed)

'''

binemmanuel avatar Jun 30 '25 10:06 binemmanuel

Yes I also got the same exception on Android side

Money1998 avatar Jul 07 '25 09:07 Money1998

Could this possibly be a regression between v18.0.1 and v19.2.1?

I recently upgraded and prior to that i saw this kind of crash literally 1x per day, while it now is happending dozends and hundreds of times. According to Crashlytics this happens on all kinds of devices and Android versions, including 9, 10, 13 and 15 - 100% in the background.

What is really strange about this error is that is occurs for a fraction of my user base, literally 9 people over the past 90 days....

mk-dev-1 avatar Jul 08 '25 07:07 mk-dev-1

@mk-dev-1 it's difficult for me to say unless someone can provide a link to a repo hosting a minimal app that can reproduce this problem. From what I've seen, these kind of issues stem more from lack of release build configuration being done on apps. This configuration isn't to do with the Flutter/Dart side so having a repo to look into helps with diagnosing the problem

MaikuB avatar Jul 08 '25 07:07 MaikuB

import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:permission_handler/permission_handler.dart';

class LocalNotificationService {
  static final FlutterLocalNotificationsPlugin _notificationsPlugin =
  FlutterLocalNotificationsPlugin();

  static Future<void> initialize() async {
    const AndroidInitializationSettings androidSettings =
        AndroidInitializationSettings('@drawable/ic_notification');

    const InitializationSettings initSettings = InitializationSettings(
      android: androidSettings,
    );

    await _notificationsPlugin.initialize(initSettings);

    // Register Android notification channel
    const AndroidNotificationChannel channel = AndroidNotificationChannel(
      'default_channel_id',
      'Default Channel',
      description: 'Used for basic app alerts',
      importance: Importance.high,
    );

    final androidPlatform = _notificationsPlugin
        .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
    await androidPlatform?.createNotificationChannel(channel);
  }

  static Future<void> requestPermission() async {
    if (await Permission.notification.isDenied) {
      await Permission.notification.request();
    }
  }

  static Future<void> showNotification({
    required String title,
    required String message,
  }) async {
    if (_notificationsPlugin == null) return;
    const AndroidNotificationDetails androidDetails =
        AndroidNotificationDetails(
      'default_channel_id', // Channel ID
      'Default Channel', // Channel name
      importance: Importance.max,
      priority: Priority.high,
    );

    const NotificationDetails notificationDetails = NotificationDetails(
      android: androidDetails,
    );

    await _notificationsPlugin.show(
      0, // Notification ID
      title,
      message,
      notificationDetails,
    );
  }

  static Future<void> clearAllNotifications() async {
    await _notificationsPlugin.cancelAll();
  }
}

Exception:

D/AudioManager( 6385): dispatching onAudioFocusChange(-2) to android.media.AudioManager@e386519ai.deepak.assistant.AudioFocusObserver@726d9de D/AudioFocusObserver( 6385): onAudioFocusChange: -2 D/AudioFocusObserver( 6385): Audio focus lost or transient loss E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): Failed to handle method call E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:482) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:383) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:1289) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1668) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:1454) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at android.os.Handler.handleCallback(Handler.java:959) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at android.os.Handler.dispatchMessage(Handler.java:100) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at android.os.Looper.loopOnce(Looper.java:234) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at android.os.Looper.loop(Looper.java:319) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at android.app.ActivityThread.main(ActivityThread.java:8754) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) E/MethodChannel#dexterous.com/flutter/local_notifications( 6385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:962) E/flutter ( 6385): [ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference E/flutter ( 6385): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.setSmallIcon(FlutterLocalNotificationsPlugin.java:482) E/flutter ( 6385): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.createNotification(FlutterLocalNotificationsPlugin.java:383) E/flutter ( 6385): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.showNotification(FlutterLocalNotificationsPlugin.java:1289) E/flutter ( 6385): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.show(FlutterLocalNotificationsPlugin.java:1668) E/flutter ( 6385): at com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin.onMethodCall(FlutterLocalNotificationsPlugin.java:1454) E/flutter ( 6385): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/flutter ( 6385): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292) E/flutter ( 6385): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319) E/flutter ( 6385): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0) E/flutter ( 6385): at android.os.Handler.handleCallback(Handler.java:959) E/flutter ( 6385): at android.os.Handler.dispatchMessage(Handler.java:100) E/flutter ( 6385): at android.os.Looper.loopOnce(Looper.java:234) E/flutter ( 6385): at android.os.Looper.loop(Looper.java:319) E/flutter ( 6385): at android.app.ActivityThread.main(ActivityThread.java:8754) E/flutter ( 6385): at java.lang.reflect.Method.invoke(Native Method) E/flutter ( 6385): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) E/flutter ( 6385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:962) E/flutter ( 6385): ) E/flutter ( 6385): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7) E/flutter ( 6385): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:370:18) E/flutter ( 6385): E/flutter ( 6385): #2 FlutterLocalNotificationsPlugin.show (package:flutter_local_notifications/src/flutter_local_notifications_plugin.dart:244:7) E/flutter ( 6385): E/flutter ( 6385): #3 LocalNotificationService.showNotification (package:mobile/core/utils/local_notification_service.dart:54:5) E/flutter ( 6385): E/flutter ( 6385):

flutter_local_notifications: ^19.3.0

dsngeu avatar Jul 11 '25 12:07 dsngeu

@MaikuB

dsngeu avatar Jul 11 '25 12:07 dsngeu

@dsngeu I mentioned it in my previous message but for issues like this, providing snippets of Dart code and stack traces won't help. I'd need links to a repo hosting a minimal app that can reproduce the problem

MaikuB avatar Aug 03 '25 00:08 MaikuB

@binemmanuel, your error is happening because your call to show() did not provide an AndroidNotificationDetails with a channel ID that corresponds to a channel you've created with createNotificationChannel(). Make sure to do that, and the error will go away.

#2689 tracks a better error message for this. Note that this is not the cause of the original issue in this report -- different stack trace.

Levi-Lesches avatar Sep 19 '25 11:09 Levi-Lesches