3
votes

I am trying to make so when I press a row on my UITableView, it will send some data to another UIViewController and make a seque.

However when testing it out, it gives me:

System.NullReferenceException has been thrown Object reference not set to an instance of an object

The error seems to show here:

viewController.NavigationController.PushViewController (detail, true);

Here is my "RowSelected"

public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
    Console.WriteLine ("Row: " + tableItems [indexPath.Row].Heading);

    //new UIAlertView("Row Selected", tableItems[indexPath.Row].Heading, null, "OK", null).Show();
    tableView.DeselectRow (indexPath, true);

    // Specially for Storyboard !!
    var detail = viewController.Storyboard.InstantiateViewController("detail") as HolsteinItemViewController;
    detail.Title = tableItems[indexPath.Row].Heading;
    detail.LoadUrl (tableItems[indexPath.Row].SubHeading);
    viewController.NavigationController.PushViewController (detail, true);
}

Exception caught:

System.NullReferenceException: Object reference not set to an instance of an object
  at Holstein.TableSource.RowSelected (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x0008e] in /Users/Emil/Projects/Holstein/Holstein/Code/TableSource.cs:41
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at Holstein.Application.Main (System.String[] args) [0x00008] in /Users/Emil/Projects/Holstein/Holstein/Main.cs:16

TableSource.cs line 46

viewController.NavigationController.PushViewController (detail, true);

Main.cs line 16

UIApplication.Main (args, null, "AppDelegate");
1
Are both tableItems[indexPath.Row].Heading and Subheading not null? - user1646737
Both should be set, as they both show up in the cell - Emil Elkjær
Is there some other property of detail that is expected but not defined? - user1646737
If the null reference exception is thrown when you are trying to push the controller, it's most likely that the NavigationController property of "viewController" is null. Is that controller actually pushed on a navigation controller? Is it visible at the time, or just instantiated? - Dimitris Tavlikos
could you share the complete stack trace ? - Stephane Delcroix

1 Answers

4
votes

Your viewController.NavigationController property is null. According to iOS UIViewController reference here

If the receiver or one of its ancestors is a child of a navigation controller, this property contains the owning navigation controller. This property is nil if the view controller is not embedded inside a navigation controller.

Apparently, that view controller is not part of a UINavigationController stack.