0

Hai,

In my code while executing a function i got regularly the exception error as "Object reference not set to an instance of an object"

The function exceuting is as follows

private void PageHeaderSetting(Graphics g)
        {
            try
            {
                DataTable dtPageHeader=new DataTable() ;
                dtPageHeader = ds.Tables["Page Header"];
                if (dtPageHeader.Rows.Count != 0)
                {
                    foreach (DataRow dr in dtPageHeader.Rows)
                    {
                        if (dr.ItemArray[0].ToString() != "")
                            PageHeaderText = dr.ItemArray[0].ToString();
                        else
                            PageHeaderText = "";
                        if (dr.ItemArray[1].ToString() != "")
                            PageHeaderFont = (Font)dr.ItemArray[1];
                        else
                            PageHeaderFont = new Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point);
                        if (dr.ItemArray[2].ToString() != "")
                            PageHeaderFormat = AlignmentSetting(dr.ItemArray[2].ToString());
                        else
                            PageHeaderFormat = AlignmentSetting(Convert.ToString(Alignment.Left));
                        if (dr.ItemArray[3].ToString() != "")
                            PageHeaderColor = (System.Drawing.Color)dr.ItemArray[3];
                        else
                            PageHeaderColor = Color.Black;

                        PageFooterText = Word_Wrap(PageHeaderText, PageHeaderFont, g, 0);
                        PageHeader(g);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

How can i solve this.Can anybody help me?

From the second line i got the exception error means after declaring the datatable and putting the ds.Tables in it from there erroe occurs

5
  • 5
    What line is the exception being thrown from? Commented Sep 9, 2009 at 9:48
  • 1
    Please post, Which line? Commented Sep 9, 2009 at 9:49
  • 1
    It would help if you told us where (on what line) the error occurred. Also you have variables (like "ds") that appear "out of the blue". Commented Sep 9, 2009 at 9:51
  • On which line was the exception thrown? Please tell us. Commented Sep 9, 2009 at 9:52
  • Minor remark: you don't need the "new DataTable()" as you overwrite that in the next line. Just a DataTable dtPageHeader=ds.Tables["Page Header"]; is enough. Commented Sep 9, 2009 at 15:45

4 Answers 4

6

This exception means you're trying to call a method on a null object. The exception should have given you a stack trace with the line number it was thrown at; this'll help you pin it down a bit. You could also try debugging it in visual studio and see where the exception is thrown & see what is null.

Sign up to request clarification or add additional context in comments.

Comments

4

Instead of using ToString() to check if you have a value in the ItemArray you should check the actual value. Check for ItemArray[0] == null and ItemArray[0] == DBNull.Value.

Edit: From your comment it seems that there may not be any data table named "Page Header" in your data set.

Try to add a null check to your DataTable object after the line dtPageHeader = ds.Tables["Page Header"];

Something like this:

if (dtPageHeader == null) 
{ 
   // There is no table named Page Header
}

Comments

1

Look at the line number in the exception.

Your code has numerous chained calls, which is a common source of this exception. If either of these chained properties returns null, the "next" call in the chain will fail with a NullReferenceException.

Comments

0

One of this could be true:

  • ds is null
  • A Value on a columun in dtPageHeader is null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.