3 Attachment(s)
[RESOLVED] VS.NET 2015 getting System.NullReferenceExcept while trying to transfer Strings
Hi Everyone
I'm trying to use Visual Basic.net 2015 to programming app to use it to split data logger record and store in into array, then use any operations with them...
but the problem is when i try to transfer string between variable i get an error
sell the source code below.
Attachment 140793
Attachment 140795
Code:
Imports System.Text
Imports System.IO
Public Class Form1
Dim path As String = Nothing
Dim splited() As String
Dim buffer() As String
Dim truec() As String
Dim ic As Integer
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FileReader As StreamReader
Dim results As DialogResult
results = OpenFileDialog1.ShowDialog
If results = DialogResult.OK Then
FileReader = New StreamReader(OpenFileDialog1.FileName)
Document.Text = FileReader.ReadToEnd()
FileReader.Close()
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button2.Click
ic = 0
splited = Document.Text.Split(";")
MsgBox(splited.Count)
MsgBox(splited(4))
buffer = splited(0).Split(",")
Label1.Text = buffer(0)
Label2.Text = buffer(1)
For i As Integer = 0 To splited.Count - 2
buffer = splited(i).Split(",")
If buffer(0) = "ROOM_ON" Then
MsgBox(buffer(1))
truec(0) = buffer(1)
ic = ic + 1
End If
Next i
End Sub
End Class
the source code in the attachment contain vb.net source code and text file
Re: VS.NET 2015 getting System.NullReferenceExcept while trying to transfer Strings
You never create the 'truec' array. You declare the variable but you never create an array object and assign it to the variable.
Re: VS.NET 2015 getting System.NullReferenceExcept while trying to transfer Strings
Quote:
Originally Posted by
jmcilhinney
You never create the 'truec' array. You declare the variable but you never create an array object and assign it to the variable.
can you give me a code fix for this?
Re: VS.NET 2015 getting System.NullReferenceExcept while trying to transfer Strings
If I declare 'truec' like
Dim truce(100) as string
there will be no problem and the program fun fine but if I remove the 100 from the truce I'll get the same error why, I want to make an array with out declare it's size like Dim splited() As String, and I use it.
Re: VS.NET 2015 getting System.NullReferenceExcept while trying to transfer Strings
You HAVE to give it a size at some point... or it doesn't exist. splitted() works because the Split function will resize the array and fill it. You need to do the same with truec array... you have to give it a size at some point, so the memory can be allocated and used.
I only see one index of truec being used - truec(0) ... is that the only index that's used? if that's the case, it doesn't need to be an array. If there are other indicies used... well... then yes, it needs to be an array.
-tg
Re: VS.NET 2015 getting System.NullReferenceExcept while trying to transfer Strings
Thank you very much, I solve it after giving it a size.