TargetInvocationException when trying to use BarCharts

I have a class called Child which contains a name and 2 lists for a child

In my Default ViewController I call an instance.

I have another View Controller called StatisticsController which is supposed to graph a bar chart using the child's data.

I'm trying to send the child from the ViewController to StatisticsController. doing this:
` public partial class ViewController : UIViewController
{
string DEFAULT_FED_STRING = "Child last fed at : ";
private string DEFAULT_SLEPT_STRING = "Child slept at : ";
private string error = "Plese enter positive numbers only";
DateTime sleptTime;

    private Child activeChild = new Child();

    //..Viewcontrolelr blah blah blah.
    public override void PrepareForSegue (UIStoryboardSegue segue, 
        NSObject sender)
    {
        base.PrepareForSegue (segue, sender);

        var StatisticsController = segue.DestinationViewController 
            as StatisticsController;

        if (StatisticsController != null) {
            StatisticsController.child = activeChild;
        }
    }
}

`

Right now i'm just trying to graph a generic plot from the sample...

Here's the code for StatisticsController

`using System;
using UIKit;
using CoreGraphics;
using Foundation;
using CoreAnimation;
using BarChart;

//Delete this need it only for the trial
using System.Collections.Generic;

namespace FeedTracker
{

partial class StatisticsController : UIViewController
{

    public Child child;

    const float BarChartTopMargin = 5f;
    const float BarChartBottomMargin = 50f;
    const float BarChartHorizontalMargin = 30f;

    BarChartView barChart = new BarChartView();

    UIImageView imageView;

    public StatisticsController (IntPtr handle) : base (handle)
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        using (var image = UIImage.FromBundle("back.png"))
        {
            imageView = new UIImageView(image);
            imageView.Frame = View.Frame;
            imageView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
            View.AddSubview(imageView);
        }

// barChart = new BarChartView ();
// barChart.MinimumValue = -50000f;
// barChart.MaximumValue = 50000f;
// barChart.BarColor = UIColor.Green;
barChart.BarOffset = 2f;
barChart.BarWidth = 40f;
barChart.BarCaptionInnerColor = UIColor.White;
barChart.BarCaptionInnerShadowColor = UIColor.Black;
barChart.BarCaptionOuterColor = UIColor.Black;
barChart.BarCaptionOuterShadowColor = UIColor.White;

        barChart.BarClick += OnBarClick;
        barChart.Frame = new CGRect (
            BarChartHorizontalMargin, 
            BarChartTopMargin,
            View.Bounds.Width - 2 * BarChartHorizontalMargin,
            View.Bounds.Height - BarChartTopMargin - BarChartBottomMargin);

        View.AddSubview (barChart);


        UIButton randomize = new UIButton ();
        randomize.Frame = new CGRect (
            BarChartHorizontalMargin, 
            barChart.Frame.Bottom + BarChartTopMargin, 
            barChart.Frame.Width,
            BarChartBottomMargin - BarChartTopMargin - BarChartTopMargin);
        randomize.BackgroundColor = UIColor.DarkGray;
        randomize.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
        randomize.SetTitle ("Randomize Data", UIControlState.Normal);
        randomize.TouchUpInside += delegate {
            barChart.ItemsSource = GenerateTestData ();
        };
        View.AddSubview (randomize);


        barChart.ItemsSource = GenerateTestData ();

        //          barChart.AddLevelIndicator (0f, "0");
        //          barChart.AddLevelIndicator (100f, "100");
        //          barChart.AddLevelIndicator (-100f, "-100");

        //          barChart.GridHidden = true;
        //          barChart.LegendHidden = true;
        //          barChart.LevelsHidden = true;
    }

    public List<BarModel> GenerateTestData ()
    {
        var models = new List<BarModel> ();

        var rnd = new Random ((int)DateTime.UtcNow.Ticks);

        for (var i = 0; i < rnd.Next (10) + 5; i += 1) {
            models.Add (new BarModel () { Value = rnd.Next (-5000, 5000), Color = UIColor.Gray, Legend = "1H" });
            models.Add (new BarModel () { Value = rnd.Next (-5000, 5000), Color = UIColor.Brown, Legend = "6H" });
            models.Add (new BarModel () { Value = rnd.Next (-5000, 5000), Color = UIColor.Gray, Legend = "12H" });
            models.Add (new BarModel () { Value = rnd.Next (-5000, 5000), Legend = "24H" });            
            models.Add (new BarModel () { Value = rnd.Next (-5000, 5000), Legend = "32H" });
        }

        return models;
    }

    public override void ViewDidLayoutSubviews ()
    {
        base.ViewDidLayoutSubviews ();

        barChart.Frame = new CGRect (
            BarChartHorizontalMargin, 
            BarChartTopMargin, 
            View.Bounds.Width - 2 * BarChartHorizontalMargin,
            View.Bounds.Height - BarChartTopMargin - BarChartBottomMargin);
    }

    void OnBarClick (object sender, BarClickEventArgs e)
    {
        Console.WriteLine ("bar clicked: name = {0}, value = {1}", e.Bar.Legend, e.Bar.Value);
    }

    public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
    {
        base.DidRotate (fromInterfaceOrientation);
    }

    public override void DidReceiveMemoryWarning ()
    {
        base.DidReceiveMemoryWarning ();
    }

    public override void PrepareForSegue (UIStoryboardSegue segue, 
        NSObject sender)
    {
        base.PrepareForSegue (segue, sender);

    }

}

}
`

