Exception has occurred: CLR/System.NullReferenceException due to DashboardTabbedPageHandler

Sreenivasan, Sreejith 865 Reputation points

My home page is a flyout page with a tabbed page as the detail and a menu pop up page as the flyout. My problem is I have a handler for tabbed page and while using it I am getting below exception:

Exception has occurred: CLR/System.NotImplementedException

  •   An unhandled exception of type 'System.NotImplementedException' occurred in Microsoft.iOS.dll: 'The method or operation is not implemented.'    at UIKit.UIApplication.xamarin_UIApplicationMain(Int32 argc, IntPtr argv, IntPtr principalClassName, IntPtr delegateClassName, IntPtr* gchandle)   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)   at Inventiva.Program.Main(String[] args) in /Users/sreejith.sreenivasan/Downloads/inv-hnas337-mobile-patient/Inventiva/Platforms/iOS/Program.cs:line 13

Below is my DashboardTabbedPageHandler.cs:

using System;
using UIKit;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Controls;
using Xamarin.Forms.Clinical6.Views;

namespace MAUI.Clinical6.Platforms.iOS.Controls
{
 public class DashboardTabbedPageHandler : TabbedViewHandler
 {
 protected override void ConnectHandler(UIView platformView)
 {
 base.ConnectHandler(platformView);

 if (VirtualView is DashboardTabPage dashboardTabPage)
 {
 dashboardTabPage.DashboardBadgeEvent += OnDashboardBadgeEvent;
 }
 }

 protected override void DisconnectHandler(UIView platformView)
 {
 if (VirtualView is DashboardTabPage dashboardTabPage)
 {
 dashboardTabPage.DashboardBadgeEvent -= OnDashboardBadgeEvent;
 }

 base.DisconnectHandler(platformView);
 }

 private void OnDashboardBadgeEvent(object sender, int count)
 {
 UpdateBadge(count);
 }

 private void UpdateBadge(int count)
 {
 try
 {
 // Get the UITabBarController from the root view hierarchy
 UITabBarController tabBarController = null;

 var window = UIApplication.SharedApplication.KeyWindow;
 var rootController = window.RootViewController;

 if (rootController is UITabBarController tbc)
 {
 tabBarController = tbc;
 }
 else if (rootController is UINavigationController nav && nav.ViewControllers.Length > 0)
 {
 tabBarController = nav.ViewControllers[0] as UITabBarController;
 }

 if (tabBarController?.TabBar?.Items == null || tabBarController.TabBar.Items.Length == 0)
 return;

 var tabBarItem = tabBarController.TabBar.Items[0];

 if (count == 0)
 {
 RemoveBadge(tabBarItem);
 return;
 }

 tabBarItem.BadgeColor = UIColor.FromRGB(190, 23, 23); // #BE1717
 tabBarItem.BadgeValue = count.ToString();
 }
 catch (Exception ex)
 {
 Console.WriteLine(ex);
 }
 }

 private void RemoveBadge(UITabBarItem tabBarItem)
 {
 if (tabBarItem == null)
 return;

 tabBarItem.BadgeColor = UIColor.Clear;
 tabBarItem.BadgeValue = string.Empty;
 }
 }
}

I tried to remove this handler by commenting its registration in MAUIProgram.cs, but still getting another exception.

//Commecnted below
handlers.AddHandler(typeof(DashboardTabPage), typeof(DashboardTabbedPageHandler)); 

Exception getting:

Exception has occurred: CLR/System.NullReferenceException

  •   An unhandled exception of type 'System.NullReferenceException' occurred in Microsoft.iOS.dll: 'Object reference not set to an instance of an object.'    at UIKit.UIApplication.xamarin_UIApplicationMain(Int32 argc, IntPtr argv, IntPtr principalClassName, IntPtr delegateClassName, IntPtr* gchandle)   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)   at Inventiva.Program.Main(String[] args) in /Users/sreejith.sreenivasan/Downloads/inv-hnas337-mobile-patient/Inventiva/Platforms/iOS/Program.cs:line 13

In tabs the firsttab is a list of alerts and I want to show the unread alert count on it as a badge and that logic is inside of this handler.

  1. Viorel 127K Reputation points

Sign in to comment