你调用的对象是空的! [英] Object reference not set to an instance of an object!

查看:129
本文介绍了你调用的对象是空的!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个学生管理系统,它给我一个错误对象引用未设置为对象的实例,同时更新DataGridView内的数据...更新按钮在我插入TreeView之前成功运行...从那以后我插入TreeView ..Update按钮给出错误...错误显示在SQL命令行上...



我尝试过:



这是我的代码...



I created a Student Management System and It gives me an error "Object Reference Not set to an Instance of an Object" while updating data inside the DataGridView...Update button worked successfully before i Insert a TreeView...Ever since i inserted the TreeView..Update Button Gives an error...The error shows on the SQL Command Line...

What I have tried:

This is my code...

Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
        Try
            Dim upcom11 As New SqlCommand("Update Students SET FullName='" & ffname.Text & "',FirstName='" & firstname.Text & "',LastName='" & lastname.Text & "',Gender='" & male.Checked Or female.Checked & "',Address='" & address1.Text & "',Contact='" & contact1.Text & "',NIC='" & nic1.Text & "',Email='" & Email1.Text & "',DateOfBirth='" & DOB1.Value & "' Where StudentID = '" & Registered_Students.Registered.CurrentRow.Cells(0).Value & "' ", connection)
            If Registered_Students.Registered.SelectedRows.Count > 0 Then
                connection.Open()
                upcom11.ExecuteNonQuery()
                connection.Close()
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

推荐答案

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



让我解释一下错误的含义:你试图使用变量,属性或方法返回值,但它包含null - 这意味着没有insta变量中一个类的nce。

它有点像一个口袋:你的衬衫上有一个口袋,用来握笔。如果你进入口袋并发现那里没有笔,你就不能在一张纸上签名 - 如果你尝试的话,你会得到非常有趣的外观!空口袋给你一个空值(这里没有笔!)所以你不能做任何你检索笔通常做的事情。它为什么空?这就是问题 - 可能是你今天早上离开家时忘了拿起你的笔,或者你昨晚把它拿到昨天的衬衫口袋里时可能会把笔留下来。



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



回到计算机,你做了同样的事情,不知何故 - 我们看不到你的代码,更不用说运行它了,找不到包含null的东西。

但是你可以 - 而Visual Studio将在这里帮助你。在调试器中运行您的程序,当它失败时,VS将向您显示它发现问题的行。然后,您可以开始查看它的各个部分,以查看哪个值为null,并开始回顾代码以找出原因。因此,在包含错误行的方法的开头放置一个断点,然后从头再次运行程序。这一次,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!


0)错误是显而易见的。某些引用类型变量的值为null,您尝试调用其属性或方法之一。结果是你看到的错误。



1)我们不要猜测错误的位置。发生异常时检查堆栈跟踪。它将告诉您错误发生在哪一行。



2)使用调试器并在堆栈跟踪告诉您的行处设置断点。然后你必须检查所有变量的值,看看它的值是null。



3)如果你在尝试调用方法之前没有检查或者属性,你会经常看到这个错误。



4)猜测:看看你把SQL字符串放在一起的行。它很可能是许多参数中的一个,它们是null。无论如何,您应该使用SQL参数intead of strings。即使它似乎工作,这是一个非常危险的做法。
0) The error is obvious. Some reference type variable has the value null and you try to call one of its properties or methods. The result is the error you see.

1) Let's not guess about the location of the error. Examine the stack trace when the exception occurs. It will tell you on which line the error occurs.

2) Use the debugger and set a breakpoint at the line the stack trace told you. Then you must examine the values of all variables to see which one it is that has the value null.

3) If you don't check before trying to call methods or properties, you will be seeing this error quite often.

4) Just guessing: Look at the line in which you put together the SQL string. It's most probably one of the many parameters which is null. You should use SQL parameters intead of strings anyway. Even if it seems to work, this is a really dangerous practice.


不是你的问题的解决方案,但你有另一个问题。

从不构建SQL通过连接字符串进行查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
Quote:

对象引用未设置为对象的实例!

Object reference not set to an instance of an object!



使用调试器查看错误的位置并检查变量,其中1个不是预期的。应该设置该对象的命令失败了,但是在你尝试使用它之前你不知道。

-----

有一个工具允许您查看代码正在执行的操作,其名称为调试程序。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

Visual Basic / Visual Studio视频导师ial - 基本调试 - YouTube [ ^ ]

初学者的Visual Basic .NET编程 - 断点和调试工具 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你到。当代码没有达到预期的效果时,你就会接近一个错误。


Use the debugger to see where is the error and check the variables, 1 of them is not what is expected. The command that is supposed to set that object have failed, but you don't know until you try to use it.
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于你调用的对象是空的!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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