I am trying to do Serial Communication plugin from https://pub.dev/packages/serial_communication. But the app keep crashing. I only know Flutter, so I need help in Java.
Here is the error log :
E/AndroidRuntime(21246): java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.EventChannel$EventSink.success(java.lang.Object)' on a null object reference
E/AndroidRuntime(21246): at com.example.serial_communication.CustomEventHandler.lambda$sendEvent$0(CustomStreamHandler.java:43)
E/AndroidRuntime(21246): at com.example.serial_communication.CustomEventHandler$$ExternalSyntheticLambda0.run(D8$$SyntheticClass)
Here is the code :
class CustomEventHandler extends BroadcastReceiver implements EventChannel.StreamHandler {
static EventChannel.EventSink events;
static final Handler mainHandler = new Handler(Looper.getMainLooper());
@Override
public void onListen(Object arguments, EventChannel.EventSink events) {
this.events = events;
}
@Override
public void onCancel(Object arguments) {
events = null;
}
static void sendEvent(final Map<String,String> response) {
System.out.println("sendEvent response 1 " + response);
System.out.println("sendEvent events 1 " + events);
System.out.println("sendEvent mainHandler 1 " + mainHandler);
if (response != null && events != null && mainHandler != null) {
System.out.println("sendEvent response 2 " + response);
System.out.println("sendEvent response isEmpty 2 " + response.isEmpty());
System.out.println("sendEvent events 2 " + events);
System.out.println("sendEvent mainHandler 2 " + mainHandler);
Runnable runnable = () -> events.success(response);
if(runnable != null) {
mainHandler.post(runnable);
}
} else {
System.out.println("sendEvent failed " + response + events + mainHandler);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (events != null) {
events.success("Hello");
}
}
}
For normal results :
sendEvent response 1 {readChannel=fafb410040, LogChannel=Write a successful:fafb420043}
sendEvent events 1 io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler$EventSinkImplementation@b3930ac
sendEvent mainHandler 1 Handler (android.os.Handler) {5365175}
sendEvent response 2 {readChannel=fafb410040, LogChannel=Write a successful:fafb420043}
sendEvent response isEmpty 2 false
sendEvent events 2 io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler$EventSinkImplementation@b3930ac
sendEvent mainHandler 2 Handler (android.os.Handler) {5365175}
For crash results :
sendEvent response 1 {readChannel=fafb521934010100ff000000000000000000003030303030303030303081, LogChannel=Write a successful:fafb420043}
sendEvent events 1 null
sendEvent mainHandler 1 Handler (android.os.Handler) {5365175}
The line code for the error :
Runnable runnable = () -> events.success(response);
It seems like the "events" is null. How to make sure that the object EventChannel.EventSink events is initialized or is there any way to fix NullPointerException? I only do the null checking for now, but it keeps getting the same error. Please help me ^^