3

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

1
  • The correct answers are below, but you should consider renaming route to something else. Route means something in MVC. Commented Feb 17, 2013 at 14:24

4 Answers 4

3

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

@TrevorDaniel do you know that you can accept answers? There is a checkmark thingy next below the vote buttons. That way you can mark the question as answered. I noted that you did not accept any questions yet. Helps others from not having to read the Q&A for "solved questions". PS +1 for Darren for keeping me awake.:)
@DarrenDavies back on the stack ;)
@mattytommo I'm an addict ;)
@DarrenDavies +1 to you, you'll be 10k in no time ;)
2

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);

6 Comments

no it shouldn't contain quotes as from is a string. He is simply assigning addressDescription to this variable. The problem lies with that from and to had no instance of Address.
yeah I realise that but assigning addressDescription to "from" would not give the OP the correct output.
I think that's exactly what he's trying to do, but lets not try to think for him. I'll remove the addition then.
why would he pass in a string called from containing the data if he wanted to assign it to the string literal "from"?
I didn't say it was meaningful, but that's what the code example looks like.
|
1

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()
};

Comments

0

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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.