c#对象问题——无法解决
本文关键字:解决 问题 对象 | 更新日期: 2023-09-27 18:02:29
我得到错误'对象引用未设置为对象的实例'。我试着看类似的问题,但真的看不出我的程序有什么问题。我有一个错误的代码行是:
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");
}
}
}
}
}
我意识到问题不是太大,但我看不出我的声明时间是错误的
您从未初始化table
,因此当您尝试访问其Rows
属性时,它将是null
。
在本例中实际上不需要DataTable
。您可以直接从OleDbDataReader
访问属性(您对其进行初始化)。详细信息请参阅此处(查看Get***()
方法族)
类似:
labelQuestion.Text = reader.GetString(reader.GetOrdinal("Question"));
依此类推。
如果您选择使用table
,则省略reader.Read()
行并添加:
table = new DataTable();
table.Load(reader);
问题是您的表没有初始化。因此,当您尝试访问table.row[0]
时,您会得到这个错误消息。
如果没有问题列,你可能会得到一个错误,如
Column ‘Question’ does not belong to table.
因此,在这一点上它是不可以的。来说明是否有"问题"栏。首先,需要填充表格。
在Form1_Load中调用ExecuteReader()之后,需要
table = new DataTable();
table.Load (reader);
错误对象引用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);
}
表没有行,或者没有"Question"列,或者列中有Null值
考虑将任务分解为多个步骤,以帮助您诊断问题。
编辑:或者,正如敏锐的眼睛已经注意到的那样,这是因为你没有给"table"(d'oh)分配任何东西。
关于分解分配的注释是一个有用的诊断方法。