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

use*_*041 2 c#

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

labelQuestion.Text = table.Rows[0]["Question"].ToString();
Run Code Online (Sandbox Code Playgroud)

这是我的完整代码:

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");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

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

dle*_*lev 5

你永远不会初始化table,所以null当你试图访问它的Rows属性时.

但是,DataTable在这种情况下,您实际上并不需要.您可以直接从访问属性OleDbDataReader(你初始化.)请参阅这里的详细信息(看Get***()家庭的方法.)

就像是:

labelQuestion.Text = reader.GetString(reader.GetOrdinal("Question"));
Run Code Online (Sandbox Code Playgroud)

等等其余的列.

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

table = new DataTable();
table.Load(reader);
Run Code Online (Sandbox Code Playgroud)