Exception loading PDF File on Android Side

I have the next error when Load a document:
System.NullReferenceException: Object reference not set to an instance of an object.
  at Syncfusion.SfPdfViewer.XForms.Droid.ScrollViewEx.LoadDocument (System.IO.Stream inputStream) [0x001ab] in <3a7a067f010a46fb9644922c745ce452>:0 
  at Syncfusion.SfPdfViewer.XForms.Droid.SfPdfDocumentViewRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00083] in <3a7a067f010a46fb9644922c745ce452>:0 
  at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs)
  at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:229 
  at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:358 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00114] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:461 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:397 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x00042] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:334 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:311 
  at Syncfusion.SfPdfViewer.XForms.DocumentViewHelper.set_InputFileStream (System.IO.Stream value) [0x00000] in <58958fe884e342ac898d77120a7e28c8>:0 
  at Syncfusion.SfPdfViewer.XForms.SfPdfViewer.OnPdfViewerPropertyChanged (System.String propertyName) [0x00019] in <58958fe884e342ac898d77120a7e28c8>:0 
  at Syncfusion.SfPdfViewer.XForms.SfPdfViewer.OnInputFileStreamChanged (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x0048c] in <58958fe884e342ac898d77120a7e28c8>:0 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:463 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:397 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x00042] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:334 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:311 
ewer.set_InputFileStream (System.IO.Stream value) [0x00000] in <58958fe884e342ac898d77120a7e28c8>:0 
  at Syncfusion.SfPdfViewer.XForms.SfPdfViewer.LoadDocument (System.IO.Stream stream) [0x0005a] in <58958fe884e342ac898d77120a7e28c8>:0 
  at Avantxa.Vistas.PDFViewer.OnAppearing () [0x00299] in /Users/maxal2/Desktop/APP/Avantxa/Avantxa/Vistas/PDFViewer.xaml.cs:63 


It only happens when runs on Android (but not on all phones) and since I updated SfPdfViewer to the last version.

I'm using:
  • SfPdfViewer 18.3.0.44
  • Xamarin.Forms 4.8.0.1560
My code:
protected async override void OnAppearing()
        {
            base.OnAppearing();

            var Recibo = await Constants.ObtenerReciboId(jwt, id);

            if (Recibo == null)
            {
                await DisplayAlert("Error", "Error", "Ok");
            }
            else
            {
                Stream fileStream = new MemoryStream();

                foreach (var item in Recibo)
                {
                    fileStream = new MemoryStream(item.RecArchivo);
                    
                    if (item.RecConcepto.Trim() != "Renta")
                        BtnFactura.IsVisible = false;
                }

                try
                {
                    pdfViewerControl.LoadDocument(fileStream);
                }
                catch(Exception es)
                {
                    Console.WriteLine(es);
                }
            }
        }



13 Replies 1 reply marked as answer

ME Matthew Emson October 29, 2020 07:07 PM UTC

I am also seeing this after upgrading to 18.3.0.44 with the latest 4.8.0.1560 of Xamarin Forms. 

Originally we bound the stream to the InputFileStream in Xaml, but this stopped working after the update. I then changed the code to call LoadDocument() passing the same stream instance, but I get the same error. The original bound code was working on  18.1.0.52 (which as I remember it, I had to go to to make it work at all), but now we get: System.NullReferenceException: 'Object reference not set to an instance of an object.' and if I dig in to the exception, "at Syncfusion.SfPdfViewer.XForms.Droid.ScrollViewEx.LoadDocument" is the first step on the stack trace mentioned (call stack the rest of the call stack looks the same as the original poster) and if I look at the instance of sfPdfViewer, it looks like the "CustomPdfRenderer" is null. I would say this is a show stopper for us as both our help and licenses are delivered as PDF in app. The PdfViewer is basically just a Content page:



