Results 1 to 6 of 6

Thread: [RESOLVED] VS.NET 2015 getting System.NullReferenceExcept while trying to transfer Strings

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    11

    Resolved [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.

    Name:  1.jpg
Views: 339
Size:  35.7 KB

    Name:  2.jpg
Views: 310
Size:  34.0 KB

    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
    Attached Files Attached Files

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    11

    Re: VS.NET 2015 getting System.NullReferenceExcept while trying to transfer Strings

    Quote Originally Posted by jmcilhinney View Post
    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?

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    11

    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.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    11

    Re: VS.NET 2015 getting System.NullReferenceExcept while trying to transfer Strings

    Thank you very much, I solve it after giving it a size.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width