2 Replies - 949 Views - Last Post: 26 August 2013 - 06:42 AM Rate Topic: -----

#1 brco900033  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-August 13

NullReferenceException / Listing all software in listview

Posted 25 August 2013 - 01:27 PM

I have another question already. I'm getting an error in my code snippet and I don't know why because everything seems good. It just needs to list the name and the installation path of the application in a listviw. The following is the code I use:
Dim strSoftwareKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products"
        Using rk As RegistryKey = Registry.LocalMachine.OpenSubKey(strSoftwareKey)
            For Each skName In rk.GetSubKeyNames
                Dim name = Registry.LocalMachine.OpenSubKey(strSoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("DisplayName")
                Dim installocation = Registry.LocalMachine.OpenSubKey(strSoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("InstallLocation")
                If name.ToString <> String.Empty Then
                    Dim list As New ListViewItem
                    list.Text = name.ToString
                    list.SubItems.Add(installocation.ToString)
                    ListView1.Items.Add(list)
                End If
            Next
        End Using



The error persists on the line
list.SubItems.Add(installocation.ToString)

and is a NullReferenceException (Object reference not set to an instance of an object.)


Because that line is not needed and just an option I remove him and test the code again. And again I have the NullReferenceException but on another line:
If name.ToString <> String.Empty Then



NOTE: I have imported the required namespaces, so that should be no problem...

Sincerely, Brecht

Is This A Good Question/Topic? 0
  • +

Replies To: NullReferenceException / Listing all software in listview

#2 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 297
  • View blog
  • Posts: 1,525
  • Joined: 11-May 12

Re: NullReferenceException / Listing all software in listview

Posted 25 August 2013 - 04:01 PM

It's because you haven't given your objects a data type. Instead of
Dim name = Registry.LocalMachine.OpenSubKey(strSoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("DisplayName")



replace it with
Dim name As String = Registry.LocalMachine.OpenSubKey(strSoftwareKey).OpenSubKey(skName).OpenSubKey("InstallProperties").GetValue("DisplayName")



Do the same for install location. Also if nothing exists in that registry entry it is going to return nothing and You need to check for that before you compare the string to something otherwise you will get the same error.

I would also recommend highly that you turn Option Strict On

This post has been edited by ybadragon: 25 August 2013 - 04:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 brco900033  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 12-August 13

Re: NullReferenceException / Listing all software in listview

Posted 26 August 2013 - 06:42 AM

Thanks, it works now and thanks for the advice on using the Otpion Strict. I always use Option Explicit but not Strict.

Double thanks, Brecht!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1