object reference not set to an instance of an object

da_gibb

New member
Joined
Jun 8, 2004
Messages
3
Programming Experience
1-3
I just started learning Visual Basic a few days ago and have played with this for an hour. If I use line 113 (about ten lines from bottom), I get the
object reference not set to an instance of an object
error. If I replace "textbox1.text" with "fubar", it still gives the error.

VB.NET:
[size=2][color=#0000ff]Imports[/color][/size][size=2] System.Text

[/size][size=2][color=#0000ff]Imports[/color][/size][size=2] System.IO

[/size][size=2][color=#0000ff]Imports[/color][/size][size=2] System.Net

[/size][size=2][color=#0000ff]Public[/color][/size][size=2] [/size][size=2][color=#0000ff]Class[/color][/size][size=2] Form_addremovelist

[/size][size=2][color=#0000ff]Inherits[/color][/size][size=2] System.Windows.Forms.Form

#[/size][size=2][color=#0000ff]Region[/color][/size][size=2] " Windows Form Designer generated code "

[/size][size=2][color=#0000ff]Public[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2]()

[/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].New()

[/size][size=2][color=#008000]'This call is required by the Windows Form Designer.

[/color][/size][size=2]InitializeComponent()

[/size][size=2][color=#008000]'Add any initialization after the InitializeComponent() call

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2][/size][size=2][color=#008000]'Form overrides dispose to clean up the component list.

[/color][/size][size=2][/size][size=2][color=#0000ff]Protected[/color][/size][size=2] [/size][size=2][color=#0000ff]Overloads[/color][/size][size=2] [/size][size=2][color=#0000ff]Overrides[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Dispose([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] disposing [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Boolean[/color][/size][size=2])

[/size][size=2][color=#0000ff]If[/color][/size][size=2] disposing [/size][size=2][color=#0000ff]Then

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] [/size][size=2][color=#0000ff]Not[/color][/size][size=2] (components [/size][size=2][color=#0000ff]Is[/color][/size][size=2] [/size][size=2][color=#0000ff]Nothing[/color][/size][size=2]) [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]components.Dispose()

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Dispose(disposing)

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2][/size][size=2][color=#008000]'Required by the Windows Form Designer

[/color][/size][size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] components [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.ComponentModel.IContainer

[/size][size=2][color=#008000]'NOTE: The following procedure is required by the Windows Form Designer

[/color][/size][size=2][/size][size=2][color=#008000]'It can be modified using the Windows Form Designer. 

[/color][/size][size=2][/size][size=2][color=#008000]'Do not modify it using the code editor.

[/color][/size][size=2][/size][size=2][color=#0000ff]Friend[/color][/size][size=2] [/size][size=2][color=#0000ff]WithEvents[/color][/size][size=2] Button2 [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.Button

[/size][size=2][color=#0000ff]Friend[/color][/size][size=2] [/size][size=2][color=#0000ff]WithEvents[/color][/size][size=2] ListBox1 [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.ListBox

[/size][size=2][color=#0000ff]Friend[/color][/size][size=2] [/size][size=2][color=#0000ff]WithEvents[/color][/size][size=2] Button1 [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.Button

[/size][size=2][color=#0000ff]Friend[/color][/size][size=2] [/size][size=2][color=#0000ff]WithEvents[/color][/size][size=2] TextBox1 [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.TextBox

<System.Diagnostics.DebuggerStepThrough()> [/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] InitializeComponent()

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button2 = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Windows.Forms.Button

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].ListBox1 = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Windows.Forms.ListBox

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button1 = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Windows.Forms.Button

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBox1 = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Windows.Forms.TextBox

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].SuspendLayout()

[/size][size=2][color=#008000]'

[/color][/size][size=2][/size][size=2][color=#008000]'Button2

[/color][/size][size=2][/size][size=2][color=#008000]'

[/color][/size][size=2][/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button2.Location = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Drawing.Point(56, 336)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button2.Name = "Button2"

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button2.Size = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Drawing.Size(112, 24)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button2.TabIndex = 5

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button2.Text = "Remove Selected"

[/size][size=2][color=#008000]'

[/color][/size][size=2][/size][size=2][color=#008000]'ListBox1

[/color][/size][size=2][/size][size=2][color=#008000]'

[/color][/size][size=2][/size][size=2][color=#0000ff]Me[/color][/size][size=2].ListBox1.Location = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Drawing.Point(8, 80)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].ListBox1.Name = "ListBox1"

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].ListBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].ListBox1.Size = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Drawing.Size(216, 251)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].ListBox1.TabIndex = 6

[/size][size=2][color=#008000]'

[/color][/size][size=2][/size][size=2][color=#008000]'Button1

[/color][/size][size=2][/size][size=2][color=#008000]'

[/color][/size][size=2][/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button1.Location = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Drawing.Point(80, 48)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button1.Name = "Button1"

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button1.Size = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Drawing.Size(72, 24)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button1.TabIndex = 7

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button1.Text = "Add"

[/size][size=2][color=#008000]'

[/color][/size][size=2][/size][size=2][color=#008000]'TextBox1

[/color][/size][size=2][/size][size=2][color=#008000]'

[/color][/size][size=2][/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBox1.Location = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Drawing.Point(48, 24)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBox1.Name = "TextBox1"

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBox1.Size = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Drawing.Size(144, 20)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBox1.TabIndex = 8

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBox1.Text = "Type Symbol to Add Here"

[/size][size=2][color=#008000]'

[/color][/size][size=2][/size][size=2][color=#008000]'Form_addremovelist

[/color][/size][size=2][/size][size=2][color=#008000]'

[/color][/size][size=2][/size][size=2][color=#0000ff]Me[/color][/size][size=2].AutoScaleBaseSize = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Drawing.Size(5, 13)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].ClientSize = [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Drawing.Size(232, 373)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Controls.Add([/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBox1)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Controls.Add([/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button1)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Controls.Add([/size][size=2][color=#0000ff]Me[/color][/size][size=2].ListBox1)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Controls.Add([/size][size=2][color=#0000ff]Me[/color][/size][size=2].Button2)

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Name = "Form_addremovelist"

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].Text = "Form_Infinity"

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].ResumeLayout([/size][size=2][color=#0000ff]False[/color][/size][size=2])

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2]#[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Region

[/color][/size][size=2][/size][size=2][color=#0000ff]Public[/color][/size][size=2] symbols [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] ArrayList

[/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Form_addremovelist_Load([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] [/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Load

symbols = LoadSymbolList()

ListBox1.DataSource = symbols

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Button2_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] Button2.Click

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ListBox1_SelectedIndexChanged([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] ListBox1.SelectedIndexChanged

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Button1_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] Button1.Click

[/size][size=2][color=#0000ff]If[/color][/size][size=2] PingSymbol(TextBox1.Text) [/size][size=2][color=#0000ff]Then

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] symbols2 [/size][size=2][color=#0000ff]As[/color][/size][size=2] ArrayList = symbols[/size]
[size=2]'''''''''''''''''''''''''''''''''''''
symbols2.Add([/size][size=2][color=#0000ff]Me[/color][/size][size=2].TextBox1.Text)[/size]
[size=2]'''''''''''''''''''''''''''''''''''''''
ListBox1.DataSource = symbols2

[/size][size=2][color=#0000ff]Else

[/color][/size][size=2]MsgBox("Not a valid Symbol")

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] TextBox1_TextChanged([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] TextBox1.TextChanged

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

End[/color][/size][size=2] [/size][size=2][color=#0000ff]Class

[/color][/size]

VINI VIDI VICI
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
I'm guessing that LoadSymbolList is a procedure. Maybe you could post that code? The problem is probably in that method, if it is a method.

Since you don't have line numbers , perhaps you could point out exactly which line errors out.
 

da_gibb

New member
Joined
Jun 8, 2004
Messages
3
Programming Experience
1-3
sorry, it is the line symbols2.Add(Me.TextBox1.Text) . With out the line, the program does not produce an error. It still produces an error when like changed to symbols2.Add("text") so it is something with the "symbols.add" part. the LoadSymbolList() function is in a module and just reads a txt file and enters each line into a arraylist which works correctly.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
Does the function "LoadSymbolList" return the array?
You would need to define it something like this:

VB.NET:
Public Function LoadSymbolList() as ArrayList
 

da_gibb

New member
Joined
Jun 8, 2004
Messages
3
Programming Experience
1-3
I did define the function like that and i figured out my problem.

I did not define symbols correctly. I guess I created the variable to point to an arraylist but did not actually create the arraylist object. I needed to add "= new arraylist" after my symbols declaration.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
That's not exactly correct. You should be able to declare the symbols arrayList without initializing it (the New method). Then setting it equal to LoadSymbolList would make it equal to whatever that function returned.

Your code above already has the declaration with an initializer.
 
Top Bottom