2011-07-19 70 views
2

我收到错误'对象引用未设置为对象的实例'。我试过寻找类似的问题,但真的不知道我的程序有什么问题。那我有一个错误代码行是: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"); 
      } 
     } 
    } 
} 

}

我意识到这个问题不是太大,但我看不到我的申报计划错误如何

+1

我不知道你在哪里填充您的数据表。它是空的。 – Cyberdrew

+0

是否尝试在该行代码上设置断点(按f9),然后将鼠标悬停在各种标识符上 - 查看哪一个出现空值... –

+0

您错过了将结果集分配给您的部分数据表。表未分配。 – Jonathan

回答

0

你调用Form1_Load的的ExecuteReader()后,右键你需要

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

您永远不会初始化table,因此当您尝试访问其时将会是财产。

虽然在这种情况下实际上并不需要DataTable。您可以直接从OleDbDataReader访问属性见here的细节

喜欢的东西(你初始化。)(看Get***()家庭的方法。):

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

如此反复其余的列。

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

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

无论是表中没有行,或者没有“问题”列或列有一个空它的价值。

考虑将作业分成多个步骤以帮助您诊断问题。或者,更清晰的眼睛已经注意到,这是因为你没有任何东西分配给'桌子'(德哦)。

尽管如此,关于分配作业的评论是一种有用的诊断方法。

1

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

而且如果没有问题列,你可能会得到这样

Column ‘Question’ does not belong to table. 

错误。因此,在这一点上也不行。说是否有'问题'栏。首先,你需要填写你的表格。

+1

表甚至没有初始化。 – mdm

+0

你是对的我的坏。 –

0

错误对象引用空:

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); 
}