Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) 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 Expert
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu Islands
Posts: 810
Send a message via Skype™ to alfcen
Awards Showcase
Beta Tester 
Total Awards: 1
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: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
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 Expert
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu Islands
Posts: 810
Send a message via Skype™ to alfcen
Awards Showcase
Beta Tester 
Total Awards: 1
Default

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
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: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
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 Expert
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu Islands
Posts: 810
Send a message via Skype™ to alfcen
Awards Showcase
Beta Tester 
Total Awards: 1
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: 15,726
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
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: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
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 Expert
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu Islands
Posts: 810
Send a message via Skype™ to alfcen
Awards Showcase
Beta Tester 
Total Awards: 1
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 Expert
 
Join Date: Apr 2007
Location: Okinawa, Ryukyu Islands
Posts: 810
Send a message via Skype™ to alfcen
Awards Showcase
Beta Tester 
Total Awards: 1
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: 15,726
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
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 Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reload registry after change? mcflaytasche Questions (Windows Mobile) 9 04-28-2010 03:49 PM
WM6.1 Backlight Timeout (Registry) digitaldon37 Open Source Projects 3 11-20-2008 04:23 PM
Checking if registry key exists? N1c0_ds Questions (Windows Mobile) 3 10-21-2008 05:30 AM
Lettura del registry Raytracer Italian Forum 9 04-19-2008 01:03 PM
registry bdiscount Questions (Windows Mobile) 4 05-11-2007 02:29 PM


All times are GMT. The time now is 02:10 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0