react-native-sms icon indicating copy to clipboard operation
react-native-sms copied to clipboard

NullPointerException on calling `SendSMSPackage.onActivityResult`

Open xavi opened this issue 7 years ago • 4 comments

Our logs show that the most popular crash of our app is

Caused by java.lang.NullPointerException
Attempt to invoke virtual method 'void com.tkporter.sendsms.SendSMSModule.onActivityResult(android.app.Activity, int, int, android.content.Intent)' on a null object reference
  com.tkporter.sendsms.SendSMSPackage.onActivityResult (SendSMSPackage.java:49)

So, I understand that the sendSms in this line is null.

That's called from the app's MainActivity.java

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  SendSMSPackage.getInstance().onActivityResult(requestCode, resultCode, data);
  ...

Any idea on what's the problem? Thanks in advance.

xavi avatar Apr 30 '18 16:04 xavi

Hey @xavi, I'm taking a look now and it seems like getInstance() might be starting with instance as null and then creating a new instance without initializing sendSms. Could you check to see if instance is null when getInstance() is called by SendSMSPackage.getInstance().onActivityResult(requestCode, resultCode, data);?

That might help understand why sendSms is not initialized properly. Thanks!

tkporter avatar Apr 30 '18 18:04 tkporter

Hi @tkporter, sorry for the late reply, I was busy with other things.

I can't check if instance is null when getInstance() is called because I can't reproduce it. This is a type of crash that some users of our app are having. I guess the only way to check it would be to put some logging in SendSMSPackage.getInstance(). We can do that but it will take time because requires a new release of the app (and a forked react-native-sms).

But looking at the react-native-sms code, I understand that the only way for this NullPointerException to occur is that instance is effectively null when getInstance() executes, and so sendSms is not initialized. If that were the case, do you know how it can be solved? Maybe there's a way to ensure that createNativeModules is called before MainActivity's onActivityResult? (Just guessing, I don't know what's the lifecycle of React Native's native modules.)

xavi avatar May 15 '18 15:05 xavi

Is the line SendSMSPackage.getInstance().onActivityResult(requestCode, resultCode, data); in MainActivity#onActivityResult really necessary? That's the line that leads to the crash, so I tried deleting it, and SMS sending still seems to work.

xavi avatar May 16 '18 12:05 xavi

Hello @tkporter. I observed the same on a LG G4c with Android 6.0 and had reports from users with Android 5 and 4. I changed SendSMSPackage.java:48 to public void onActivityResult(int requestCode, int resultCode, Intent data) { if (sendSms != null) { sendSms.onActivityResult(null, requestCode, resultCode, data); } } and my app did not crash (but did not resume either of course) which leads me to believe that private SendSMSModule sendSms is null by that point.

mousikosk avatar Jul 30 '18 12:07 mousikosk