[VB6 ]求救,『並未將物件參考設定為物件的執行個體』

看板Visual_Basic作者 (QQ~~~又被抓了)時間16年前 (2008/01/13 19:07), 編輯推噓2(201)
留言3則, 2人參與, 最新討論串1/1
Public Class Form1 Dim index As Integer = 0 Dim numOfElement As Integer = 0 Dim numArray() As Integer Dim sum As Integer = 0 Dim average As Double Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If numOfElement = 0 Then numOfElement = CInt(TextBox1.Text) TextBox1.Text = "" Label2.Text = "" If numOfElement > 0 Then Dim numArray(numOfElement) As Integer End If Else If index < numOfElement Then numArray(index) = CInt(TextBox1.Text) TextBox1.Text = "" index = index + 1 End If End If Label1.Text = "輸入第" & CStr(index + 1) & "個數字" If index = numOfElement Then For index As Integer = 0 To numOfElement - 1 sum = sum + numArray(index) Next average = sum / numOfElement Label2.Text = CStr(numOfElement) & "個數字的平均值是" & CStr(average) Label1.Text = "輸入數字個數" numOfElement = 0 index = 0 sum = 0 End If TextBox1.Focus() End Sub End Class -- 可以執行~可是執行到紅色那裡就卡死 出現:第一個可能發生的例外狀況類型 'System.NullReferenceException' 發生於 WindowsApplication1.exe 然後我看了說明 他說要我加new 不過加了編譯後就告訴我陣列不能這樣搞 那....問題在哪裡? -- ▃▂▁ . 玄月王朝 ◣◣◢◢◢ 御林軍團 威武校衛 ◥█◤◥ 裡我的愛 ▔▔▔▔▔▔▔▔▔. ◢█ █◣ ;▌▎ ▃▂▁. ◥◥◤◤ ▆▄▆ FCK-MOON -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.130.198.4

01/13 20:00, , 1F
numArray <<你有宣告array的範圍嗎?多檢查幾次吧?
01/13 20:00, 1F

01/15 06:39, , 2F
sub中應該是redim preserve array(index) ...
01/15 06:39, 2F

01/15 06:40, , 3F
否則numArray就是此sub中的區域變數
01/15 06:40, 3F
文章代碼(AID): #17YV3kBr (Visual_Basic)