question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Do not call `byteArrayToWritableMap` with a null byte array

See original GitHub issue

Version

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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Sean-Browncommented, Jul 24, 2018

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 81

0reactions
Sean-Browncommented, Jul 24, 2018

Yes I’m still seeing the error:

01:55 W/System.err: java.lang.NullPointerException: Attempt to get length of null array
01:55 W/System.err: 	at android.util.Base64.encode(Base64.java:494)
01:55 W/System.err: 	at android.util.Base64.encodeToString(Base64.java:456)
01:55 W/System.err: 	at it.innove.Peripheral.byteArrayToWritableMap(Peripheral.java:264)
01:55 W/System.err: 	at it.innove.Peripheral.asWritableMap(Peripheral.java:164)
01:55 W/System.err: 	at it.innove.BleManager.getConnectedPeripherals(BleManager.java:480)
01:55 W/System.err: 	at java.lang.reflect.Method.invoke(Native Method)
01:55 W/System.err: 	at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:374)
01:55 W/System.err: 	at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:162)
01:55 W/System.err: 	at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
01:55 W/System.err: 	at android.os.Handler.handleCallback(Handler.java:751)
01:55 W/System.err: 	at android.os.Handler.dispatchMessage(Handler.java:95)
01:55 W/System.err: 	at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
01:55 W/System.err: 	at android.os.Looper.loop(Looper.java:154)
01:55 W/System.err: 	at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
01:55 W/System.err: 	at java.lang.Thread.run(Thread.java:762)

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 the advertisingDataBytes attribute. Then it calls the asWritableMap method on the new peripheral, which will call byteArrayToWritableMap 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:

  • Initialize advertisingDataBytes to an empty array
  • Do a null-check on advertisingDataBytes before calling byteArrayToWritableMap

What I don’t fully understand is how this would impact the caller (BleManager.getConnectedPeripherals)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found