<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:syncfusion="clr-namespace:Syncfusion.SfPdfViewer.XForms;assembly=Syncfusion.SfPdfViewer.XForms"
             mc:Ignorable="d"
             x:Class="ProductName.PdfViewerPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <syncfusion:Toolbar x:Key="ToolbarSettings">
                <syncfusion:Toolbar.Enabled>false
                syncfusion:Toolbar.Enabled>
            syncfusion:Toolbar>
        ResourceDictionary>
    ContentPage.Resources>
    <ContentPage.Content>
        <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            Grid.RowDefinitions>
            <syncfusion:SfPdfViewer Grid.Row="0" x:Name="PdfControl" InputFileStream="{Binding DocumentSource}"
                                    Toolbar="{StaticResource ToolbarSettings}"
                                    BookmarkNavigationEnabled="False"/>
            <Button Grid.Row="1" Text="Close" Command="{Binding Close}" VerticalOptions="End" HorizontalOptions="CenterAndExpand" />
        Grid>
    ContentPage.Content>
ContentPage>
There is nothing in the code behind

The viewmodel is:

public class PdfViewModel : ViewModel
{
    private Stream documentStream;
 
    public PdfViewModel()
    {
        Close = new Command(CloseHandler);
    }
 
    async void CloseHandler(object obj)
    {
        await App.Current.MainPage.Navigation.PopModalAsync();
    }
 
    public Stream DocumentStream
    {
        get => documentStream;
        set
        {
            if (value != documentStream)
            {
                documentStream = value;
                OnPropertyChanged();
            }
        }
    }
 
    public ICommand Close { get; }
}

ViewModel just implements INotifyPropertyChanged

    public abstract class ViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberNamestring propertyName = null)
        {
            PropertyChanged?.Invoke(thisnew PropertyChangedEventArgs(propertyName));
        }
    }
Then the code that sets this up is just boiler plate stuff:
var viewModel = new PdfViewModel
{
    DocumentStream = stream // stream is valid, has data and has seeked position 0 from the start of the stream.
};

var page = new PdfViewerPage
{
    BindingContext = viewModel,
};

try
{
    await App.Current.MainPage.Navigation.PushModalAsync(page);
}
catch (Exception ex)
{
    log.Error(TAG, ex.Message); // this is just a simple log provider and you don't need this to get the example working
    log.Error(TAG, ex.StackTrace);                    
}  
So, the code is in a Command attached to a Button on a ContentPage that was shown with PushModalAsync() (we do not use a NavigationPage as we do not show pages in a navigation stuck - all sub forms are modal.)

Any ideas?

             


FR Francisco October 29, 2020 07:12 PM UTC

I have to reinstall the old version of SfPdfViewer (18.3.038)


ME Matthew Emson replied to Francisco October 29, 2020 07:23 PM UTC

I have to reinstall the old version of SfPdfViewer (18.3.038)

Does that version work with the latest Xamarin Forms?

I reverted back to my previous versions (Xamarin Forms 4.6.0.847 and Syncfusion assemblies at version 18.1.0.52) and the exact same files delivered with the same mechanism (Xaml binding) now open and render correctly. There is definitely a bug in 18.3.0.44.


FR Francisco October 29, 2020 07:27 PM UTC

Yes

I'm working with Xamarin.Forms 4.8.0.1560 and SfPdfViewer 18.3.038, all runs perfectly


SP Satheesh Palanisamy Syncfusion Team October 30, 2020 01:49 PM UTC

Hi Francisco,

 

Greetings from Syncfusion support, 


We have faced the issue in the weekly NuGet version of 18.3.0.44 while loading the document in the Xarmarin Forms Android and the fix for the issue is available on the custom NuGet in the link below. 


Note: Kindly follow the steps to clear the NuGet cache and then install patched NuGet to get the issue resolved.     
     



Regards,
Satheesh Palanisamy. 


Marked as answer

MJ Malcolm Jack November 4, 2020 10:44 PM UTC

Satheesh when can we expect a fix on nuget?
I was expecting a weekly nuget update on the 3rd, but it seems nothing has been released yet?


NK Nguyen Khoa Lu November 5, 2020 02:00 AM UTC

