简体   繁体   中英

error in table reading :“Object reference not set to an instance of an object.”

my code is for a workbook creator.

the method takes the questions form the DB and put them in list.

i'm trying to put the data in my question list, i've a question class and a getpageDB method, but still getting the error "Object reference not set to an instance of an object."

public DataSet getPageDB(string myQuery, string ConnStr)
{


   OleDbDataAdapter oda = new OleDbDataAdapter   (myQuery, ConnStr);
    DataSet ds = new DataSet();
    oda.Fill(ds);

    foreach(DataRow pRow in ds.Tables[0].Rows){


        _currentQuest.question=pRow["question"].ToString();
        _currentQuest.questionNumber =Convert.ToInt16( pRow["questionnumber"]);
        _currentQuest.rightAnswer=pRow["answer"].ToString();
        _currentQuest.goodFeedBack=pRow["goodfeedback"].ToString();
        _currentQuest.badFeedBack1=pRow["badfeedback1"].ToString();
         _currentQuest.badFeedBack2=pRow["badfeedback2"].ToString();
        AllQuestions.Add(_currentQuest);


    }
    return ds;

}

the error i'm getting is :

Object reference not set to an instance of an object.

what this error mean? what is the problem?

always initialize the class before accessing the property/members of a class.

For ex;

class objcls=null;

objcls=new class();

objcls.name="Stack overflow";

As it says, you're attempting to access an object class that hasn't been instantiated.

Try running in debug to see which line throws the error.

For instance have you instantiated _ currentQuest or AllQuestions before you attempt to use them?

You always need a new Instnace of your _currentQuest!

Before adding values to your question, questionNumber, etc. write

_currentQuest = new Question();

Try instantiating each object before use with NEW operator. You would get to know about the object by debugging. Please try to debug and find which line throws error.

It seems like dataset is empty. This means you need to look into your query first. It is not executing correctly and hence mot filling up the dataset which in turn doest not have any row and when you are starting your foreach loop..it is throwing error. For this you can debug your code and find out where exactly it is throwing and exception.

Object reference error means that one or more of your objects have a value of null and you are trying to access a method or a property of that object.

There may be several places your code can break:

public DataSet getPageDB(string myQuery, string ConnStr)
{
    OleDbDataAdapter oda = new OleDbDataAdapter   (myQuery, ConnStr);
    DataSet ds = new DataSet();
    oda.Fill(ds);

    foreach(DataRow pRow in ds.Tables[0].Rows){ //here if there are no tables in the dataset. So you must check if(ds.Tables.Count > 0) before executing the for loop.

         //What is _currentQuest? Have you initialised it with a new keyword? Is it null when you try to use it?
        _currentQuest.question=pRow["question"].ToString();
        _currentQuest.questionNumber =Convert.ToInt16( pRow["questionnumber"]);
        _currentQuest.rightAnswer=pRow["answer"].ToString();
        _currentQuest.goodFeedBack=pRow["goodfeedback"].ToString();
        _currentQuest.badFeedBack1=pRow["badfeedback1"].ToString();
        _currentQuest.badFeedBack2=pRow["badfeedback2"].ToString();

        //What is AllQuestions? make sure that this is not null.
        AllQuestions.Add(_currentQuest);

    }
    return ds;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM