HELP: Object Reference Not Set To An Instance Of An Object

G

Guest

Hi,

I am writing a small app to check for the existence of files on remote
workstations. GUI is great, but when I click to check the workstations, the
following error is displayed. The point of failure is highlighted within the
code sample. I beleive the problem is to do with the arraylist within the
structure, but have no proof or idea of how to fix the problem. I have tried
changing the structure into classes, writing a text string instead of an
InstCheck instance, but always get the same error.

Any help would be appreciated.

Many thanks,

Martin


ERROR MESSAGE :

An unhandled exception of type 'System.NullReferenceException' occurred in
StoreWorkstationSoftwareInstallationChecker.exe

Additional information: Object reference not set to an instance of an object.


CODE SAMPLE :

Public Structure InstCheck
Public Application As String
Public Installed As Boolean
End Structure

Private Structure Results
Public Client As String
Public Installations As ArrayList

Private Function AllOK() As Boolean
Dim ReturnString As Boolean = True
Dim Install As InstCheck
For Each Install In Installations
If Not Install.Installed Then
ReturnString = False
End If
Next
Return ReturnString
End Function

Public Overrides Function ToString() As String
Dim ReturnString As String
ReturnString = Client & " " & AllOK()
Return ReturnString
End Function

Public Sub AddApp(ByVal App As InstCheck)
Dim temp As Integer
temp = Installations.Add(App) ß-- ERROR HERE
End Sub
End Structure


Private Sub ibCheckClients_Click() Handles ibCheckClients.Click
Dim ClientToCheck As String
Dim AppFile As String
Dim AppPath As String
Dim ClientPath As String
Dim ClientResult As Results
For Each ClientToCheck In lbClientsToCheck.Items
ClientResult.Client = ClientToCheck
ClientPath = "\\" & ClientToCheck & "\c$\StrSupport\SW_INSTALLED\"
For Each AppFile In lbFilesToCheck.Items
Dim AppResult As InstCheck
AppResult.Application = AppFile
AppPath = ClientPath & AppFile
If File.Exists(AppPath) Then
AppResult.Installed = True
Else
AppResult.Installed = False
End If
ClientResult.AddApp(AppResult)
Next
lbResults.Items.Add(ClientResult)
Next
End Sub
 
P

Peter Proost

After a quick look, I think Public Installations As ArrayList should be
Public Installations as new Arraylist

hth Peter
 
G

Guest

Nope. You can't use the NEW keyword for a member of a structure. I had
already tried this (sorry, forgot to mention that). Would this work as a
class instead? (I'll go and try it).
 
G

Guest

Works fine with the NEW keyword when its a class instead of a structure. Also
had to play around with the line "Dim ClientResult As Results" becoming "Dim
ClientResult As New Results". Now all working fine.

Thanks guys for helping me in the right direction.

Regards,

Martin
 
G

Guest

When Results is a class instead of a structure, the NEW keyword can be used
when declaring the Installations arraylist. I also had to amend the
decleration of clientResult to include the NEW keyword. Everything is now
working fine.

Thanks for helping me in the right direction guys.

Regards,

Martin


FINAL CODE SAMPLE :

Public Structure InstCheck
Public Application As String
Public Installed As Boolean
End Structure

Private Class Results
Public Client As String
Public Installations As New ArrayList

Private Function AllOK() As Boolean
Dim ReturnString As Boolean = True
Dim Install As InstCheck
For Each Install In Installations
If Not Install.Installed Then
ReturnString = False
End If
Next
Return ReturnString
End Function

Public Overrides Function ToString() As String
Dim ReturnString As String
ReturnString = Client & " " & AllOK()
Return ReturnString
End Function

Public Sub AddApp(ByVal App As InstCheck)
Dim temp As Integer
temp = Installations.Add(App)
End Sub
End Class

Private Sub ibCheckClients_Click() Handles ibCheckClients.Click
Dim ClientToCheck As String
Dim AppFile As String
Dim AppPath As String
Dim ClientPath As String
For Each ClientToCheck In lbClientsToCheck.Items
Dim ClientResult As New Results
ClientResult.Client = ClientToCheck
ClientPath = "\\" & ClientToCheck & "\c$\StrSupport\SW_INSTALLED\"
For Each AppFile In lbFilesToCheck.Items
Dim AppResult As InstCheck
AppResult.Application = AppFile
AppPath = ClientPath & AppFile
If File.Exists(AppPath) Then
AppResult.Installed = True
Else
AppResult.Installed = False
End If
ClientResult.AddApp(AppResult)
Next
lbResults.Items.Add(ClientResult)
Next
End Sub
 

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