How it work? 
I was deleted Nuget cache and copy file which you attchment to folder C:\Users\[USERNAME]\.nuget\packages\syncfusion.xamarin.sfpdfviewer\18.3.0.44. But it not working.

What I'm missing anything? 


NK Nguyen Khoa Lu replied to Francisco November 5, 2020 08:57 AM UTC

Yes

I'm working with Xamarin.Forms 4.8.0.1560 and SfPdfViewer 18.3.038, all runs perfectly

Reverte back Xamarin.Forms 4.8.0.1560 and SfPdfViewer 18.3.038 Not working with Release.


AV Ashokkumar Viswanathan Syncfusion Team November 5, 2020 12:54 PM UTC

Hi Francisco, 
 
We have tried to reproduce the issue, ”Application crashes with null reference exception while loading in Xamarin Forms Android Platform” with provided custom NuGet, but its working as expected in our end.    
 
Kindly find the refer the demo sample below,   
 
Satheesh when can we expect a fix on nuget? 
I was expecting a weekly nuget update on the 3rd, but it seems nothing has been released yet? 
 
The fix for the issue, “Application crashes with null reference exception while loading in Xamarin Forms Android Platform” is resolved in 2020 Volume 3 service pack release v(18.3.0.47). Kindly update the mentioned version to resolve the issue. 
 
Regards, 
Ashok Kumar Viswanathan. 



MJ Malcolm Jack November 5, 2020 09:07 PM UTC

confirmed: latest release fixes it! 


AV Ashokkumar Viswanathan Syncfusion Team November 6, 2020 05:49 AM UTC

Hi Malcolm, 
 
Thank you for your confirmation. Please let us know if you need any further assistance on this. 
 
Regards, 
Ashok Kumar Viswanathan. 



DV Dat Vo September 27, 2021 11:43 AM UTC

Hi we are having this issue in 19.2.0.44 again. I noticed this issue seem to be recurring (I see articles in 2018, 2019 and 2020 having the same crash stack trace). Can we please have a fix? At least we want a way so it wouldn't crash the whole app (which is currently impossible).


On our emulator, we have the problem randomly as well though we cannot know exactly what causes it. The same file that crashed would work well next time. I do not know what causes it but here is from user report from Firebase Crashlytics:


```

Fatal Exception: android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object

  at Syncfusion.SfPdfViewer.XForms.Droid.ScrollViewEx.<LoadDocumentWorker_RunWorkerCompleted>b__1152_0 (System.Object _) [0x00017] in <d093d8495fd04c46ab2b856f568b0d43>:0

  at Android.App.SyncContext+<>c__DisplayClass3_0.<Send>b__0 () [0x00000] in <5178e605439148a0b87853e9db2bfd5f>:0

  at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <5178e605439148a0b87853e9db2bfd5f>:0

  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <5178e605439148a0b87853e9db2bfd5f>:0

  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.62(intptr,intptr)

       at mono.java.lang.RunnableImplementor.n_run(RunnableImplementor.java)

       at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)

       at android.os.Handler.handleCallback(Handler.java:873)

       at android.os.Handler.dispatchMessage(Handler.java:99)

       at android.os.Looper.loop(Looper.java:193)

       at android.app.ActivityThread.main(ActivityThread.java:6819)

       at java.lang.reflect.Method.invoke(Method.java)

       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)

       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)

```


It's a NullReferenceException, wouldn't it be easy to find the problem if you have the source code?



VD Vikram Devaraj Syncfusion Team September 28, 2021 08:46 AM UTC

Hi Dat,


We were unable to reproduce the issue, Application crashes with null reference exception while loading  and it is working as expected., Could you please try the below-provided sample and let us know whether we have missed out anything while replicating the issue. 
   
Please find the sample in the below link, which we have used to reproduce the issue in our end  
https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample400932078.zip

Kindly share the following details to analyze more on this issue and assist you with the better solution   
 
1.Simple sample/ modify the above sample to reproduce the issue.   
2.Device info. 
3.Pdf document
4. Replication video/procedure to reproduce the issue. 
 

Regards,
Vikram
 


Loader.
Up arrow icon