[英]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. Dep
或Ident
可以为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: 至于为什么这些都不为空,可能是因为:
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
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.