简体   繁体   中英

C# MVC Class Object reference not set to an instance of an object

Can anyone help me get around my problem with my class please?

I have an Address class:

public class Address
{
    public string addressDescription { get; set; }
    public string addressNumber { get; set; }
    public string adddressLine1 { get; set; }
    public string adddressLine2 { get; set; }
    public string adddressLine3 { get; set; }
    public string addressPostCode { get; set; }
    public double addressLatitude { get; set; }
    public double addressLongitude { get; set; }
}

And I have a Route Class:

public class Route
{
    public Address from { get; set; }
    public Address to { get; set; }
}

And in my controller i have setup some dummy information like this:

public ActionResult FareCalculator(string from , string to)
    {

        var myroute = new Route();

        myroute.from.addressDescription = from;
        myroute.from.addressLatitude = 51.481581;
        myroute.from.addressLongitude = -3.179090;
        myroute.to.addressDescription = to;
        myroute.to.addressLatitude = 51.507335;
        myroute.to.addressLongitude = -0.127683;

        return View(myroute);
    }

but when i run the project it falls over on the myroute.from.addressDescription = from; line saying Object reference not set to an instance of an object.

I cannot see what I am doing wrong. Can anyone help please?

Thanks

Trev

You need to create a new instance of Address and assign it to from and to :

public ActionResult FareCalculator(string from , string to)
{

    var myroute = new Route();
    myroute.from = new Address(); // new instance
    myroute.from.addressDescription = from;
    myroute.from.addressLatitude = 51.481581;
    myroute.from.addressLongitude = -3.179090;

    myroute.to = new Address(); // new instance
    myroute.to.addressDescription = to;
    myroute.to.addressLatitude = 51.507335;
    myroute.to.addressLongitude = -0.127683;

    return View(myroute);
}

May I suggest to use the constructor to initialize the from and to fields? Otherwise you will have to new the objects every time you use the Route class.

public class Route
{
    public Address from { get; set; }
    public Address to { get; set; }

    public Route()
    {
        from = new Address();
        to = new Address();
    }
}

That way you can use your code as you provided:

    var myroute = new Route();

    myroute.from.addressDescription = from;
    myroute.from.addressLatitude = 51.481581;
    myroute.from.addressLongitude = -3.179090;
    myroute.to.addressDescription = to;
    myroute.to.addressLatitude = 51.507335;
    myroute.to.addressLongitude = -0.127683;

    return View(myroute);

You have created Route instance, but you have forgotten to create new instances of Address (for from and to ):

var myroute = new Route
{
    from = new Address(),
    to = new Address()
};

myroute.from and myroute.to shoud be an instance of Address class.

 public ActionResult FareCalculator(string from , string to)
 {
        var myroute = new Route();

        myroute.from = new Address();
        myroute.from.addressDescription = from;
        myroute.from.addressLatitude = 51.481581;
        myroute.from.addressLongitude = -3.179090;

        myroute.to = new Address();
        myroute.to.addressDescription = to;
        myroute.to.addressLatitude = 51.507335;
        myroute.to.addressLongitude = -0.127683;

        return View(myroute);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM