如何解决未设置为对象实例的错误对象引用。 [英] How to solve error object reference not set to an instance of an object.

查看:589
本文介绍了如何解决未设置为对象实例的错误对象引用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了

 对象参考而不是 set 的问题 object 的实例。



我使用new关键字作为基本抽象类,但没有运气。我需要帮助xD



我尝试过:



基本摘要具有新StatList的类:

  public   abstract   class 生物:VisibleObject 
{
public StatsList StatsList;
private int state =( int )CreatureState.ACTIVE;
public Creature( int objId,SpawnTemplate spawnTemplate,VisibleObjectTemplate objectTemplate,Vector5D Position): base (objId,spawnTemplate,objectTemplate,Position)
{
StatsList = new StatsList( ); //
}

}





并且类播放器扩展了Creature类:

< pre lang =C#> public class 玩家:生物,IDisposable
{
public TemplateCharacter Template;

public 播放器(TemplateCharacter模板): base (template.ID,< span class =code-keyword> null , null ,template.Position)
{
Template = template;
StatsList = new StatsList( this );
}
}









现在来自这里的错误:

float [] stats = CharacterBaseStats.Stats [(int)c.Template.Class]; //

 对象引用 set 对象的实例。









  public   class  StatsList:Dictionary< Stat,float> 
{
生物物品;

字典< Stat,float> InitialStats = new Dictionary< Stat,float>();

int 健康;
int Mana;
ushort DivinePoints;
ushort FlyPoints;

public StatsList(Creature obj)
{
this .obj = obj;

InitializeStats();
}

public void InitializeStats()
{
if (obj Player)
{
Player c = obj as Player;
float [] stats = CharacterBaseStats.Stats [( int )c.Template.Class] ; // 此处出错

InitializeStat(Stat.MaxHp, 10000 );
}
其他
{
// < span class =code-comment> GameObjects

InitializeStat(Stat.Speed, 6 );
InitializeStat(Stat.FlySpeed, 9 );
}
}
}





当我尝试删除

 StatsList =  new  StatsList( this ); 

在Creature中抽象类此错误离开了。但我不知道它的正常或只是一个坏的解决方案?

解决方案

对象引用未设置为对象的实例。



意味着某些东西 null 调试你的应用程序并将鼠标移到有问题的行上并观察工具提示中的值 null s并从那里开始向后工作。


I got a problem with

Object reference not set to an instance of an object.


im using new keyword for base abstract class but no luck. I need help xD

What I have tried:

Base abstract class with new StatList:

public abstract class Creature : VisibleObject
    {
        public StatsList StatsList;
        private int state = (int)CreatureState.ACTIVE;
        public Creature(int objId, SpawnTemplate spawnTemplate, VisibleObjectTemplate objectTemplate, Vector5D Position) : base(objId, spawnTemplate, objectTemplate, Position)
        {
            StatsList = new StatsList(this);//
        }
        
    }



And class Player extends Creature class:

public class Player : Creature, IDisposable
    {
        public TemplateCharacter Template;

        public Player(TemplateCharacter template) : base(template.ID, null, null, template.Position)
        {
            Template = template;
            StatsList = new StatsList(this);
        }
    }





Now error from here:
float[] stats = CharacterBaseStats.Stats[(int)c.Template.Class];//

Object reference not set to an instance of an object.





public class StatsList : Dictionary<Stat, float>
    {
        Creature obj;

        Dictionary<Stat, float> InitialStats = new Dictionary<Stat, float>();

        int Health;
        int Mana;
        ushort DivinePoints;
        ushort FlyPoints;

        public StatsList(Creature obj)
        {
            this.obj = obj;

            InitializeStats();
        }

        public void InitializeStats()
        {
            if (obj is Player)
            {
                Player c = obj as Player;
                float[] stats = CharacterBaseStats.Stats[(int)c.Template.Class];// Error here

                InitializeStat(Stat.MaxHp, 10000);
            }
            else
            {
                // GameObjects
                InitializeStat(Stat.Speed, 6);
                InitializeStat(Stat.FlySpeed, 9);
            }
        }
    }



When i try to remove

StatsList = new StatsList(this);

in Creature abstract class this error is gone. but i dont know its normally or just a bad solution?

解决方案

Object reference not set to an instance of an object.


Means that something is null debug your application and move the mouse over the offending line and watch the values in the tooltip for nulls and work backward from there.


这篇关于如何解决未设置为对象实例的错误对象引用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