NullPointerException - Register native module in React Native
Hi, I'm currently porting my react native app to Android and I'm getting this error when I try to register my module in the MainActivity class :
12-03 12:15:47.716 21670-21670/com.cinqsdriverapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cinqsdriverapp, PID: 21670
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
at java.util.ArrayList.addAll(ArrayList.java:188)
at com.facebook.react.ReactInstanceManager.createAllViewManagers(ReactInstanceManager.java:292)
at com.facebook.react.CoreModulesPackage.createNativeModules(CoreModulesPackage.java:54)
at com.facebook.react.ReactInstanceManager.processPackage(ReactInstanceManager.java:446)
at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:412)
at com.facebook.react.ReactInstanceManager.recreateReactContext(ReactInstanceManager.java:345)
at com.facebook.react.ReactInstanceManager.onJSBundleLoadedFromServer(ReactInstanceManager.java:310)
at com.facebook.react.ReactInstanceManager.access$100(ReactInstanceManager.java:70)
at com.facebook.react.ReactInstanceManager$1.onJSBundleLoadedFromServer(ReactInstanceManager.java:97)
at com.facebook.react.devsupport.DevSupportManager$13$1.run(DevSupportManager.java:530)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Here is my code : MainActivity.java
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
private ReactInstanceManager mReactInstanceManager;
private ReactRootView mReactRootView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new RCTModulesPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "myApp", null);
setContentView(mReactRootView);
}
//other functions
}
My package :
public class RCTModulesPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new AppInit(reactContext));
return modules;*/
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return null;
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return null;
}
}
And finally my module:
public class AppInit extends ReactContextBaseJavaModule {
public AppInit(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "AppInit";
}
@ReactMethod
public void getInitialState(Callback callback) {
JSONObject json = new JSONObject();
try {
json.put("DEBUG", "DEBUG");
} catch (JSONException e) {
e.printStackTrace();
}
callback.invoke(json.toString());
}
}
Is this caused internally or did I make a mistake ? Thanks
Hey ilansas, thanks for reporting this issue!
React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.
- If this is a feature request or a bug that you would like to be fixed by the team, please report it on Product Pains. It has a ranking feature that lets us focus on the most important issues the community is experiencing.
- If you don't know how to do something or not sure whether some behavior is expected or a bug, please ask on StackOverflow with the tag
react-nativeor for more real time interactions, ask on Discord in the #react-native channel. - We welcome clear issues and PRs that are ready for in-depth discussion; thank you for your contributions!
You need to return Collections.emptyList() instead of null in your RCTModulesPackage.
public class RCTModulesPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new AppInit(reactContext));
return modules;
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
BTW, there's an awesome place to ask question like this one: StackOverflow. It's the best system for Q&A. Many people from the community hang out there and will be able to see your question, you can vote on answers and mark question as answered etc. This lets us keep a list of bug reports and feature requests on github and especially Product Pains (again, with voting which is really nice).
If you think StackOverflow works for you please consider posting there instead and closing this issue.
I'm posting this here because github issues haven't been working very well for us and because StackOverflow is so much better. Thanks for reading! :)
Thanks, for the trick. I'm sorry for using Issues, I can delete my post if you'd like