我在下面的这行给了我标题错误。
dtDetail = SQLQuery.getRequestDetails(reqNo, vendorcode);
但我测试了它,dtDetail
不为空,reqNo
不为空,vendorCode
不为空,我不在乎。
logger.Debug("request#: " + reqNo);
logger.Debug("vendor code: " + vendorcode);
System.Data.DataTable dtDetail = new System.Data.DataTable();
if (dtDetail == null)
logger.Debug("dtDetail is null.");
if (SQLQuery.getRequestDetails(reqNo, vendorcode) == null)
logger.Debug("SQLQuery.getRequestDetails(reqNo, vendorcode) is null.");
dtDetail = SQLQuery.getRequestDetails(reqNo, vendorcode);
我使用来自SQLQuery.getRequestDetails(reqNo, vendorcode)
的{{1}}和reqNo
来测试vendorCode
,其中没有例外。
所有测试均在我的本地PC上完成,但连接到生产数据库。 Windows 7 Pro Service Pack 1。
给我错误的是生产环境,Windows XP Pro 2002 Service Pack 3。
下面是SQLQuery类。
logger.Debug
我甚至去写了一个新函数,但它在生产环境中也给了我同样的错误。
public static class SQLQuery
{
private static ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static DataTable getRequestDetails(string reqNo, string vendorcode)
{
DataSet ds = new DataSet();
string strSQL = string.Empty;
ATSSPCommon.SQLDB myDB = null;
string ret = string.Empty;
try
{
string strConnectionString = Config.DrawingConnString();
myDB = new ATSSPCommon.SQLDB(strConnectionString);
strSQL = " SELECT * FROM vw_drawing_req_status WHERE regno = '" + reqNo + "'" + " AND vendorcode = '" + vendorcode + "'";
ds = myDB.GetDataSet(strSQL);
}
catch (Exception ex)
{
logger.Error("request#: " + reqNo + ", vendor code: " + vendorcode, ex);
throw ex;
}
finally
{
}
if (ds.Tables[0].Rows.Count > 0)
return ds.Tables[0];
else
return null;
}
public static DataTable getRFQDetail(string reqNo, string vendorcode)
{
DataSet ds;
string strSQL = string.Empty;
ATSSPCommon.SQLDB myDB = null;
string ret = string.Empty;
try
{
string strConnectionString = Config.DrawingConnString();
myDB = new ATSSPCommon.SQLDB(strConnectionString);
strSQL = " SELECT * FROM t_RFQ_info WHERE regno = '" + reqNo + "'" +
"AND vendor_code = '" + vendorcode + "'";
ds = myDB.GetDataSet(strSQL);
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
if (ds.Tables[0].Rows.Count > 0)
return ds.Tables[0];
else
return null;
}
public static DataTable getVendorEmailAddress(string comp_code, string vendorcode)
{
DataSet ds;
string strSQL = string.Empty;
ATSSPCommon.SQLDB myDB = null;
string ret = string.Empty;
try
{
string strConnectionString = Config.DrawingConnString();
myDB = new ATSSPCommon.SQLDB(strConnectionString);
strSQL = " SELECT * FROM t_vendor_email WHERE comp_code = '" + comp_code + "'" +
"AND vendor = '" + vendorcode + "' AND status = 1";
ds = myDB.GetDataSet(strSQL);
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
return ds.Tables[0];
}
public static string getModuleNumber(string reqNo)
{
DataSet ds;
string strSQL = string.Empty;
ATSSPCommon.SQLDB myDB = null;
string ret = string.Empty;
try
{
string strConnectionString = Config.DrawingConnString();
myDB = new ATSSPCommon.SQLDB(strConnectionString);
strSQL = " SELECT moduleno FROM subconjobQ WHERE regno = '" + reqNo + "'";
ret = myDB.GetOneValue(strSQL).ToString();
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
return ret;
}
public static string getPlant(string reqNo)
{
DataSet ds;
string strSQL = string.Empty;
ATSSPCommon.SQLDB myDB = null;
string ret = string.Empty;
try
{
string strConnectionString = Config.DrawingConnString();
myDB = new ATSSPCommon.SQLDB(strConnectionString);
strSQL = " SELECT plant FROM subconjobQ WHERE regno = '" + reqNo + "'";
ret = myDB.GetOneValue(strSQL).ToString();
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
return ret;
}
public static string getVendorCurrency(string plant, string vendorcode)
{
DataSet ds;
string strSQL = string.Empty;
ATSSPCommon.SQLDB myDB = null;
string ret = string.Empty;
try
{
string strConnectionString = Config.DrawingConnString();
myDB = new ATSSPCommon.SQLDB(strConnectionString);
strSQL = " SELECT currency FROM t_vendor_info WHERE plant = '" + plant + "'" +
"AND vendor = '" + vendorcode + "' AND status = 1";
ret = myDB.GetOneValue(strSQL).ToString();
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
return ret;
}
public static void updateRFQTable(string regno, string vendor, string partno, string currency, double procCost, double rawCost, double treatCost, double unitPrice, string leadTime)
{
string strSQL = string.Empty;
ATSSPCommon.SQLDB myDB = null;
string ret = string.Empty;
try
{
string strConnectionString = Config.DrawingConnString();
myDB = new ATSSPCommon.SQLDB(strConnectionString);
strSQL = " UPDATE t_RFQ_info SET " +
"currency = '" + currency + "', " +
"process_cost = " + procCost + ", " +
"rawmat_cost = " + rawCost + ", " +
"treatment_cost = " + treatCost + ", " +
"unit_price = " + unitPrice + ", " +
"lead_time = '" + leadTime.Trim() + "', " +
"status = 1 " +
"WHERE Regno = '" + regno + "' AND " +
"vendor_code = '" + vendor + "' AND " +
"partno = '" + partno + "'";
myDB.ExecuteSQL(strSQL);
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
}
记录错误消息:
2017-01-18 15:30:53,303 [1] ERROR DrawingRequestEmail.Util [(null)] - ReceiveEmail System.NullReferenceException:未将对象引用设置为对象的实例。 在D:\ TFS \ SERVER \ Logistics \ Main \ MM \ DrawingRequest \ DrawingRequestEmail \ DrawingRequestEmail \ Email.cs中的DrawingRequestEmail.Email.ProcessEmailFromSubconSystem(MailItem消息):第155行 在D:\ TFS \ SERVER \ Logistics \ Main \ MM \ DrawingRequest \ DrawingRequestEmail \ DrawingRequestEmail \ Util.cs中的DrawingRequestEmail.Util.ReceiveEmail():第123行
Util.cs:第123行
class RFQHandler
{
public static DataTable GetRequestDetail(string requestNumber, string vendorCode)
{
string connectionString = Config.DrawingConnString();
SqlConnection sqlConnection = new SqlConnection(connectionString);
string commandText = "SELECT * FROM vw_drawing_req_status WHERE regno = @requestNumber AND vendorcode = @vendorCode";
SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection);
sqlCommand.Parameters.Add("@requestNumber", SqlDbType.Char, 12).Value = requestNumber;
sqlCommand.Parameters.Add("@vendorCode", SqlDbType.VarChar, 25).Value = vendorCode;
DataTable requestDetail = new DataTable();
using (sqlConnection)
using (sqlCommand)
{
sqlConnection.Open();
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
requestDetail.Load(sqlDataReader);
}
}
return requestDetail;
}
}
Email.cs:第155行
ret = email.ProcessEmailFromSubconSystem(oMessage);
答案 0 :(得分:0)
可能的空引用错误,我可以看到 -
DataSet ds = new DataSet();
string strSQL = string.Empty;
ATSSPCommon.SQLDB myDB = null;
string ret = string.Empty;
try
{
........
ds = myDB.GetDataSet(strSQL);
}
catch (Exception ex)
{
....
}
finally
{
}
if (ds.Tables[0].Rows.Count > 0) // this could be the error
return ds.Tables[0];
else
return null;
即使查询执行,也无法保证DataTable
内部至少有一个DataSet
,DataSet
本身也可能为空。尝试添加这样的空检查 -
if (ds != null && ds.Tables.Any() && ds.Tables[0].Rows.Count > 0)
return ds.Tables[0];
else
return null;
在此声明之后也没有空检查 -
myDB = new ATSSPCommon.SQLDB(strConnectionString);
myDB
也可能为空。
更新:对于.net 2.0,请在评论中提及ds.Tables.Count > 0
而不是ds.Tables.Any()
。
答案 1 :(得分:0)
我也得到了一个无意义的NULL异常,当我使用“Set Next Statement”(Ctrl + Shift + F10)时,我将其追溯到调试器跳过的变量声明。
例如,我的代码如下所示:
if (false)
{
classTypeObject myObject;
myObject = someValue;
}
如果我在条件上设置断点然后移动下一个语句在开括号上运行,当设置myObject
的值时,我得到一个空引用异常,因为调试器必须跳过声明。 / p>
但是,如果我将代码更改为这样并执行相同的调试步骤则没有问题:
classTypeObject myObject;
if (false)
{
myObject = someValue;
}
P.S。显然这是伪代码,你永远不想硬编码if (false)
。