如何解决此错误对象引用未设置为对象的实例。 [英] How Do I Solve This Error Object Reference Not Set To An Instance Of An Object.

查看:134
本文介绍了如何解决此错误对象引用未设置为对象的实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadImageData();
        }
    }
    
    private void LoadImageData()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/Data/ImageData.xml"));
        ViewState["ImageData"] = ds;
    
        ViewState["ImageDisplayed"] = 1;
        DataRow imageDataRow = ds.Tables["image"].Select().FirstOrDefault(x => x["order"].ToString() == "1");
        Image1.ImageUrl = "/slide/" + imageDataRow["name"].ToString();
        lblImageName.Text = imageDataRow["name"].ToString();
        lblImageOrder.Text = imageDataRow["order"].ToString();
    }
    
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        int i = (int)ViewState["ImageDisplayed"];
        i = i + 1;
        ViewState["ImageDisplayed"] = i;
    
        DataRow imageDataRow = ((DataSet)ViewState["ImageData"]).Tables["image"].Select().FirstOrDefault(x => x["order"].ToString() == i.ToString());
        if (imageDataRow != null)
        {
            Image1.ImageUrl = "~/slide/" + imageDataRow["name"].ToString();
            lblImageName.Text = imageDataRow["name"].ToString();
            lblImageOrder.Text = imageDataRow["order"].ToString();
        }
        else
        {
            LoadImageData();
        }
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Timer1.Enabled)
        {
            Timer1.Enabled = false;
            Button1.Text = "Start Slideshow";
        }
        else
        {
            Timer1.Enabled = true;
            Button1.Text = "Stop Slideshow";
        }
    }

推荐答案

此错误表示您在代码中尝试访问属性/方法一个null对象。

你应该调试你的程序(通过在生成异常的方法中放置一个断点)然后从该代码区检查你的对象。



PS:您应该提供有关生成异常的代码行的更多信息,以便更好地帮助您。
This error signal that in your code you are trying to access a property/method of an null object.
You should debug your program (by putting a breakpoint in the method that generate the exception) then inspect your objects from that code zone.

PS: You should provide more info about the line of code that generate the exception in order to can help you better.


您好开发者,



尝试这个步骤,



步骤1:在代码中放置try-catch块。

步骤2:设置一个特定方法中的断点并调试应用程序。

步骤3:按F11,在特定行中将发生错误并中断执行。

步骤4:Anlayse特定线。它将具有空值。

步骤5:如果清除空值问题,您的代码将运行良好。



享受编码。 !
Hi Developer,

Try this steps,

Step 1 : Put try-catch block in the code.
Step 2 : Set a breakpoint in the particular method and debug the application.
Step 3 : Press F11, in the specific line error will occur and break the execution.
Step 4 : Anlayse the particular line. It will have a null value.
Step 5 : If null value problem cleared, your code will work well.

Enjoy the coding.!


请参阅最受欢迎的答案 Sergey (@SAKryukov)对象引用未设置为对象的实例,请参阅下面的代码。 [ ^ ]。
Refer the most popular answer by Sergey ( @SAKryukov ) "Object Reference not set to Instance of an object", please see the code below.[^].


这篇关于如何解决此错误对象引用未设置为对象的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