question

EmonHaque-1485 avatar image
0 Votes"
EmonHaque-1485 asked EmonHaque-1485 edited

Why do I get null reference exception in this polymorphism?

For the three Report Views (Plot, Space, Tenant) I've used two abstract base classes, one for the view, LedgerView, and the other for the view model, ReportBase. In the ReportBase, I've an abstract property selectionView, which is bound to the ComboBox like control, and when I change selection in that control, it resets the Id property of the ReportBase and in the setter of Id property, I call updateReportables, that fetches data from database.

Reportables is a property of ReportBase and it points to a private field, reserved, of the ReportBase. Entries of that Reportables/reserved are displayed in the ListBox. ReportPlotVM, ReportSpaceVM and ReportTenantVM are subclasses of ReporBase and, at this moment, they just set the view for the selectionView property of ReportBase.

On the top-right corner, I've the refresh button and on click, a function, refreshReport, of ReportBase gets called and there in the first line, I've a LINQ and it throws a null reference exception! So reserved is null although, in Output, I see there are 4 items in the reserved:

98700-1.gif

nothing else touches the reserved other than refreshReport and updateReportables functions.

For the views, the base LedgerView has an abstract property, viewModel, of type ReportBase and this viewModel property becomes the DataContext for those views. ReportPlot, ReportSpace and ReportTenant are subclasses of LedgerView and they just set the viewModel property of the the LedgerView:

98724-2.gif

In the previous version of this app, I don't have this problem because I haven't use base view, LedgerView, like this and I set DataContext for each of those view separately. Now, as I'm simplifying and updating the app, I want to use the base LedgerView for all of them since their look and functionalities are same.

Why do I get that exception?


dotnet-csharpwindows-wpf
1.gif (2.9 MiB)
2.gif (2.2 MiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Did you view the Details in the Exception popup screen? Why you are getting a null reference exception on an object that doesn't exist in memory at the time of the reference in code you're going to have to debug your code. You can use debug Quickwatch and examine the content of an object and find out was is null or what is inside an object that is null.

Primitive types inside an object are objects too. Everything in .NET is an object derived from System.Object the base object for all .NET objects.

1 Vote 1 ·

@DuaneArnold-0443, looks like for these kind of issues, those debugging outputs don't provide enough information, to inexperienced users, on what's going on! For example, when you get null reference exception in ICollectionView source, it tells, Parameter c is null, what's that? Now I know what does that mean in context of IColllectionView. It'd be helpful if it'd told that We delete CollectionViewSource, in runtime, that doesn't have any reference elsewhere, so checkout!

0 Votes 0 ·

1 Answer

EmonHaque-1485 avatar image
0 Votes"
EmonHaque-1485 answered

The problem was in these lines of my sub views (ReportPlot, ReportSpace, ReportTenant):

 protected override ReportBase viewModel => new ReportPlotVM();
 protected override ReportBase viewModel => new ReportSpaceVM();
 protected override ReportBase viewModel => new ReportTenantVM();

these viewModel references get deleted in runtime! I had similar issue with ICollectionView where CollectioViewSource got deleted so that came to my mind and tried that approach and now it works:

98790-test.gif

so in each of those sub views I've to do this:

 ReportPlotVM vm = new ReportPlotVM();
 protected override ReportBase viewModel => vm;

 ReportSpaceVM vm = new ReportSpaceVM();
 protected override ReportBase viewModel => vm;

 ReportTenantVM vm = new ReportTenantVM();
 protected override ReportBase viewModel => vm;

test.gif (263.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.