变量在赋值前被使用,可能导致null错误!!!!!

haolly 2010-12-13 02:39:31
Public Class Child
Dim arry(,) As String



Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim temp As Boolean = True
If TextBox1.Text = "" Then
MessageBox.Show("请输入姓名", "出错了!", MessageBoxButtons.OK, MessageBoxIcon.Error)
temp = False
ElseIf IsNumeric(TextBox2.Text) = False Then
MessageBox.Show("成绩必须为正整数", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error)
temp = False
ElseIf CInt(TextBox2.Text) > 100 Then
MessageBox.Show("成绩不能大于100", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error)
temp = False
ElseIf IsNumeric(TextBox3.Text) = False Then
MessageBox.Show("成绩必须为正整数", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error)
temp = False
ElseIf CInt(TextBox3.Text) > 100 Then
MessageBox.Show("成绩不能大于100", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error)
temp = False
ElseIf IsNumeric(TextBox4.Text) = False Then
MessageBox.Show("成绩必须为正整数", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error)
temp = False
ElseIf CInt(TextBox4.Text) > 100 Then
MessageBox.Show("成绩不能大于100", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error)
temp = False
ElseIf IsNumeric(TextBox5.Text) = False Then
MessageBox.Show("成绩必须为正整数", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error)
temp = False
ElseIf CInt(TextBox5.Text) > 100 Then
MessageBox.Show("成绩不能大于100", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error)
temp = False

End If
If temp = True Then
Dim fmtstr As String = "{0,-24}{1,-5}{2,-5}{3,-5}{4,-6}{5,-7}{6,-5}"
Dim sum As Double = CDbl(TextBox2.Text) + CDbl(TextBox3.Text) + CDbl(TextBox4.Text) + CDbl(TextBox5.Text)
Dim avg As Double = sum / 4
Dim gread As String

If 0 <= avg < 60 Then

gread = "E"
ElseIf 60 <= avg < 70 Then


gread = "D"
ElseIf 70 <= avg < 80 Then
gread = "C"
ElseIf 80 <= avg < 90 Then

gread = "B"
ElseIf 90 <= avg <= 100 Then
gread = "A"


End If
Dim i As Integer
ReDim Preserve arry(i, 6)


arry(i, 0) = TextBox1.Text
arry(i, 1) = TextBox2.Text
arry(i, 2) = TextBox3.Text
arry(i, 3) = TextBox4.Text
arry(i, 4) = TextBox5.Text
arry(i, 5) = avg
arry(i, 6) = gread







Form1.ListBox1.Items.Add(String.Format(fmtstr, arry(i, 0), arry(i, 1), arry(i, 2), arry(i, 3), arry(i, 4), arry(i, 5), arry(i, 6)))

MessageBox.Show("已添加到列表中,请注意保存", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)

TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
i += 1
End If






End Sub

End Class

就那个gread有问题了。
...全文
449 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiansr 2010-12-13
  • 打赏
  • 举报
回复
ReDim Preserve arry(i, 6)

改为 7 试试
chinaboyzyq 2010-12-13
  • 打赏
  • 举报
回复
If 0 <= avg < 60 Then
gread = "E"
ElseIf 60 <= avg < 70 Then
.
.

if avg>=0 and avg<60 then
gread="E"
elseif avg>=60 and avg<70 then
.
.
另外,使用变量尽量先定义变量。
yanlongwuhui 2010-12-13
  • 打赏
  • 举报
回复
Dim gread As String=""试试

herocxgood 2010-12-13
  • 打赏
  • 举报
回复
LZ說gread出了問題
Dim gread As String

If 0 <= avg < 60 Then

gread = "E"
ElseIf 60 <= avg < 70 Then


gread = "D"
ElseIf 70 <= avg < 80 Then
gread = "C"
ElseIf 80 <= avg < 90 Then

gread = "B"
ElseIf 90 <= avg <= 100 Then
gread = "A"
這里給gread賦值我看也沒啥問題啊
LZ可以試著將賦值后的gread打印出來看看是否為A,B,C,D,E中的一個值
haolly 2010-12-13
  • 打赏
  • 举报
回复
Me.ListBox1.Items.Add(String.Format(fmtstr, Child.arry(i, 0), Child.arry(i, 1), Child.arry(i, 2), Child.arry(i, 3), Child.arry(i, 4), Child.arry(i, 5), Child.arry(i, 6)))
我在另一个窗口引用时出现错误,“未将对象引用设置到对象的实例。”
可是我明明在窗口中已经赋值了呀
arry(i, 0) = TextBox1.Text
arry(i, 1) = TextBox2.Text
arry(i, 2) = TextBox3.Text
arry(i, 3) = TextBox4.Text
arry(i, 4) = TextBox5.Text
arry(i, 5) = avg
arry(i, 6) = gread

haolly 2010-12-13
  • 打赏
  • 举报
回复
楼上,你在仔细看看,不是i的问题呀,是gread的问题
herocxgood 2010-12-13
  • 打赏
  • 举报
回复
我看是那個 Dim i As Integer出了問題,沒有初始化,后面的arry(i, 0) = TextBox1.Text就用到了i

16,556

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