MVC控制器抛出空引用错误

时间:2016-07-05 20:15:36

标签: c# asp.net-mvc

在我的数据库上下文类中,我有像这样的代码

 private readonly ApplicationDbContext _context;

    public IActionResult Index()
    {
        return View( _context.JobCategory.ToList());
    }

在我的控制器上

function count(){

    function ninjas (name,dmg,dmgrate,dmggrow,speed,fury) {
        this.name = name;
        this.dmg = dmg;
        this.dmgrate = dmgrate;
        this.dmggrow = dmggrow;
        this.speed = speed;
        this.fury = fury;
    }
    var name = [];
    var dmg = [];
    var dmgrate = [];
    var dmggrow = [];
    var speed = [];
    var fury = [];
    var ninja = [];
    for(var i = 0; i <5; i++){
      name[name.length] = document.getElementById("ninja" + (i +1)).value;
        dmg[dmg.length] = document.getElementById("dmg" + (i +1)).value; 
      dmgrate[dmgrate.length] = document.getElementById("dmgrate" + (i +1)).value; 
        dmggrow[dmggrow.length] = document.getElementById("dmggrow" + (i +1)).value; 
        speed[speed.length] = document.getElementById("speed" + (i +1)).value; 
        fury[fury.length] = 50;
        ninja[i] = new ninjas(name[i],dmg[i],dmgrate[i],dmggrow[i],speed[i],fury[i]);
    }

    ninja.sort(function(a, b){return b.speed - a.speed}); 

    var totaldmg;
    var damagerate;
    var damagegrow; 
    var furydmg;

    for(var a = 0; a < 6; a++){ // 6 fight 
        for(var b = 0; b < 5; b++){ // 5 ninjas
            if(ninja[b].name == "Kabuto"){
                 if(ninja[b].fury == 100){
                    damagerate = ninja[b].dmg / 100 * ninja[b].dmgrate;
                    damagegrow = damagerate / 100 * ninja[b].dmggrow;
                    furydmg = damagegrow + (damagegrow / 100) * ((ninja[b].fury - 100) / 0.25);
                    totaldmg += furydmg;
                    for(var c = 0; c < 5; c++){ // add fury each ninja by 25
                        ninja[c].fury +=25;
                    }
                     ninja[b].fury -= 25;
                     ninja[b].fury +=100;
                 }else if(ninja[b].fury > 100){
                    damagerate = ninja[b].dmg / 100 * ninja[b].dmgrate;
                    damagegrow = damagerate / 100 * ninja[b].dmggrow;
                    totaldmg += damagegrow;
                    for(var c = 0; c < 5; c++){// add fury each ninja by 25
                        ninja[c].fury +=25;
                    }
                     ninja[b].fury -= 25;
                     ninja[b].fury +=100;
                 }else {
                    damagerate = ninja[b].dmg / 100 * ninja[b].dmgrate;
                    totaldmg += damagerate;
                    ninja[b].fury += 50;
                 }
            } else {
                if(ninja[b].fury == 100){
                    damagerate = ninja[b].dmg / 100 * ninja[b].dmgrate;
                    damagegrow = damagerate / 100 * ninja[b].dmggrow;
                    totaldmg += damagegrow;
                    ninja[b].fury = 0;
                }else if(ninja[b].fury > 100){
                    damagerate = ninja[b].dmg / 100 * ninja[b].dmgrate;
                    damagegrow = damagerate / 100 * ninja[b].dmggrow;
                    furydmg = damagegrow + (damagegrow / 100) * ((ninja[b].fury - 100) / 0.25);
                    totaldmg += furydmg;
                    ninja[b].fury = 0;
                }else {
                    damagerate = ninja[b].dmg / 100 * ninja[b].dmgrate;
                    totaldmg += damagerate;
                    ninja[b].fury += 50;
                }
            }
        }
    }

   document.getElementById("result").innerHTML = totaldmg;
};

但是在我的控制器类上我得到一个像这样的错误

  

WebApplication10.dll中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理

附加信息:未将对象引用设置为对象的实例。 谁能指出我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的_context变量似乎从未初始化,所以如何访问其JobCategory属性?

运行时抛出NullReferenceException 总是意味着同样的事情:您正在尝试使用引用。引用未初始化(或者已初始化,但不再初始化)。

这意味着引用为null,您无法通过null引用访问成员。最简单的情况:

string foo = null;
foo.ToUpper();

这将在第二行引发NullReferenceException,因为您无法在指向ToUpper()的{​​{1}}引用上调用实例方法string。< / p>

首先输出你的变量:

null