This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 45602 - NullPointerException when applying style
Summary: NullPointerException when applying style
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms (show other bugs)
Version: 2.3.2
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-10-17 21:33 UTC by Sascha Schwegelbauer
Modified: 2017-07-18 12:51 UTC (History)
6 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Sascha Schwegelbauer 2016-10-17 21:33:49 UTC
When applying a Style which is setting BackgroundColor on a VisualElement, a Java.Lang.NullPointerException is thrown.
Maybe it's related to API19 (another project with nearly the same code uses API15 and there it runs without problems).
Also on iOS and Windows, the code runs without errors.

StackTrace:

Unhandled Exception:

Java.Lang.NullPointerException: 

10-17 23:29:52.639 D/Mono    (11187): DllImport attempting to load: '/system/lib/liblog.so'.
10-17 23:29:52.639 D/Mono    (11187): DllImport loaded library '/system/lib/liblog.so'.
10-17 23:29:52.639 D/Mono    (11187): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
10-17 23:29:52.639 D/Mono    (11187): Searching for '__android_log_print'.
10-17 23:29:52.639 D/Mono    (11187): Probing '__android_log_print'.
10-17 23:29:52.639 D/Mono    (11187): Found as '__android_log_print'.
10-17 23:29:52.639 I/MonoDroid(11187): UNHANDLED EXCEPTION:
10-17 23:29:52.639 I/MonoDroid(11187): Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
10-17 23:29:52.639 I/MonoDroid(11187):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3819/96c7ba6c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
10-17 23:29:52.639 I/MonoDroid(11187):   at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x000a7] in /Users/builder/data/lanes/3819/96c7ba6c/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12083 
10-17 23:29:52.639 I/MonoDroid(11187):   at Java.Interop.JniPeerMembers+JniInstanceMethods.FinishCreateInstance (System.String constructorSignature, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00060] in /Users/builder/data/lanes/3819/96c7ba6c/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods.cs:148 
10-17 23:29:52.639 I/MonoDroid(11187):   at Android.Graphics.Canvas..ctor (Android.Graphics.Bitmap bitmap) [0x00078] in /Users/builder/data/lanes/3819/96c7ba6c/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Graphics.Canvas.cs:236 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.Platform.Android.FrameRenderer+FrameDrawable.FrameOnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x0002e] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\FrameRenderer.cs:182 
10-17 23:29:52.639 I/MonoDroid(11187):   at (wrapper delegate-invoke) <Module>:invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs)
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x0000a] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:137 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Element.cs:369 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x000f4] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:581 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0014b] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:378 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0005f] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:531 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:314 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.Setter.Apply (Xamarin.Forms.BindableObject target, System.Boolean fromStyle) [0x00097] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Setter.cs:59 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.Style.ApplyCore (Xamarin.Forms.BindableObject bindable, Xamarin.Forms.Style basedOn) [0x0001e] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Style.cs:134 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.Style.Xamarin.Forms.IStyle.Apply (Xamarin.Forms.BindableObject bindable) [0x0002b] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Style.cs:97 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.VisualElement+MergedStyle.SetStyle (Xamarin.Forms.IStyle implicitStyle, System.Collections.Generic.IList`1[T] classStyles, Xamarin.Forms.IStyle style) [0x00156] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\MergedStyle.cs:164 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.VisualElement+MergedStyle.set_Style (Xamarin.Forms.IStyle value) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\MergedStyle.cs:39 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.VisualElement+<>c.<.cctor>b__212_6 (Xamarin.Forms.BindableObject bindable, System.Object oldvalue, System.Object newvalue) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:79 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.BindableObject+SetValueFlags attributes, System.Boolean silent) [0x00108] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:584 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.BindableObject+SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x0014b] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:378 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x0020c] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindingExpression.cs:177 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindingExpression.cs:55 
10-17 23:29:52.639 I/MonoDroid(11187):   at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__44_0 () [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindingExpression.cs:475 
10-17 23:29:52.643 I/MonoDroid(11187):   at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/3819/96c7ba6c/source/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36 
10-17 23:29:52.643 I/MonoDroid(11187):   at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in /Users/builder/data/lanes/3819/96c7ba6c/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Java.Lang.IRunnable.cs:81 
10-17 23:29:52.643 I/MonoDroid(11187):   at (wrapper dynamic-method) System.Object:bc1d8053-473a-4ed8-bff0-bcfae9b482f6 (intptr,intptr)
10-17 23:29:52.643 I/MonoDroid(11187):   --- End of managed Java.Lang.NullPointerException stack trace ---
10-17 23:29:52.643 I/MonoDroid(11187): java.lang.NullPointerException
10-17 23:29:52.643 I/MonoDroid(11187): 	at android.graphics.Canvas.<init>(Canvas.java:136)
10-17 23:29:52.643 I/MonoDroid(11187): 	at mono.java.lang.RunnableImplementor.n_run(Native Method)
10-17 23:29:52.643 I/MonoDroid(11187): 	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
10-17 23:29:52.643 I/MonoDroid(11187): 	at android.app.Activity.runOnUiThread(Activity.java:4713)
10-17 23:29:52.643 I/MonoDroid(11187): 	at mono.java.lang.RunnableImplementor.n_run(Native Method)
10-17 23:29:52.643 I/MonoDroid(11187): 	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
10-17 23:29:52.643 I/MonoDroid(11187): 	at android.os.Handler.handleCallback(Handler.java:733)
10-17 23:29:52.643 I/MonoDroid(11187): 	at android.os.Handler.dispatchMessage(Handler.java:95)
10-17 23:29:52.643 I/MonoDroid(11187): 	at android.os.Looper.loop(Looper.java:136)
10-17 23:29:52.643 I/MonoDroid(11187): 	at android.app.ActivityThread.main(ActivityThread.java:5001)
10-17 23:29:52.643 I/MonoDroid(11187): 	at java.lang.reflect.Method.invokeNative(Native Method)
10-17 23:29:52.643 I/MonoDroid(11187): 	at java.lang.reflect.Method.invoke(Method.java:515)
10-17 23:29:52.643 I/MonoDroid(11187): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-17 23:29:52.643 I/MonoDroid(11187): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-17 23:29:52.643 I/MonoDroid(11187): 	at dalvik.system.NativeStart.main(Native Method)
10-17 23:29:52.643 D/Mono    (11187): Assembly Ref addref HockeySDK.AndroidBindings[0xb881c860] -> System[0xb8907b50]: 15
Comment 1 adrianknight89 2016-10-17 23:15:50 UTC
Can you post a repro?
Comment 2 Philip.Apptelic 2017-04-26 11:46:49 UTC
I'm getting the same exception at the same place (Java.Lang.NullPointerException in Android.Graphics.Canvas..ctor, called from Xamarin.Forms.Platform.Android.FrameRenderer+FrameDrawable.FrameOnPropertyChanged), but mine is due to attempting to bind a colour value to the frame's BackgroundColor property, although this is also what OP's style application is  effectively doing, too.

This is still happening in current version of Forms 2.3.4.231, and pre-release 2.3.5.233-pre1 too.

I don't have a simple repro for this, but I have diagnosed it. It's happening because one or more of the BackgroundColor, OutlineColor or CornerRadius properties is being set before the view containing the Frame element has been initially rendered (or the Frame is not visible and so hasn't been rendered), which results in the _normalBitmap local not having been created yet due to FrameDrawable.Draw not having been called yet.

This can be resolved by adding a null check on _normalBitmap in the Android Frame renderer before attempting to create the new Android Canvas with it, here:
https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Renderers/FrameRenderer.cs#L191
Comment 3 Rui Marinho 2017-05-04 14:13:20 UTC
Should be fixed in 2.3.5-pre3
Comment 5 Sascha Schwegelbauer 2017-06-30 10:28:58 UTC
You don't set the background color using a style, so your code does not cover the problem.
Comment 6 Saurabh Paunikar 2017-06-30 10:35:57 UTC
@Sascha Can you please guide how to set background color using style.
Comment 7 Saurabh Paunikar 2017-07-18 12:51:54 UTC
Hi ,

Step I followed to verify this issue

1.Create new xamarin form project.
2.Added styles in resource dictionary in App.xaml as 
 
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App91.App">
	<Application.Resources>

        <ResourceDictionary>
            <Style TargetType="ContentPage" x:Key="PageStyle">
                <Setter Property="BackgroundColor" Value="#ff0000" />
            </Style>
            <Style TargetType="Button" x:Key="ButtonColor">
                <Setter Property="BackgroundColor" Value="#27dbcc" />
                </Style>
        </ResourceDictionary>

    </Application.Resources>
</Application>

3. Use added style to set background color of content page and button in MainPage.xaml as

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App91"
             x:Class="App91.MainPage"
             Style="{StaticResource PageStyle}">

    <Label Text="Welcome to Xamarin Forms!" 
           VerticalOptions="Center" 
           HorizontalOptions="Center" />
    <Button
        Text="My Button" 
           VerticalOptions="Center" 
           HorizontalOptions="Center"
         Style="{StaticResource ButtonColor}"/>

</ContentPage>

4.Update xamarin.form version to 2.3.5.256-pre6

5.Build and run this project on various android emulator with different API


ScreenCast Link : https://www.screencast.com/t/I6e0DiW7AWn

Note You need to log in before you can comment on or make changes to this bug.