system.NullReferenceException有问题

编程入门 行业动态 更新时间:2024-10-26 06:27:45
本文介绍了system.NullReferenceException有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用sqlserver2008,在Windows 7上的Visual Studio 2010 ultimate.i有学校数据库,其中包含学生table.im无法从backend.whats访问此代码中的数据错误。对象引用未设置为实例对象。

使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用 System.Data; 使用 System.Drawing; 使用 System.Linq; 使用 System.Text; 使用 System.Windows.Forms; 使用 System.Data.SqlClient; 命名空间 WindowsFirstsample { public partial class Form1:Form { private SqlConnection cn; private SqlDataAdapter da; private SqlCommand cmd; private DataSet ds; private string strcon; private string strq; private int icounter; public Form1() { InitializeComponent(); } private void Form1_Load( object sender,EventArgs e) { strcon = 数据源= HP-PC // SQLEXPRESS;初始目录=学校;综合安全性=真;; cn = new SqlConnection(strcon); strq = select * from student; cn.Open(); da = new SqlDataAdapter(strq,cn); ds = new DataSet(); da.Fill(ds); textBox1.Text = ds.Tables [ 0 ]。行[ 0 ] [ id]。ToString(); textBox2.Text = ds.Tables [ 0 ]。行[ 0 ] [ name]。ToString(); } private void button1_Click( object sender,EventArgs e) { icounter = 0 ; textBox1.Text = ds.Tables [ 0 ]。行[ 0 ] [ id]。ToString(); textBox2.Text = ds.Tables [ 0 ]。行[ 0 ] [ name]。ToString(); } private void button2_Click( object sender,EventArgs e) { int irowcount = ds.Tables [ 0 ]。Rows.Count - 1 ; if (icounter < irowcount) { icounter ++; textBox1.Text = ds.Tables [ 0 ]。行[icounter] [ id]。ToString(); textBox2.Text = ds.Tables [ 0 ]。行[icounter] [ name]。ToString(); } else { MessageBox.Show( 您已经在最后一条记录); } } private void button3_Click( object sender,EventArgs e) { if (icounter > 0 ) { icounter--; textBox1.Text = ds.Tables [ 0 ]。行[icounter] [ id]。ToString(); textBox2.Text = ds.Tables [ 0 ]。行[icounter] [ name]。ToString(); } else { MessageBox.Show( 您已经在第一条记录); } } private void button4_Click( object sender,EventArgs e) { icounter = ds.Tables [ 0 ] .Rows.Count - 1 ; textBox1.Text = ds.Tables [ 0 ]。行[icounter] [ id]。ToString(); textBox2.Text = ds.Tables [ 0 ]。行[icounter] [ name]。ToString(); } } }

解决方案

来自msdn:

Quote:

尝试取消引用空对象引用时引发的异常。

这里是链接: NullReferenceException [ ^ ] 此异常表示您尝试访问不存在的对象。最好在使用引用之前检查空值。 最好:使用调试器 F5开始调试 F9切换断点 F10跨越 F11步入 祝你有个美好的一天。

im using sqlserver2008,Visual studio 2010 on windows 7 ultimate.i have school database which contains student table.im not able to access the data from the backend.whats wrong in this code.object reference not set to an instance of an object.

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.SqlClient; namespace WindowsFirstsample { public partial class Form1 : Form { private SqlConnection cn; private SqlDataAdapter da; private SqlCommand cmd; private DataSet ds; private string strcon; private string strq; private int icounter; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { strcon="Data Source=HP-PC//SQLEXPRESS;Initial Catalog=school;Integrated security=true; "; cn = new SqlConnection(strcon); strq = "select * from student"; cn.Open(); da = new SqlDataAdapter(strq,cn); ds = new DataSet(); da.Fill(ds); textBox1.Text = ds.Tables[0].Rows[0]["id"].ToString(); textBox2.Text = ds.Tables[0].Rows[0]["name"].ToString(); } private void button1_Click(object sender, EventArgs e) { icounter = 0; textBox1.Text = ds.Tables[0].Rows[0]["id"].ToString(); textBox2.Text = ds.Tables[0].Rows[0]["name"].ToString(); } private void button2_Click(object sender, EventArgs e) { int irowcount = ds.Tables[0].Rows.Count - 1; if (icounter < irowcount) { icounter++; textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString(); textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString(); } else { MessageBox.Show("you are already on the last record"); } } private void button3_Click(object sender, EventArgs e) { if (icounter > 0) { icounter--; textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString(); textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString(); } else { MessageBox.Show("you are already on the first record"); } } private void button4_Click(object sender, EventArgs e) { icounter = ds.Tables[0].Rows.Count - 1; textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString(); textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString(); } } }

解决方案

from msdn:

Quote:

The exception that is thrown when there is an attempt to dereference a null object reference.

here is the link: NullReferenceException[^] this exception means that you tried to access a non-existing object. it is better to check for null values before using references. the best: use the debugger F5 start debug F9 toggle breakpoint F10 step over F11 step into have a good day.

更多推荐

system.NullReferenceException有问题

本文发布于:2023-11-17 03:22:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:system   NullReferenceException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!