Error: Object reference not set to an instance of an object

G

GrandpaB

When I attempt to run the following code I get the
following error:

An unhandled exception of
type 'System.NullReferenceException' occurred in Art.exe
Additional information: Object reference not set to an
instance of an object.

Being new to VB.Net I'm stumped. I have attempted
several versions of the attached code, all with the same
result. Help is unfortunately written in an
unintelligible language; your assistance would be greatly
appreciated.

I am attempting to add information in a class named Art
to an arraylist named ArtList with the Button1_click
event.

'** MODULE1 **
Imports System
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Soap
<[Serializable]()> Public Class Art
Public Title As String
Public Des As String
Public Room As String
Public Location As String
Public Value As Integer
Public Bequeth As String
Public Picture As String
End Class
Module Module1
Public ArtList As ArrayList
End Module

' ** FORM1 **
Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) Handles
Button1.Click
Dim myArt As New Object
myArt = New Art
With myArt
.Title = tbTitle.Text
.Des = tbDesc.Text
.Location = tbLocation.Text
.Room = cbRoom.SelectedItem
.Value = tbValue.Text
.Picture = tbPicture.Text
.Bequeth = tbBequeth.Text
End With
ArtList.Add(myArt)
SaveFile()
End Sub
End Class
 
C

Chris

Module Module1
Public ArtList As ArrayList
End Module

should read

Module Module1
Public ArtList As new ArrayList
End Module

any time you get that error, find the line that causes it and one object it
that line, the object hasn't been created, usually with new. (a function
could return an object to create it as well)

Chris
 
G

GrandpaB

Chris,

Thanks not only for the solution to my error message, but
for the advice on avoiding future problems.

GrandpaB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top