Skip to content

Commit 47c4568

Browse files
ramanversemeta-codesync[bot]
authored andcommitted
Fix NullPointerException in PromiseImpl.reject by allowing nullable code (facebook#54731)
Summary: # Fix NullPointerException in PromiseImpl.reject This PR fixes a `NullPointerException` that occurs when `Promise.reject` is called with a `null` code from Java native modules. The issue arises because `Promise.kt` and `PromiseImpl.kt` defined the `code` parameter as non-nullable `String` in several overloads. However, `PromiseImpl`'s internal logic (specifically the catch-all `reject` method) is designed to handle `null` codes by defaulting to `EUNSPECIFIED`. When a Java module (such as `react-native-ble-plx`) calls `promise.reject(null, message)`, Kotlin's generated null-checks throw a `NullPointerException` before the method body is executed. ## Changelog [Android] [Fixed] - Allow nullable `code` in `Promise.reject` to prevent NPEs from Java modules Pull Request resolved: facebook#54731 Test Plan: 1. Create a native module in Java that calls `promise.reject(null, "Error message")`. 2. Before this fix, the app crashes with `java.lang.NullPointerException: Parameter specified as non-null is null: method com.facebook.react.bridge.PromiseImpl.reject, parameter code`. 3. After this fix, the promise is rejected with code `EUNSPECIFIED` and the app does not crash. ## Related Issue Fixes facebook#54722 Reviewed By: fabriziocucci, cortinico Differential Revision: D88066375 Pulled By: javache fbshipit-source-id: aec56b2d44dc8260e9b0496c2ec5fc49e70ca9cf
1 parent d49e07a commit 47c4568

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Promise.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public interface Promise {
2828
* @param code String
2929
* @param message String
3030
*/
31-
public fun reject(code: String, message: String?)
31+
public fun reject(code: String?, message: String?)
3232

3333
/**
3434
* Report an exception with a custom code.
3535
*
3636
* @param code String
3737
* @param throwable Throwable
3838
*/
39-
public fun reject(code: String, throwable: Throwable?)
39+
public fun reject(code: String?, throwable: Throwable?)
4040

4141
/**
4242
* Report an exception with a custom code and error message.
@@ -45,7 +45,7 @@ public interface Promise {
4545
* @param message String
4646
* @param throwable Throwable
4747
*/
48-
public fun reject(code: String, message: String?, throwable: Throwable?)
48+
public fun reject(code: String?, message: String?, throwable: Throwable?)
4949

5050
/**
5151
* Report an exception, with default error code. Useful in catch-all scenarios where it's unclear
@@ -73,7 +73,7 @@ public interface Promise {
7373
* @param code String
7474
* @param userInfo WritableMap
7575
*/
76-
public fun reject(code: String, userInfo: WritableMap)
76+
public fun reject(code: String?, userInfo: WritableMap)
7777

7878
/**
7979
* Report an exception with a custom code and userInfo.
@@ -82,7 +82,7 @@ public interface Promise {
8282
* @param throwable Throwable
8383
* @param userInfo WritableMap
8484
*/
85-
public fun reject(code: String, throwable: Throwable?, userInfo: WritableMap)
85+
public fun reject(code: String?, throwable: Throwable?, userInfo: WritableMap)
8686

8787
/**
8888
* Report an error with a custom code, error message and userInfo, an error not caused by an
@@ -92,7 +92,7 @@ public interface Promise {
9292
* @param message String
9393
* @param userInfo WritableMap
9494
*/
95-
public fun reject(code: String, message: String?, userInfo: WritableMap)
95+
public fun reject(code: String?, message: String?, userInfo: WritableMap)
9696

9797
/**
9898
* Report an exception with a custom code, error message and userInfo.

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/PromiseImpl.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ constructor(private var resolve: Callback?, private var reject: Callback?) : Pro
3838
* @param code String
3939
* @param message String
4040
*/
41-
override fun reject(code: String, message: String?) {
41+
override fun reject(code: String?, message: String?) {
4242
reject(code, message, null, null)
4343
}
4444

@@ -48,7 +48,7 @@ constructor(private var resolve: Callback?, private var reject: Callback?) : Pro
4848
* @param code String
4949
* @param throwable Throwable
5050
*/
51-
override fun reject(code: String, throwable: Throwable?) {
51+
override fun reject(code: String?, throwable: Throwable?) {
5252
reject(code, null, throwable, null)
5353
}
5454

@@ -59,7 +59,7 @@ constructor(private var resolve: Callback?, private var reject: Callback?) : Pro
5959
* @param message String
6060
* @param throwable Throwable
6161
*/
62-
override fun reject(code: String, message: String?, throwable: Throwable?) {
62+
override fun reject(code: String?, message: String?, throwable: Throwable?) {
6363
reject(code, message, throwable, null)
6464
}
6565

@@ -94,7 +94,7 @@ constructor(private var resolve: Callback?, private var reject: Callback?) : Pro
9494
* @param code String
9595
* @param userInfo WritableMap
9696
*/
97-
override fun reject(code: String, userInfo: WritableMap) {
97+
override fun reject(code: String?, userInfo: WritableMap) {
9898
reject(code, null, null, userInfo)
9999
}
100100

@@ -105,7 +105,7 @@ constructor(private var resolve: Callback?, private var reject: Callback?) : Pro
105105
* @param throwable Throwable
106106
* @param userInfo WritableMap
107107
*/
108-
override fun reject(code: String, throwable: Throwable?, userInfo: WritableMap) {
108+
override fun reject(code: String?, throwable: Throwable?, userInfo: WritableMap) {
109109
reject(code, null, throwable, userInfo)
110110
}
111111

@@ -117,7 +117,7 @@ constructor(private var resolve: Callback?, private var reject: Callback?) : Pro
117117
* @param message String
118118
* @param userInfo WritableMap
119119
*/
120-
override fun reject(code: String, message: String?, userInfo: WritableMap) {
120+
override fun reject(code: String?, message: String?, userInfo: WritableMap) {
121121
reject(code, message, null, userInfo)
122122
}
123123

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/share/ShareModuleTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ class ShareModuleTest {
108108
this.value = value
109109
}
110110

111-
override fun reject(code: String, message: String?) {
111+
override fun reject(code: String?, message: String?) {
112112
reject(code, message, null, null)
113113
}
114114

115-
override fun reject(code: String, throwable: Throwable?) {
115+
override fun reject(code: String?, throwable: Throwable?) {
116116
reject(code, null, throwable, null)
117117
}
118118

119-
override fun reject(code: String, message: String?, throwable: Throwable?) {
119+
override fun reject(code: String?, message: String?, throwable: Throwable?) {
120120
reject(code, message, throwable, null)
121121
}
122122

@@ -128,15 +128,15 @@ class ShareModuleTest {
128128
reject(null, null, throwable, userInfo)
129129
}
130130

131-
override fun reject(code: String, userInfo: WritableMap) {
131+
override fun reject(code: String?, userInfo: WritableMap) {
132132
reject(code, null, null, userInfo)
133133
}
134134

135-
override fun reject(code: String, throwable: Throwable?, userInfo: WritableMap) {
135+
override fun reject(code: String?, throwable: Throwable?, userInfo: WritableMap) {
136136
reject(code, null, throwable, userInfo)
137137
}
138138

139-
override fun reject(code: String, message: String?, userInfo: WritableMap) {
139+
override fun reject(code: String?, message: String?, userInfo: WritableMap) {
140140
reject(code, message, null, userInfo)
141141
}
142142

0 commit comments

Comments
 (0)