我有此错误对象引用未设置为对象的实例. [英] I HAVE THIS ERROR object reference not set to an instance of an object.

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

问题描述

这里有我的代码错了

HERES MY CODE WHATS WRONG WITH THAT

 Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged


        Dim intSelectedIndex As Integer
        intSelectedIndex = ComboBox2.SelectedIndex
        Dim objSelectedItem As Object
        objSelectedItem = ComboBox2.SelectedValue

        TextBox6.Text = objSelectedItem.ToString()

    End Sub
End Class



我尝试过的事情:



What I have tried:

I HAVE THIS ERROR Object reference not set to an instance of an object.

推荐答案

这是我们遇到的最常见的问题之一,也是我们最无力回答的问题,但是你是最有能力回答你自己.

让我来解释一下错误的含义:您试图使用变量,属性或方法的返回值,但其中包含null-这意味着变量中没有类的实例.
它有点像一个口袋:您的衬衫上有一个口袋,用来握笔.如果您伸手去拿口袋,发现那里没有笔,那您就不能在纸上签名了-如果尝试,您会得到非常有趣的表情!空的口袋为您提供了一个空值(这里没有笔!),因此您不能做任何通常在找回笔时会做的事情.为什么它是空的?这就是问题所在-可能是您今天早上离开家时忘记拿起笔,或者昨晚脱掉笔时把它留在了昨天衬衫的口袋里.

我们无法分辨,因为我们不在那,更重要的是,我们甚至看不到您的衬衫,更不用说口袋里的东西了!

回到计算机,您就以某种方式做了同样的事情-我们看不到您的代码,更不用说运行它了,找出不应该包含null的内容.
但是您可以-Visual Studio将在这里为您提供帮助.在调试器中运行程序,当程序失败时,VS将向您显示发现问题所在的行.然后,您可以开始查看它的各个部分,以了解什么是空值,然后开始回顾您的代码以找出原因.因此,在包含错误行的方法的开头放置一个断点,然后从头开始再次运行程序.这次,VS将在错误发生之前停止运行,并让您通过逐步查看代码来检查正在发生的情况.

但是我们无法做到这一点-我们没有您的代码,如果我们有它,我们也不知道如何使用它,我们没有您的数据.因此,请尝试一下-看看您能找到多少信息!
This is one of the most common problems we get asked, and it''s also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It''s a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn''t a pen there, you can''t sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can''t do anything that you would normally do once you retrieved your pen. Why is it empty? That''s the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can''t tell, because we weren''t there, and even more importantly, we can''t even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can''t see your code, much less run it and find out what contains null when it shouldn''t.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can''t do that - we don''t have your code, we don''t know how to use it if we did have it, we don''t have your data. So try it - and see how much information you can find out!


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

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