Why do I get null reference exception in this polymorphism?

Emon Haque 3,176 Reputation points
2021-05-21T18:54:30.547+00:00

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?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

Accepted answer
  1. Emon Haque 3,176 Reputation points
    2021-05-22T17:29:41.517+00:00

    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;  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful