0

I'm trying to make an app in xamarin forms with EventAggregator and Prism. Till now I have made 3 Event, 2 of them work without problem, the last crash the app bun I don't understand why.

The event is this:

    public class AggiuntaRigaOrdineEvent : PubSubEvent<RigaOrdine>
{
}

RigaOrdine is the model Class. To publish the event I use:

_ea.GetEvent<AggiuntaRigaOrdineEvent>().Publish(Riga);

where

RigaOrdine Riga = new RigaOrdine();

As you can se in the picture taked just before the crash the Riga variabile is full:

enter image description here

So why it give me error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

EDIT: @Haukinger

The Excepion is thrown on the publish line, The stack call show:

0xAA in MokaDroid.ViewModels.NuovoArticoloOrdineClienteViewModel.<get_AggiungiRigaOrdine<get_AggiungiRigaOrdine>b__50_0>d at C:\Users\canap\source\repos\MokaDroid\MokaDroid\MokaDroid\ViewModels\NuovoArticoloOrdineClienteViewModel.cs:193,25 C#

The Subscriber is:

ea.GetEvent<AggiuntaRigaOrdineEvent>().Subscribe(RigaRicevuta);

Also if the app never arrive there. And the _ea is initialize in the constructor:

        public NuovoArticoloOrdineClienteViewModel(IPageDialogService DialogSerices, INavigationService navigationService, IEventAggregator ea)
    {
        _DialogSerices = DialogSerices;
        _navigationService = navigationService;
        _ea = ea;
    }

with IEventAggregator _ea; on the variable initialization on the begin of the viewmodel class.

What I don't understand, is that I have used the same way oter 2 times and it have work like a charm, here not.... and the 'Riga' variable is not NULL as you can see from the picture.

Here you have a list of all the variable and none are null... enter image description here

  • What exactly throws the exception? What does the stacktrace look like? What do the subscribers look like? Is `_ea` actually set to something that's not `null`? – Haukinger Jul 29 '20 at 06:13
  • I have no idea what's in `NuovoArticoloOrdineClienteViewModel.cs` at line 193, I guess you'll have to do the debugging yourself. – Haukinger Jul 29 '20 at 12:16
  • At that line is the publish line – Stefano Santin Jul 29 '20 at 12:23
  • And what is `null` and why? – Haukinger Jul 29 '20 at 12:26
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – thatguy Jul 29 '20 at 12:29
  • That link provide a description to understand declaration and initialization of variable. You get a NullReferenceException when you try to use a variable not initialized or you point at a variable that is null. In my case the _ea variable is initialized, and during the debug I can point with the mouse on all the event (3) created, so that it's not null, and the 'Riga' variable is not nul, as you can see in the picture. My question is : Since the throw is on that line, is any other placevariable that can give me this exception? – Stefano Santin Jul 29 '20 at 13:07

1 Answers1

0

I have thought that the subscription of the event "read" the event when the page where is in is load. Now I have understand that The subscriber of the event read it at the same time. It have give me the error couse the variable of the subscriber was not initialized.