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.

Reading a Characteristic whose value is null crashes the app

See original GitHub issue

Hi,

We try to access a characteristic in our BLE device. The characteristic’s value is ‘null’. So, when we try to access, we should get null right? Following is the piece of code we used.

`public async Task<byte[]> Read(ICharacteristic characteristic) { byte[] data = {}; try { data = await characteristic.ReadAsync(); } catch (InvalidOperationException ex) { Debug.WriteLine("InvalidOperationException reading the characteristics value: " + ex.Message); } catch (CharacteristicReadException ex) { Debug.WriteLine("CharacteristicReadException reading the characteristics value: " + ex.Message); } catch (ArgumentNullException ex) { Debug.WriteLine("Excpetion in reading the characteristic " + characteristic.Id); } catch(Exception ex) { Debug.WriteLine(ex.Message); } return data; }

`

Steps to reproduce

  1. Used the above mentioned to read a characteristic value (which is null in BLE device)

  2. App crashes

Expected behavior

byte[] data = await characteristic.ReadAsync(); - ReadAsync() method should return null. So, datashould have the value as null. Ideally, app should not crash here.

Actual behavior

App crashes. Exception handling is done for reading the characteristic. But, the control does not come under any of the catch blocks which we have used. It crashes in Plugin.BLE.iOS.Characteristic line 71.

Crashlog

Nov 24 16:15:13 Audi-iPhone-5S RRCiOS[222] <Warning>: Characteristic.ReadAsync Nov 24 16:15:27 Audi-iPhone-5S RRCiOS[222] <Warning>: Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: src at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal:copy_from_unmanaged (intptr,int,System.Array,int) at System.Runtime.InteropServices.Marshal.Copy (System.IntPtr source, System.Byte[] destination, System.Int32 startIndex, System.Int32 length) [0x00000] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Runtime.InteropServices/Marshal.cs:152 at Foundation.NSData.ToArray () [0x00012] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/src/Foundation/NSData.cs:50 at Plugin.BLE.iOS.Characteristic.get_Value () [0x00000] in C:\Projects\xamarin-bluetooth-le\Source\Plugin.BLE.iOS\Characteristic.cs:25 at Plugin.BLE.iOS.Characteristic+<>c__DisplayClass15_0.<ReadNativeAsync>b__2 (System.Object sender, CoreBluetooth.CBCharacteristicEventArgs args) [0x00051] in C:\Projects\xamarin-bluetooth-le\Source\Plugin.BLE.iOS\Characteristic.cs:71 at CoreBluetooth.CBPeripheral+_CBPeripheralDelegate.UpdatedCharacterteristicValue (CoreBluetooth.CBPeripheral peripheral, CoreBluetooth.CBCharacteristic characteristic, Foundation.NSError error) [0x00015] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/src/build/ios/native/CoreBluetooth/CBPeripheral.g.cs:496 at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/src/UIKit/UIApplication.cs:79 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3818/3983064a/source/xamarin-macios/src/UIKit/UIApplication.cs:63 at RRC.iOS.Application.Main (System.String[] args) [0x00008] in /Users/audiologymacbook/Documents/AudiApps/TFSWS/CoffeeWorkspace/Feat/Coffee_Dev/RCApps/Source/RRC.iOS/Main.cs:17

We downloaded plugin’s source code and used it to read that particular characteristic. Below is the screenshot of crash. image

Configuration

Version: 9.3.1

Platform: iOS

Device: iPhone 5S

We want to handle this crash in our app. How should we do it? Please help.

Thanks, Leela

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
leelatoocommented, Nov 28, 2016

@smstuebe Thanks 👍 It works.

1reaction
xabrecommented, Nov 25, 2016

@smstuebe agreed

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading a Characteristic whose value is null crashes the app
Hi, We try to access a characteristic in our BLE device. The characteristic's value is 'null'. So, when we try to access, we...
Read more >
Checking null and "emptyness" in Java (Android) --> crash
I made an if/else statement that checks if the value of an Edit Text (the value of the Edit Text is saved in...
Read more >
Detect and diagnose crashes
There are many situations that can cause a crash in your app. Some reasons are obvious, like checking for a null value or...
Read more >
7 ways to null crash Android Kotlin without using !! | by Elye
7 ways to null crash Android Kotlin without using !! · 1. Getting value from Intent in Android · 2. Accessing Intent in...
Read more >
Retrofitting null-safety onto Java at Meta
We developed a new static analysis tool called Nullsafe that is used at Meta to detect NullPointerException (NPE) errors in Java code.
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