The code compiles, but when I try to segue into StatisticsController it throws a TargetInvocationException.

This only happens when I try to use BarCharts.

Prior to this, I was fixing an issue about barChart which is a BarChartView object being null if that is of any help.

here's the raw output of the error.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: Object reference not set to an instance of an object at BarChart.Views.LegendView.InitSubviews () [0x00018] in <filename unknown>:0 at BarChart.Views.LegendView..ctor () [0x00011] in <filename unknown>:0 at BarChart.BarChartView.InitSubviews () [0x00021] in <filename unknown>:0 at BarChart.BarChartView..ctor () [0x00023] in <filename unknown>:0 at FeedTracker.StatisticsController..ctor (IntPtr handle) [0x00001] in /Users/vinay/Projects/FeedTracker/FeedTracker/StatisticsController.cs:25 at at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&) at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/1977/2c66d2fe/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:535 --- End of inner exception stack trace --- at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00016] in /Users/builder/data/lanes/1977/2c66d2fe/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:541 at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00095] in /Users/builder/data/lanes/1977/2c66d2fe/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:526 at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in /Users/builder/data/lanes/1977/2c66d2fe/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:554 at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in /Users/builder/data/lanes/1977/2c66d2fe/source/mono/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:62 at ObjCRuntime.Runtime.ConstructNSObject[UIViewController] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x0003e] in /Users/builder/data/lanes/1977/2c66d2fe/source/maccore/src/ObjCRuntime/Runtime.cs:685 at ObjCRuntime.Runtime.GetNSObject[UIViewController] (IntPtr ptr) [0x000c0] in /Users/builder/data/lanes/1977/2c66d2fe/source/maccore/src/ObjCRuntime/Runtime.cs:795 at UIKit.UIStoryboardSegue.get_DestinationViewController () [0x00010] in /Users/builder/data/lanes/1977/2c66d2fe/source/maccore/src/build/ios/native/UIKit/UIStoryboardSegue.g.cs:149 at FeedTracker.ViewController.PrepareForSegue (UIKit.UIStoryboardSegue segue, Foundation.NSObject sender) [0x0000a] in /Users/vinay/Projects/FeedTracker/FeedTracker/ViewController.cs:112 at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/1977/2c66d2fe/source/maccore/src/UIKit/UIApplication.cs:63 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/1977/2c66d2fe/source/maccore/src/UIKit/UIApplication.cs:47 at FeedTracker.Application.Main (System.String[] args) [0x00008] in /Users/vinay/Projects/FeedTracker/FeedTracker/Main.cs:12

Hunches

  • I think it's something with the way i'm sending and recieveing children. Is this how you do it? Am I doing that wrong?
  • It could be possible that barChart is not initialized properly, but if that's the case then why would it work with the sample and not here...it's literally the same code.

Posts

  • FardinFardin USMember

    Hi @VinayYuvashankar
    Any progress on this? My project is almost finished but pending for this one bug :neutral:

  • VinayYuvashankarVinayYuvashankar CAMember

    Oh right, I just fixed it by making a clean build. It must have been some funny business when you compiled it.

    Try making a clean build. If not, I'm not sure.

  • FardinFardin USMember

    For anybody having the same problem, "SyncFusion" Xamarin controls are also an option. Much better.

Sign In or Register to comment.