对象引用未设置到ASP.net的对象的实例 [英] Object Reference Not Set to the Instance of the Object in ASP.net

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

问题描述

基本上我试图把出来的文本框共享功能在里面,但我没有访问共享的功能里面的文本框然后,我创建的_Default类的一个对象来访问文本框。结果
从那以后,我访问文本框,但运行状态期间,它给我的错误NullReference,当我拨动我发现Textbox1.text =什么,而不是Textbox1.text =结果
下面是小code,我对样品进行

 保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手
    Me.Load
        TextBox1.Text =123
        识别TestClass()
    结束小组    受保护的共享子识别TestClass()
        昏暗MyF1作为新_Default
        MyF1.TextBox1.Text =ABC
    结束小组


解决方案

TextBox1中是由ASP.NET设计师的Page类,创建(可能)的实例变量

因此​​,你不能访问它在静态/共享的内容。

当ASP.NET接收到一个请求,它:


  • 创建您的_Default类的实例

  • 根据来自客户端的数据填充_Default类的控制(例如表格数据)

  • 做了很多,不能在这方面有关的其他操作的

在您的情况,您创建 _Default 类的实例,但不设置任何属性。

这导致 Textbox1的财产被空,这就是为什么它抛出的NullReferenceException 使用时。

有两种可能的解决方案:


  • 标记的TestClass 方法,实例方法(删除共享关键字)

  • 通过 _Default 的当前实例作为参数传递给静态/共享方法,像这样:

     保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load
        TextBox1.Text =123
        识别TestClass(ME)
    结束小组受保护的共享子的TestClass(如当前页_Default)
        currentPage.TextBox1.Text =ABC
    结束小组


第二个解决方案要求 TextBox1中属性应该被声明为公共

(对不起,如果有任何错误VB;我是一个C#程序员☺)

Basically I'm Trying to put out on the Textbox inside the Shared Function, But i didn't access the Textbox inside the Shared Function Then i created an object of the _Default Class to access the Textbox.
After that i access the Textbox, but during running state it give me error NullReference, when i toggle i found that Textbox1.text = Nothing instead of Textbox1.text=""
Below is the Small Code which i made for sample

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles       
    Me.Load        
        TextBox1.Text = "123"        
        TestClass()        
    End Sub       

    Protected Shared Sub TestClass()       
        Dim MyF1 As New _Default       
        MyF1.TextBox1.Text = "ABC"       
    End Sub       

解决方案

TextBox1 is an instance variable of your Page class, created (probably) by the ASP.NET designer.

Thus, you cannot access it in static/shared context.

When ASP.NET receives a request, it:

  • creates an instance of your _Default class
  • populates the controls of _Default class according to the data that comes from the client (e.g. form data)
  • does a lot of other operations which are not relevant in this context

In your scenario, you create an instance of the _Default class, but don't set any of its properties.

This results in the Textbox1 property being null and that's why it throws NullreferenceException when used.

There are two possible solutions:

  • mark the TestClass method as instance method (drop the Shared keyword)
  • pass the current instance of _Default as a parameter to the static/shared method, like so:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        
        TextBox1.Text = "123"        
        TestClass(Me)        
    End Sub       
    
    Protected Shared Sub TestClass(currentPage as _Default)       
        currentPage.TextBox1.Text = "ABC"       
    End Sub 
    

The second solution requires that the TextBox1 property should be declared as public.

(sorry if there are any VB mistakes; I'm a C# programmer ☺)

这篇关于对象引用未设置到ASP.net的对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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