NullReference error with Registry

Discussion in 'Questions (Windows Mobile)' started by alfcen, Mar 31, 2008.

  1. alfcen

    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?


    Code:
    '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
     
  2. agraham

    agraham Expert Licensed User

    It looks like a a bug in GetString. It is returning null rather than an empty string. The IDE can cope with a null but the compiled code chokes on it.
     
  3. alfcen

    alfcen Well-Known Member Licensed User

    Thanks Andrew, that does make sense to me.

    I also tried

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

    Cheers
    Robert
     
  4. agraham

    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.
     
  5. alfcen

    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.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Did you try to first test the value with IsNull ?
     
  7. agraham

    agraham Expert Licensed User

    :sign0161: You know, I never even noticed that existed until you pointed it out :(
     
  8. alfcen

    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!
     
  9. alfcen

    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

    Code:
    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
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Which line raises the error?
     
  11. alfcen

    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.
     
Loading...