1

I have this line below giving me the error on my title.

dtDetail = SQLQuery.getRequestDetails(reqNo, vendorcode);

But I tested it, dtDetail is not null, reqNo is not null, vendorCode is not null and I am out of idea.

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

I tested SQLQuery.getRequestDetails(reqNo, vendorcode) using the reqNo and vendorCode I get from logger.Debug, there are no exception in there.

All testing is done on my local PC but connect to production database. Windows 7 Pro Service Pack 1.

The one that gave me error is production environment, a windows XP Pro 2002 Service Pack 3.

Below is SQLQuery class.

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
        {

        }
    }
}

I even went and write a new function but it also give me the same error in the production environment.

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

Error message logged:

2017-01-18 15:30:53,303 [1] ERROR DrawingRequestEmail.Util [(null)] - ReceiveEmail System.NullReferenceException: Object reference not set to an instance of an object. at DrawingRequestEmail.Email.ProcessEmailFromSubconSystem(MailItem message) in D:\TFS\SERVER\Logistics\Main\MM\DrawingRequest\DrawingRequestEmail\DrawingRequestEmail\Email.cs:line 155 at DrawingRequestEmail.Util.ReceiveEmail() in D:\TFS\SERVER\Logistics\Main\MM\DrawingRequest\DrawingRequestEmail\DrawingRequestEmail\Util.cs:line 123

Util.cs:line 123

ret = email.ProcessEmailFromSubconSystem(oMessage);

Email.cs:line 155

dtDetail = SQLQuery.getRequestDetails(reqNo, vendorcode);
Pop
  • 525
  • 1
  • 7
  • 22
  • Could you please remove irrelevant codes, and include only the block that raises the exception – sujith karivelil Jan 18 '17 at 01:31
  • @GrantWinney I tested `getRequestDetails`, no error. – Pop Jan 18 '17 at 02:16
  • Your question lacks information; on which environment does it returns an NullReferenceException? Provide details of the two environments, the one that generates the error and your local env, which doesn't. – Emerson Cardoso Jan 18 '17 at 02:36
  • In your new function that you wrote, could you check exactly on which line the exception is thrown? Can you provide the details of the exception? – Emerson Cardoso Jan 19 '17 at 00:50
  • @EmersonCardoso I cannot set break point in production environment. I updated exception in my edit. – Pop Jan 20 '17 at 01:41
  • Change your function in order to log the stack trace of the exception, then. This will help us to investigate the problem. – Emerson Cardoso Jan 20 '17 at 02:16

2 Answers2

0

A possible null reference error, I can see is 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;

Even though the query executes, there is no guarantee that there will be atleast one DataTable inside that DataSet and also DataSet itself could be null. Try adding a null check like this -

if (ds != null && ds.Tables.Any() && ds.Tables[0].Rows.Count > 0) 
        return ds.Tables[0];
    else
        return null;

There is also no null check that after this statement -

myDB = new ATSSPCommon.SQLDB(strConnectionString);

myDB could also be null.

UPDATE: For .net 2.0 try ds.Tables.Count > 0 instead of ds.Tables.Any() as mentioned in the comment.

brainless coder
  • 6,310
  • 1
  • 20
  • 36
  • I removed the checking and only `return ds.Tables[0];` is left, it still give me error. – Pop Jan 18 '17 at 03:53
  • That still need a check. How do you know there is a table for sure? Table[0] means it has a table at index 0. – brainless coder Jan 18 '17 at 04:01
  • In my testing(point to production database), I already make sure that my query will return something. Even my new `GetRequestDetail` without `myDB` and checking gave me the same error. – Pop Jan 18 '17 at 04:04
  • .Net 2.0, I cannot use `ds.Tables.Any()`. – Pop Jan 18 '17 at 04:08
  • I tested using `if (ds != null && ds.Tables[0].Rows.Count > 0)`, still error. – Pop Jan 18 '17 at 04:25
  • use this if (ds != null && ds.Tables!=null && ds.Tables.Count>0 && ds.Tables[0].Rows.Count >0) – Võ Quang Hòa Jan 18 '17 at 04:44
0

I was getting a nonsensical NULL exception as well and I traced it down to a variable declaration getting skipped by the debugger when I use "Set Next Statement" (Ctrl+Shift+F10).

For example, my code looks like this:

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

If I set a breakpoint on the condition then move the next statement to run on the opening brace, when the value of myObject is set I get a null reference exception because the debugger must have skipped the declaration.

However, if I change the code to be like this and do the same debugging steps there is no issue:

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

P.S. Obviously this is pseudocode and you would never want to hardcode if (false).

Elaskanator
  • 1,135
  • 10
  • 28