C#对象问题 - 无法解决它

时间:2011-07-19 20:18:51

标签: c#

我收到错误'对象引用没有设置为对象的实例'。我试过看类似的问题,但真的看不出我的程序有什么问题。我遇到错误的代码行是:

labelQuestion.Text = table.Rows[0]["Question"].ToString();

以下是我的完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Quiz_Test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    String chosenAnswer, correctAnswer;
    DataTable table;

    private void Form1_Load(object sender, EventArgs e)
    {
        //declare connection string using windows security
        string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb";

        //declare Connection, command and other related objects
        OleDbConnection conGet = new OleDbConnection(cnString);
        OleDbCommand cmdGet = new OleDbCommand();

        //try
        //{
        //open connection
        conGet.Open();
        //String correctAnswer;

        cmdGet.CommandType = CommandType.Text;
        cmdGet.Connection = conGet;

        cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()";

        OleDbDataReader reader = cmdGet.ExecuteReader();
        reader.Read();
        labelQuestion.Text = table.Rows[0]["Question"].ToString();
        radioButton1.Text = table.Rows[0]["Answer 1"].ToString();
        radioButton2.Text = table.Rows[0]["Answer 2"].ToString();
        radioButton3.Text = table.Rows[0]["Answer 3"].ToString();
        radioButton4.Text = table.Rows[0]["Answer 4"].ToString();
        correctAnswer = table.Rows[0]["Correct Answer"].ToString(); ;


        conGet.Close();

    }

    private void btnSelect_Click(object sender, EventArgs e)
    {
        String cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb";

        //declare Connection, command and other related objects
        OleDbConnection conGet = new OleDbConnection(cnString);
        OleDbCommand cmdGet = new OleDbCommand();

        //try
        {
            //open connection
            conGet.Open();

            cmdGet.CommandType = CommandType.Text;
            cmdGet.Connection = conGet;

            cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()"; // select all columns in all rows

            OleDbDataReader reader = cmdGet.ExecuteReader();
            reader.Read();

            if (radioButton1.Checked)
            {
                chosenAnswer = reader["Answer 1"].ToString();
            }
            else if (radioButton2.Checked)
            {
                chosenAnswer = reader["Answer 2"].ToString();
            }
            else if (radioButton3.Checked)
            {
                chosenAnswer = reader["Answer 3"].ToString();
            }
            else
            {
                chosenAnswer = reader["Answer 4"].ToString();
            }

            if (chosenAnswer == reader["Correct Answer"].ToString())
            {
                //chosenCorrectly++;
                MessageBox.Show("You have got this answer correct");
                //label2.Text = "You have got " + chosenCorrectly + " answers correct";
            }
            else
            {
                MessageBox.Show("That is not the correct answer");
            }
        }
    }
}

}

我意识到问题不是太大但我看不出我的宣言时间是错误的

5 个答案:

答案 0 :(得分:5)

您永远不会初始化table,因此当您尝试访问其null属性时,它将为Rows

但是,在这种情况下,您实际上并不需要DataTable。您可以直接从OleDbDataReader(您执行初始化)访问这些属性。有关详细信息,请参阅here(查看Get***()系列方法。)< / p>

类似的东西:

labelQuestion.Text = reader.GetString(reader.GetOrdinal("Question"));

其他栏目等等。

如果您选择使用table,请忽略reader.Read()行并添加:

table = new DataTable();
table.Load(reader);

答案 1 :(得分:1)

问题是您的表未初始化。因此,当您尝试访问table.row[0]时,会收到此错误消息。

如果没有问题栏,您可能会收到类似

的错误
Column ‘Question’ does not belong to table.

因此,在这一点上它并不好。说是否有“问题”专栏。首先,你需要填写你的表格。

答案 2 :(得分:0)

在Form1_Load中调用ExecuteReader()之后,您需要

table = new DataTable();
table.Load (reader);

答案 3 :(得分:0)

错误对象引用Null:

try
{
    OleDbCommand com;
    //   OleDbConnection con = new OleDbConnection(ConnectionString);
    cn.Open();

    string str = "select *from DatabaseLogin where user_id='" + textBox2.Text + "'";
    com = new OleDbCommand(str, cn);
    OleDbDataReader reader = com.ExecuteReader();
    while (reader.Read())
    {
        checkedListBox1.Items.Add(reader["stckdemge"].ToString());
    }
}
catch (Exception ex)
{
    MessageBox.Show(" " + ex);
}

答案 4 :(得分:-2)

表中没有行,或者没有“Question”列,或者列中有Null值。

考虑将分配分解为多个步骤,以帮助您诊断问题。

编辑: 或者,正如眼睛更清晰已经注意到,这是因为你没有为'table'指定任何东西(d'oh)。

关于分手的评论是一种有用的诊断方法。