Variable 'X' is used before it has been assigned a value. A null reference exception could result at runtime.
来源:互联网 发布:中国报告大厅数据库 编辑:程序博客网 时间:2024/05/02 02:01
在自己做机房收费系统的过程中出现了这样的一个现象:如图
str1 下面出现了一行波浪线.同时提醒自己错出现的错误.提示如下:
Variable 'str1' is used before it has been assigned a value. A null reference exception could result at runtime.
在Build的时候会出现以下的警告:
如图:
问题没有大小之分,只要出现就要去解决它.这个并不是错误,这是VS给出的一个警告,是系统潜在的一个问题. 大概意思是变量在用用之前应赋值,否则在运行是可能会出现空引用错误.
但是这有可能会发展成一个非常严重的错误.在执行的时候这些警告有可能会变成严重的 NullReferenceException错误
此错误是一般在执行时是无法处理的,提示会是: 对象的引用未设置到对象的实例!记着在看陈旭的博客中有一篇关于凡客的文章就是出现和此相关的类似错误.文章是凡客网站出现了未处理的错误.借用陈旭博客中的一张图片如下:
为什么会出现这个问题?
如何解决这个问题?
让我们先看看一个简单的例子吧:
Dim str1 As String
Select Case cboKeyword1.SelectedIndex
Case 0
str1 = "stuNo"
Case 1
str1 = "stuName"
Case 2
str1 = "stuSex"
Case 3
str1 = "grade"
Case 4
str1 = "department"
Case 5
str1 = "stuClass"
End Select
如果在某些情况下.当cboKeyword1.SelectIndex的索引是6的话,那么str1将不会被定义,.而System.String这个类是没有默认的构造函数.也就是str1将会是Null值而不是空字符串值(String.Empty).所以在某些情况下会造成凡客网站出现的类似错误:未将对象的引用未设置到对象的实例!
想要修改这个问题其实很简单,只要加上一个Case Else语句就可以了.如下
Dim str1 As String
Select Case cboKeyword1.SelectedIndex
Case 0
str1 = "stuNo"
Case 1
str1 = "stuName"
Case 2
str1 = "stuSex"
Case 3
str1 = "grade"
Case 4
str1 = "department"
Case 5
str1 = "stuClass"
Case Else
str1 =String.Empty
End Select
当然上面的并不是很好的方法,更好的是在变量定义的时候给变量设定默认值,这样就可以从根本上防止出现未将对象的引用未设置到对象的实例的类似错误.如下
Dim str1 As String =String.Empty
Select Case cboKeyword1.SelectedIndex
Case 0
str1 = "stuNo"
Case 1
str1 = "stuName"
Case 2
str1 = "stuSex"
Case 3
str1 = "grade"
Case 4
str1 = "department"
Case 5
str1 = "stuClass"
End Select
在这里我用的是字符串类(System.String)作为一个例子.这个同样也应该适用于其它数据类行的变量或者是类的实例.通过上面的小小的修改就可以避免了并消除那些在运行时出现的错误.防患于未然.
问题到次并没有结束,通过上面的一个"小"错误和凡客出现的"大"问题,给我的感触还是很多的.导致自己程序没有成功的或许就是这么个不经意的一个"忽略".所以我们现在要做的就是尽早的把这些自己所忽略的"波浪线"尽早发现,并及时解决,尽量减少我们的损失和代价.
- Variable 'X' is used before it has been assigned a value. A null reference exception could result at runtime.
- nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of
- Passing by reference or by value? it is a question
- linq insert The null value cannot be assigned to a member with type System.Int64 which is a non-null
- hibernate:Null value was assigned to a property of primitive
- 【hibernate】Null value was assigned to a property of primitive
- Null value was assigned to a property of primitive
- Null value was assigned to a property of primitive ...
- HibernateSystemException: Null value was assigned to a property of primitive ...
- Null value was assigned to a property of primitive
- Is it possible to assign a UTexture2D to a material at runtime?
- “Cannot modify the return value of” “because it is not a variable”
- Cannot modify the return value of ...,because it is not a variable 错误分析与解决
- Cannot modify the return value of 'System...ClientRectangle' because it is not a variable
- the 'access-control-allow-origin' header has a value 'http://localhost' but origin is null
- java.lang.Error: ERROR: it turns out the root cause is not always a runtime exception!
- is-a has-a
- is-a&has-a
- 介绍一篇关于session的好文章,写的很详细
- extern "C"----实现c cpp文件的混合编程
- 哲学家就餐问题
- 强人再拍:中国歼15低俗转弯和高速滑行起飞视频!
- 学习小心得11.5.7
- Variable 'X' is used before it has been assigned a value. A null reference exception could result at runtime.
- 从apnic获取电信 网通 铁通等ip的办法
- [推荐]最长不降子序列与最长公共子序列
- 统计整数二进制表示中1的个数
- XFire实例
- baidu intern exam writing part
- win32下的几种同步机制
- 禁止通过 url直接访问
- jstl