简体   繁体   English

公共类成员的NullReferenceException

[英]NullReferenceException for Public class member

The following code throws "An unhandled exception of type 'System.NullReferenceException' occurred in..." when the GetDepAirport() method is called. 调用GetDepAirport()方法时,以下代码引发“在...中发生了'System.NullReferenceException'类型的未处理异常”。

    public WYPT GetDepAirport()
    {
        Console.WriteLine("Retrieving Airport in GetDepApt()");
        Console.WriteLine("Departure Airport is {0}", Dep.Ident);
        return Dep;
    }

    public void SetDepAirport(String ident)
    {
        Console.WriteLine("Setting Airport with ident {0}, ident");
        Dep = FetchDBAirport(ident);
        Console.WriteLine("WYPT Dep is set to {0}", Dep.Ident);
    }

The output is: 输出为:

Setting Airport with ident KABQ
WYPT Dep is set to KABQ
Retrieving Airport in GetDepApt()

Followed by a slough of exceptions. 其次是一些例外。 I cannot figure out why Dep (declared public) is getting returned as null when called from the GetDepAirport() method. 我无法弄清楚为什么从GetDepAirport()方法调用Dep (声明为public)时会返回null Both methods are being called from within a separate method in the same class. 这两种方法都是从同一类的单独方法中调用的。

Declaration is at the top of the class: 声明是课堂上的佼佼者:

class FlightPlan
{
   //Init Pg.1 data
    public WYPT Dep, Dest, Altn;

The call to set is from a different class: set的调用来自另一个类:

FlightPlan FPlan = new FlightPlan(); 
FPlan.SetDepAirport(Dep);

The call to Get comes later: 稍后致电Get:

    public void GetFPlan()
    {
        for (int i = 0; i < 14; i++)
        {
            Waypoint[i] = new WYPT();
        }
        Waypoint[0] = GetDepAirport();
        Waypoint[1] = DISCON;

FetchDBAirport method in same FlightPlan class 同一FlightPlan类中的FetchDBAirport方法

private WYPT FetchDBAirport(String airport)
    {
        WYPT Airport = new WYPT();

        String databasepath = "C:\\Users\\Family\\documents\\visual studio 2012\\Projects\\FMST\\FMST\\Database\\NavData.mdf";
        SqlConnection myConnection = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=" + databasepath + ";Integrated Security=True");
        try
        {
            myConnection.Open();
            SqlCommand cmd = myConnection.CreateCommand();
            cmd.CommandText = "SELECT * FROM Airports WHERE Ident='" + airport + "';";
            SqlDataReader rdr = cmd.ExecuteReader();
            rdr.Read();

            Airport.Ident = (String)rdr.GetValue(0);
            Airport.Lat = (decimal)rdr.GetValue(2);
            Airport.Lon = (decimal)rdr.GetValue(3);
            Airport.Elev = (decimal)rdr.GetValue(4);

            myConnection.Close();
            return Airport;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString() + "Happy Face");
            String ErrorMsg = "NOT FOUND";
            Airport.Ident = ErrorMsg;
            return Airport;
        }

The only part of the code that could return a NullReferenceException in GetDepAirport is Dep.Ident . 可以在GetDepAirport中返回NullReferenceException的代码的唯一部分是Dep.Ident Either Dep or Ident could be null. DepIdent可以为null。 If you add a null check for both of those then the method will run fine. 如果为这两个都添加了空检查,则该方法将运行良好。

As for why either of those is null, it could be because: 至于为什么这些都不为空,可能是因为:

  • You have not called SetDepAirport before calling GetDepAirport 您尚未致电SetDepAirport然后再致电GetDepAirport
  • FetchDBAirport is either returning null, or returning a Dep instance where Ident is null. FetchDBAirport返回的是null,或者返回Ident为null的Dep实例。
  • The code that called GetDepAirport is altering the value of Dep somewhere else 调用GetDepAirport的代码正在其他地方更改Dep的值
  • Dep is not storing the value correctly - so perhaps a problem in the property definition Dep未正确存储值-因此属性定义中可能存在问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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