2017-01-18 6 views
1

私は私のタイトルに私にエラーを与える以下の行があります。NullReferenceExceptionしかしヌルがありません

dtDetail = SQLQuery.getRequestDetails(reqNo, vendorcode); 

しかし、私はそれをテストし、dtDetailvendorCodeがnullでなく、私はアイデアのうち午前、reqNoがnullではない、nullではありません。

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); 

は私が logger.Debugから取得 reqNovendorCodeを使用して SQLQuery.getRequestDetails(reqNo, vendorcode)を調べ、そこには例外ではありません。

すべてのテストはローカルPCで行われますが、本番データベースに接続します。 Windows 7のプロサービスパック1

私にエラーを与えた1が、本番環境で、Windows XPのProの2002 Service Pack 3に

以下でのSQLQueryクラスです。

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 
     { 

     } 
    } 
} 

私も行って新しい機能を書いていましたが、プロダクション環境でも同じエラーが表示されました。

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; 
    } 
} 

エラーメッセージが記録:

2017年1月18日15:30:53303 [1] ERROR DrawingRequestEmail.Util [(ヌル)] - ReceiveEmail System.NullReferenceException:オブジェクト参照が設定されていませんオブジェクトのインスタンスに渡します。 (D:\ TFS \ SERVER \ Logistics \ Main \ MM \ DrawingRequest \ TFS \サーバ\物流\メイン\ MM \ DrawingRequest \ DrawingRequestEmail \ DrawingRequestEmail \ Util.cs:ライン123

Util.cs:ライン123

ret = email.ProcessEmailFromSubconSystem(oMessage); 

Email.cs:ライン155

dtDetail = SQLQuery.getRequestDetails(reqNo, vendorcode); 
+0

例外を発生させるブロックのみを含めるようにしてください。 –

+1

これらの 'throw ex;'ステートメントを 'throw'だけに置き換えてください。[あなたが報告しているスタックトレースを台無しにしています] http://stackoverflow.com/questions/730250/is-there-a-difference-between-throw-and-throw-ex)。もっと良いことに、何もしていないので、これらの 'try/catch/finally'ブロックをすべて削除してください。デフォルトの振る舞いは、例外を呼び出しメソッドまでスローすることです。 –

+0

'getRequestDetails'メソッドにいくつかのブレークポイントを置き、実行中にステップポイントを進めます。そのメソッド内の何かが例外を引き起こしているに違いありません。 –

答えて

0

可能null参照エラー、私は見ることができますが、クエリが実行されるにもかかわらずthis-

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; 

で、それ自体がnullになる可能性があることDataSet内部の少なくとも1つDataTableことも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.Any()の代わりにds.Tables.Count > 0を試してください。

+0

私は検査を取り除き、 'ds.Tables [0];を返すだけで、まだエラーが出ます。 – Pop

+0

それでもチェックが必要です。あなたは確かにテーブルがあることをどのように知っていますか?テーブル[0]はインデックス0にテーブルがあることを意味します。 –

+0

私のテスト(本番データベースを指しています)では、私のクエリが何かを返すことをすでに確認しています。 myDBがなく、私の新しいGetRequestDetailであっても、私には同じエラーが出ました。 – Pop

0

また、無意味なNULL例外が発生しました。「次のステートメントの設定」(Ctrl + Shift + F10)を使用すると、デバッガがスキップする変数宣言にトレースされました。

例えば、私のコードは次のようになります。

if (false) 
{ 
    classTypeObject myObject; 
    myObject = someValue; 
} 

私は条件にブレークポイントを設定した場合、開くブレース上で実行するために、次の文を移動し、myObjectの値が設定されているときに私が取得デバッガが宣言をスキップしている必要があるため、null参照例外が発生します。

私は変更する場合は、コードのこのようなことと同じデバッグを行うには何も問題はありません手順:

classTypeObject myObject; 
if (false) 
{ 
    myObject = someValue; 
} 

P.S.明らかにこれは擬似コードであり、if (false)をハードコードすることは決してありません。

関連する問題