简体   繁体   中英

NullReferenceException for Public class member

The following code throws "An unhandled exception of type 'System.NullReferenceException' occurred in..." when the GetDepAirport() method is called.

    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. 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:

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

The call to Get comes later:

    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

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 . Either Dep or Ident could be 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
  • FetchDBAirport is either returning null, or returning a Dep instance where Ident is null.
  • The code that called GetDepAirport is altering the value of Dep somewhere else
  • Dep is not storing the value correctly - so perhaps a problem in the property definition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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