0

When I run my program and select button8 i get:

An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.

My code is as simple as -

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    frequency.Show()
    Me.Close()
End Sub

The error is on the frequency.Show() but i don't know how to fix it as all it does is open another form. I have run a check for errors and exceptions but nothing shows until I run it.

EDIT - added frequency.Load code from comments.

Dim LineOfText
Dim aryTextFile() As String
Dim Line As String
Using SR As New IO.StreamReader("...\mainmenu.txt")
    Do While Not SR.EndOfStream
        '________________
        LineOfText = My.Computer.FileSystem.ReadAllText("...\mainmenu.txt")
        aryTextFile = LineOfText.Split(",")
        For i = 0 To UBound(aryTextFile)
            'MsgBox(aryTextFile(i))
        Next i
        ' Read the file just created
        Line = SR.ReadLine
        TextBox1.Text = aryTextFile(0)
        TextBox2.Text = aryTextFile(1)
        TextBox3.Text = aryTextFile(2)
        TextBox4.Text = Today
        TextBox5.Text = aryTextFile(4)
    Loop
End Using
Mark
  • 8,015
  • 1
  • 11
  • 29
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mark Jul 20 '15 at 22:28
  • Where/how is `frequency` declared and initialized? – Mark Jul 20 '15 at 22:28
  • In your `frequency` Form, you've got an error in either the Constructor or the Load() event...post some code. – Idle_Mind Jul 20 '15 at 22:31
  • in my frequency_load section is a simple stream reader to read text file to array and to populate date from the array. this error is occuring on form1.vb – Chris Davies Jul 20 '15 at 22:39
  • Post that "simple" code please... – Idle_Mind Jul 20 '15 at 22:41
  • Dim LineOfText Dim aryTextFile() As String Dim Line As String Using SR As New IO.StreamReader("...\mainmenu.txt") Do While Not SR.EndOfStream '________________ LineOfText = My.Computer.FileSystem.ReadAllText("...\mainmenu.txt") aryTextFile = LineOfText.Split(",") For i = 0 To UBound(aryTextFile) 'MsgBox(aryTextFile(i)) Next i ' Read the file just created – Chris Davies Jul 20 '15 at 22:47
  • Line = SR.ReadLine TextBox1.Text = aryTextFile(0) TextBox2.Text = aryTextFile(1) TextBox3.Text = aryTextFile(2) TextBox4.Text = Today TextBox5.Text = aryTextFile(4) Loop End Using – Chris Davies Jul 20 '15 at 22:47
  • ...that's a mess. How many lines are supposed to be in the file? I assume one? It'd be very easy for that code to generate errors based on the line(s) returned from the file. – Idle_Mind Jul 20 '15 at 22:54
  • yes, just 1 line only. the text is serparated by commas. my level of coding extends to simple college (uk) programs so i appologise. i debugged it one minute. stopped debugging. debugged again and bam.. that error. – Chris Davies Jul 20 '15 at 22:57
  • Can you please post the stack trace of the exception. That will show where the exception is occurring. – Chris Dunaway Jul 21 '15 at 13:44
  • Idle_Mind- no go. still an error with frequency.show – Chris Davies Jul 21 '15 at 17:52
  • i cant post stack trace without using about 4 comments on here... the first 3 lines are- at Accounts.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190 at Accounts.My.MyProject.MyForms.get_frequency() at Accounts.Form1.Button8_Click(Object sender, EventArgs e) in C:\Users\Chris\Documents\Visual Studio 2012\Projects\Accounts - Copy\Accounts - Copy\Accounts\Form1.vb:line 84 – Chris Davies Jul 21 '15 at 18:03
  • After a rebuild of the entire program it is now working for some unknown reason. Thanks for your help. – Chris Davies Jul 21 '15 at 22:04

1 Answers1

0

Try this in your Load() event instead:

Public Class frequency

    Private Sub frequency_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim Lines() As String = System.IO.File.ReadAllLines("...\ MainMenu.txt")
            Dim aryTextFile() As String = Lines(0).Split(",")
            TextBox1.Text = aryTextFile(0)
            TextBox2.Text = aryTextFile(1)
            TextBox3.Text = aryTextFile(2)
            TextBox4.Text = Today
            TextBox5.Text = aryTextFile(4)
        Catch ex As Exception
            MessageBox.Show("Error Loading File")
        End Try
    End Sub

End Class
Idle_Mind
  • 33,113
  • 3
  • 25
  • 33