【问题标题】:C# MVC Class Object reference not set to an instance of an objectC# MVC 类对象引用未设置为对象的实例
【发布时间】:2014-07-06 00:23:12
【问题描述】:

谁能帮我解决我在课堂上遇到的问题?

我有一个地址类:

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

我有一个路由类:

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

在我的控制器中,我设置了一些类似这样的虚拟信息:

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

但是当我运行项目时,它会落在 myroute.from.addressDescription = from;表示对象引用未设置为对象实例的行。

我看不出我做错了什么。有人可以帮忙吗?

谢谢

特雷夫

【问题讨论】:

  • 正确答案如下,但您应该考虑将路由重命名为其他名称。 Route 在 MVC 中意味着什么。

标签: c# .net asp.net-mvc


【解决方案1】:

您需要创建Address 的新实例并将其分配给fromto

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

【讨论】:

  • @TrevorDaniel 你知道你可以接受答案吗?投票按钮下方有一个复选标记。这样您就可以将问题标记为已回答。我注意到您尚未接受任何问题。帮助其他人不必阅读“已解决问题”的问答。 PS +1 达伦让我保持清醒。:)
  • @DarrenDavies 回到栈上 ;)
  • @mattytommo 我是个瘾君子 ;)
  • @DarrenDavies +1 给你,你很快就会达到 10k ;)
【解决方案2】:

我可以建议使用构造函数来初始化 from 和 to 字段吗?否则每次使用 Route 类时都必须新建对象。

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

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

这样您就可以使用您提供的代码:

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

【讨论】:

  • 不,它不应该包含引号,因为 from 是一个字符串。他只是将 addressDescription 分配给这个变量。问题在于 from 和 to 没有 Address 的实例。
  • 是的,我意识到,但是将 addressDescription 分配给“from”不会给 OP 正确的输出。
  • 我认为这正是他想要做的,但我们不要试图为他考虑。那我就删了。
  • 如果他想将它分配给字符串文字“from”,为什么他会传入一个包含数据的名为 from 的字符串?
  • 我没有说它有意义,但代码示例就是这样的。
【解决方案3】:

您已创建 Route 实例,但忘记创建 Address 的新实例(对于 fromto):

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

【讨论】:

    【解决方案4】:

    myroute.frommyroute.to 应该是 Address 类的实例。

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

    【讨论】:

      猜你喜欢
      • 2013-07-26
      • 2011-06-21
      • 1970-01-01
      • 2021-09-14
      • 2020-04-13
      • 2013-03-21
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多