-2

I can't seem to get my class to instantiate. When I run my web application. I'm trying to pass the results of my class into some google charts but can't seem to get the class to instantiate. Any help/links/code would be greatly appreciated.

This is where I seem to be stuck:

DashboardController.cs

using System.Web.Mvc;

namespace AudAPIApp.Controllers
{
    [Authorize]
    public class DashboardController : Controller
    {
        public ActionResult Index()
        {
            GoogleAPI googleReport = new GoogleAPI();
            return View(googleReport);
            //New to MVC not sure if this is how to instantiate my class?
        }
    }
}

I've placed code blocks in my class and my application doesn't run through it.

GoogleApi.cs

namespace AudAPIApp
{ 
    public class GoogleAPI
    {
        public static GetReportsResponse googleRpt;
        static void Main(string[] args)
        {
            try
            {
                   //code that gets all the data. This all works in a console app I made. My challenge is making it work in MVC. 

                    var response = batchRequest.Execute();
                    googleRpt = response;

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Index.cshtml

<script>
    var googleData = [];

    @{ 
        //this is where I get error: "Object reference not set to an instance of an object."
        foreach(var x in GoogleAPI.googleRpt.Reports.First().Data.Rows)
        {
            @:googleData.push([@x.Dimensions, @x.Metrics])
        }
    }
</script>

I know that this shouldn't work either because it is using an un instantiated object.

NucDev
  • 35
  • 8
  • You have posted a *lot* of code here. Please see this article on how to create a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example. – stelioslogothetis May 12 '17 at 21:19
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Quantic May 12 '17 at 21:23
  • All of your methods are `static`. If you want your stuff contained to specific instances, remove the statics. You also have a `Main(args)` which leads me to believe you copied this from a console application. It would be wise to rename `Main` to something more descriptive. There's another issue where your `Index.cshtml` iis using the static class as opposed to the instance you created. – TyCobb May 12 '17 at 21:23
  • Your Main function in `GoogleAPI` is not being called when you create the object. Either move it into a constructor function or put it into an initialisation method that you call manually. – James Buck May 12 '17 at 21:27
  • also, View(param) expects a model object that can be used in the view to attach to properties. It won't run Main, or in fact do anything for you other than pass that object to the razor view (Index.cshtml). You can then use `@model GoogleApi` and reference properties of the `googleReport` object. But as someone else mentioned, everything is static, so this is fairly useless for you right now. – Kolichikov May 12 '17 at 21:41

1 Answers1

0

First, since this looks like a website, don't use Main, because you don't have a console app to hook into that, and don't make it static. Something like this:

In general (and probably overly simplified), if your class only has static things, then you don't need to instantiate it (static = shared between all instances of the class, so creating a new instance won't do anything).

namespace AudAPIApp
{ 
    public class GoogleAPI
    {
        public GetReportsResponse googleRpt;
        public void GenerateReport()
        {
            try
            {
                   //code that gets all the data. This all works in a console app I made. My challenge is making it work in MVC. 
                var response = batchRequest.Execute();
                googleRpt = response;

            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

}

Next, change your controller to create that object, call the generate function and pass it as the model

using System.Web.Mvc;

namespace AudAPIApp.Controllers
{
    [Authorize]
    public class DashboardController : Controller
    {
        public ActionResult Index()
        {
            GoogleAPI googleReport = new GoogleAPI();
            googleReport.GenerateReport();
            return View(googleReport);
            //New to MVC not sure if this is how to instantiate my class?
        }
    }
}

You've now passed an instance of GoogleAPI called googleReport to your view, and googleReport.googleRpt is populated with your report.

//Add this to the top of your cshtml file
@model AudAPIApp.GoogleAPI

<script>
    var googleData = [];

    @{ 
        //this is where I get error: "Object reference not set to an instance of an object."
        foreach(var x in Model.googleRpt.Reports.First().Data.Rows)
        {
            @:googleData.push([@x.Dimensions, @x.Metrics])
        }
    }
</script>

You can now get your report as a property on the Model object which is really your GoogleAPI object you created in the controller.

Kolichikov
  • 2,944
  • 31
  • 46