Object reference was not set to an instance of an object RRS feed

  • Question

  • User360451555 posted

    Am just puzzled about why this line of code is throwing an "Object reference was not set to an instance of an object".

    var printJobDataTable = printJob.Label.DatabaseLabelVariables.Tables[0];

    DatabaseLabelVariables returns a Dataset and has the following code, 

    public DataSet DatabaseLabelVariables
             string query = "SELECT * FROM ILSLabelVars WHERE label=@label;";
             SqlParameter label = new SqlParameter("@label", this.uniqueName);
             return ILSDBConnector.SelectDataSet(query, label);

    So am wondering which object haven't i initialized. 

    I tried to cast the DataTable returned from DatabaseLabelVariables explicitly but still throwing the same error. 

    var printJobDataTable = (DataTable)printJob.Label.DatabaseLabelVariables.Tables[0];

    I also tried to explicitly instantiate printJobDataTable so that i just assign to it, but still am getting the same error. 

    var printJobDataTable = new DataTable();
    printJobDataTable = printJob.Label.DatabaseLabelVariables.Tables[0];

    Sunday, August 26, 2018 8:30 PM

All replies

  • User-893317190 posted

    Hi feiyim,

    It seems that the code you have posted is not complete.

    You haven't posted the ILSDBConnector.SelectDataSet method. 

    What's your variable printJob  and  what's printJob.Label?

    Please post more of your code, so that we could find what causes the error.

    Best regards,

    Ackerly Xu

    Monday, August 27, 2018 7:26 AM
  • User753101303 posted


    See first what is null using the debugger. It could be also Label or DatabaseLabelVariables.

    Edit: if not familiar with the debugger you could start with code such as :

    if (printJob.Label==null) throw new NullReferenceException("Label");
    if (printJob.Label.DatabaseLevelVariables==null) thow new NullReferenceException("DatabaseLevelVariables");


    Monday, August 27, 2018 7:34 AM