NullReference error with Registry

alfcen

Well-Known Member
Licensed User
Hi Erel,

The code shows peculiar behaviour.
Suppose the subkey "File" does not exist. Assumingly, the variable ex should
be an empty string then. However, any kind of string manipulation
(StrIndexOf, SubString, StrLength, etc.) ends up in a NullReferenceException
error as if variable ex assumed an undefined type.
There is no error thrown if compiled without optimization.
The annoying thing is, error messages show no line number.

B4PPC 6.05, .NET 2, WM6 and PPC2003, as per text book.

Any thoughts you can share, please?


B4X:
'Arrays dim'd in Global
Dim ex
Reg.RootKey(Reg.rtCurrentUser)
subKeys()=Reg.GetSubKeyNames("Software\RBSoft")
For i = 0 To ArrayLen(SubKeys())-1
  ex=Reg.GetString("Software\RBSoft\" & SubKeys(i),"File")
  If StrIndexOf(ex,".exe",0) > -1 Then
  lbS.Add(ex & " (" & Reg.GetString("Software\RBSoft\" & SubKeys(i),"FriendlyName") & ")")
    alSort.Add(Reg.GetString("Software\RBSoft\" & SubKeys(i),"Path"))
  End If
Next
 

alfcen

Well-Known Member
Licensed User
Thanks Andrew, that does make sense to me.

I also tried

B4X:
If ex <> Chr(0) Then....
but it passes through and adds to the list.
Anyway, let's see what Erel comes up with.

Cheers
Robert
 

agraham

Expert
Licensed User
Some may not know that in .NET a "null" is just that. It is the absence of anything. It is not numeric zero or an empty string or the character chr(0) or anything else that might be thought of as "nothing". It really is an indicator of nothing - and it's occurrence usually indicates a coding bug or error of some sort.
 

alfcen

Well-Known Member
Licensed User
You mentioned in another thread that .NET is the future :)
Seriously, thanks for the explanation.
If Null is a total void then Chr(0) won't help.
 

alfcen

Well-Known Member
Licensed User
I was looking for a solution in Help under Keywords/String but that was
the wrong place. With IsNull the problem is gone. I will keep that trick in mind :)

Thanks so much, Erel!!!

Andrew, thanks for your directions!
 

alfcen

Well-Known Member
Licensed User
Hi Erel,
Sorry to get back to you again on this issue.
The code below throws an

"Specified argument was out of the range of valid values." error.

The values in "Path" contain strings such as: "\SD Card\RBS Taiyoukei\taiyokei.exe". All entries are plain text and are present.

Now, the code runs without optimization on PPC 2003 and WM6.
It runs optimized under WM6.
It does NOT run optimized under PPC 2003.

It is reasonable to assume that the old compiler and WM6 are more
tolerant to something I haven't figured out yet.

Any thoughts you can share with me ?

Thanks
Robert

B4X:
Sub mnuSearchAlfcen_Click
  Dim ex
  ErrorLabel(alfcen)
  lbS.Clear 'ListBox
  alSort.Clear 'Arraylist
  Reg.RootKey(Reg.rtCurrentUser)
  subKeys()=Reg.GetSubKeyNames("Software\RBSoft")
  For i = 0 To ArrayLen(SubKeys())-1
    ex=Reg.GetString("Software\RBSoft\" & SubKeys(i),"File")
    If IsNull(ex) = false AND ex <> "" Then
      lbS.Add(ex & " (" & Reg.GetString("Software\RBSoft\" & subKeys(i),"FriendlyName") & ")")
      alSort.Add(Reg.GetString("Software\RBSoft\" & subKeys(i),"Path"))  'ERROR IS THROWN HERE. No error if I REM this line
    End If
  Next
  Return
alfcen:
  Msgbox("Not all entries could be read!"," alfcen Software",cMsgBoxNone, cMsgBoxAsterisk)
End Sub
 

alfcen

Well-Known Member
Licensed User
alSort.Add(Reg.GetString("Software\RBSoft\" & subKeys(i),"Path"))

There is a note at the end of the line truncated in the code window.
 
Top