Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Questions & Help Needed
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Questions & Help Needed Post any question regarding Basic4ppc.


NullReference error with Registry


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2008, 09:45 AM
alfcen's Avatar
Basic4ppc Veteran
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu
Posts: 424
Default NullReference error with Registry

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
Reply With Quote
  #2 (permalink)  
Old 03-31-2008, 10:19 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,368
Awards Showcase
Forum Contributer 
Total Awards: 1
Default

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.
Reply With Quote
  #3 (permalink)  
Old 03-31-2008, 10:33 AM
alfcen's Avatar
Basic4ppc Veteran
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu
Posts: 424
Default

Thanks Andrew, that does make sense to me.

I also tried

Code:
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
Reply With Quote
  #4 (permalink)  
Old 03-31-2008, 10:45 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,368
Awards Showcase
Forum Contributer 
Total Awards: 1
Default

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.
Reply With Quote
  #5 (permalink)  
Old 03-31-2008, 11:16 AM
alfcen's Avatar
Basic4ppc Veteran
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu
Posts: 424
Default

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.
Reply With Quote
  #6 (permalink)  
Old 03-31-2008, 02:01 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 2,845
Default

Did you try to first test the value with IsNull ?
Reply With Quote
  #7 (permalink)  
Old 03-31-2008, 02:10 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 1,368
Awards Showcase
Forum Contributer 
Total Awards: 1
Default

Quote:
Originally Posted by Erel View Post
Did you try to first test the value with IsNull ?
You know, I never even noticed that existed until you pointed it out
Reply With Quote
  #8 (permalink)  
Old 03-31-2008, 02:25 PM
alfcen's Avatar
Basic4ppc Veteran
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu
Posts: 424
Default

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!
Reply With Quote
  #9 (permalink)  
Old 04-08-2008, 07:15 AM
alfcen's Avatar
Basic4ppc Veteran
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu
Posts: 424
Default

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
Reply With Quote
  #10 (permalink)  
Old 04-08-2008, 07:52 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 2,845
Default

Which line raises the error?
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Lettura del registry Raytracer Italian Forum 9 04-19-2008 01:03 PM
Registry to Tree View willisgt Questions & Help Needed 0 02-20-2008 04:17 PM
reading and editing registry Dr. Feelgood Questions & Help Needed 3 02-16-2008 07:18 AM
Reload registry after change? mcflaytasche Questions & Help Needed 8 06-13-2007 07:50 PM
registry bdiscount Questions & Help Needed 4 05-11-2007 02:29 PM


All times are GMT. The time now is 07:41 AM.


Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0