Do not call `byteArrayToWritableMap` with a null byte array
See original GitHub issueVersion
Tell us which versions you are using:
- react-native-ble-manager v6.4.0
- react-native v0.52.3
- Android v7.1.1
Expected behaviour
My log should not constantly get these errors:
01:57 W/System.err: java.lang.NullPointerException: Attempt to get length of null array
01:57 W/System.err: at android.util.Base64.encode(Base64.java:494)
01:57 W/System.err: at android.util.Base64.encodeToString(Base64.java:456)
01:57 W/System.err: at it.innove.Peripheral.byteArrayToWritableMap(Peripheral.java:257)
01:57 W/System.err: at it.innove.Peripheral.asWritableMap(Peripheral.java:158)
01:57 W/System.err: at it.innove.BleManager.getConnectedPeripherals(BleManager.java:480)
01:57 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01:57 W/System.err: at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:374)
01:57 W/System.err: at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:162)
01:57 W/System.err: at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
01:57 W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
01:57 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01:57 W/System.err: at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
01:57 W/System.err: at android.os.Looper.loop(Looper.java:154)
01:57 W/System.err: at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
01:57 W/System.err: at java.lang.Thread.run(Thread.java:762)
Actual behaviour
I am constantly getting errors about trying to encode a null byte array. I believe the issue is in Peripheral.java, line 179 – it should be checking the result of entry.getValue()
and only call byteArrayToWritableMap
if the value is non-null.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How do I convert a byte array with null terminating character to ...
Any bytes after the null byte are (possibly) garbage characters that I don't want to include in the string. The last response under...
Read more >Best way to append null bytes to a byte array? — oracle-tech
I have a fixed length byte array and need to add two null bytes to its end. ... You don't have to explicitly...
Read more >Convert String to Byte Array and Reverse in Java - Baeldung
Have a look at ways to convert between a String and a byte array in Java. ... We can get this charset by...
Read more >Null-terminated String
A null-terminated string is a sequence of ASCII characters, one to a byte, followed by a zero byte (a null byte). null-terminated strings...
Read more >ByteArray - Kotlin Programming Language
firstOrNull (): Byte? Returns the first element matching the given predicate, or null if element was not found.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I experimented with initializing the advertisingDataBytes to an empty array and it seems to avoid the error, i.e. on line 48 do
private byte[] advertisingDataBytes = new byte[0]
, or initialize it in the third constructor on line 81Yes I’m still seeing the error:
Looking at the code I can’t imagine how the
getConnectedPeripherals
method in the BleManager.java file could work. It constructs a new peripheral object but calls the constructor that won’t initialize theadvertisingDataBytes
attribute. Then it calls theasWritableMap
method on the new peripheral, which will callbyteArrayToWritableMap
with the advertisingDataBytes member as an argument, but I don’t see how this member will ever be non-null. I’m not sure what the correct solution would be but a couple come to mind:What I don’t fully understand is how this would impact the caller (BleManager.getConnectedPeripherals)