Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
SCTR
Visitor

Use of uninitialized variable

How can I gracefully tell if a variable is initialized or not and handle it appropriately?

I'm modifying the example "Video Player" code which has a function in generalUtils.brs like so:

'******************************************************
'Walk an AA and print it
'******************************************************
Sub PrintAA(aa as Object)
print "---- AA ----"
if aa = invalid
print "invalid"
return
endif
mytype = type(aa)
'TODO: not sure how to do this in Brightscript
'else if mytype = "<uninitialized>"
' print "uninitialized"
' return
if mytype = "roAssociativeArray"
cnt = 0
for each e in aa
x = aa[e]
PrintAny(0, e + ": ", aa[e])
cnt = cnt + 1
next
if cnt = 0
PrintAny(0, "Nothing from for each. Looks like :", aa)
endif
else
print mytype
endif
print "------------"
End Sub

And in the appPosterScreen.brs I'm trying to do this:

myshows = getShowsForCategoryItem(category, m.curCategory)
PrintAA(myshow)
'PrintList(myshow)
screen.SetContentList(myshows)
screen.Show()

And it STB every time I try to output that as either PrintAA or PrintList:

created feed connection for http://dev.roku.example.com/xml/new_scenes_today.xml
url: http://dev.roku.example.com/xml/new_scenes_today.xml
Request Time: 18
Show Feed Parse Took : 60
---- AA ----
BrightScript Micro Debugger.
Enter any BrightScript statement, debug commands, or HELP.

Current Function:
440: Sub PrintAA(aa)
441: print "---- AA ----"
442: if aa = invalid
443: print "invalid"
444: return
445: endif
446: mytype = type(aa)
447: 'TODO: not sure how to do this in Brightscript
448: 'else if mytype = "<uninitialized>"
449: ' print "uninitialized"
450: ' return
451: if mytype = "roAssociativeArray"
452: cnt = 0
453: for each e in aa
454: x = aa[e]
455: PrintAny(0, e + ": ", aa[e])
456: cnt = cnt + 1
457: next
458: if cnt = 0
459: PrintAny(0, "Nothing from for each. Looks like :", aa)
460: endif
461: else
462: print mytype
463: endif
464: print "------------"
465: End Sub
Use of uninitialized variable. (runtime error &he9) in .../pkg:/source/generalUtils.brs(442)

442: if aa = invalid
Backtrace:
Function printaa(aa As ) As
file/line: /tmp/plugin/FIAAAAnKZ.../pkg:/source/generalUtils.brs(442)
Function showposterscreen(screen As <uninitialized>, category As <uninitialized>) As Integer
file/line: /tmp/plugin/FIAAAAnKZLFS/pkg:/source/appPosterScreen.brs(57)
Function displaycategoryposterscreen(category As <uninitialized>) As
file/line: /tmp/plugin/FIAAAAnKZLFS/pkg:/source/appHomeScreen.brs(81)
Function showhomescreen(screen As ) As Integer
file/line: /tmp/plugin/FIAAAAnKZLFS/pkg:/source/appHomeScreen.brs(56)
Function main() As
file/line: /tmp/plugin/FIAAAAnKZLFS/pkg:/source/appMain.brs(19)

Local Variables:
aa &h0000 <uninitialized> val:Uninitialized
global &h0020 rotINTERFACE:ifGlobal
m &h0010 bsc:roAssociativeArray, refcnt=6
mytype &h0000 <uninitialized> val:Uninitialized
cnt &h0000 <uninitialized> val:Uninitialized
e &h0000 <uninitialized> val:Uninitialized
x &h0000 <uninitialized> val:Uninitialized
BrightScript Debugger>

Isn't there just a simple way to output what a variable is (like in PHP there is var_dump($aa))?!

I know "myshows" has stuff in it, as without the PrintAA/PrintList the page renders fine and no error happens. So why does it say "uninitialized"?
0 Kudos
10 REPLIES 10
SCTR
Visitor

Re: Use of uninitialized variable

Okay, so I found out part of my problem, I was using "myshow" and "myshows" (note the 's')

But the other questions remain as does the concept of what I was trying to do.

How can I handle uninitialized variables gracefully?

Is there a better variable dumper than making routines/functions/methods to do this all over the place?
0 Kudos
SCTR
Visitor

Re: Use of uninitialized variable

Which leads me to another feature request for this Eclipse plugin.

In Zend Studio Eclipse (and possibly others, I can't remember right now), when I click on a variable name, it highlights all the 'read' and 'write' instances of that name within that scope. So it shows a solid yellow box around the write instances and a dotted green box around the read instances. This makes it really easy to spot where a variable is used, and in untyped, undeclared languages like PHP and Brightscript where you can just create a variable on the fly, it REALLY helps to see stupid typos like the one above.
0 Kudos
Trevor
Visitor

Re: Use of uninitialized variable

Just to answer what I think you already figured out, but checking if the variable = invalid will tell you. As far as the debugger, you can find the commands here: http://sdkdocs.roku.com/display/sdkdoc42/Brightscript+Debug+Console
*** Trevor Anderson - bloggingwordpress.com - moviemavericks.com ***
0 Kudos
malloys
Visitor

Re: Use of uninitialized variable

"SCTR" wrote:
Which leads me to another feature request for this Eclipse plugin.

In Zend Studio Eclipse (and possibly others, I can't remember right now), when I click on a variable name, it highlights all the 'read' and 'write' instances of that name within that scope. So it shows a solid yellow box around the write instances and a dotted green box around the read instances. This makes it really easy to spot where a variable is used, and in untyped, undeclared languages like PHP and Brightscript where you can just create a variable on the fly, it REALLY helps to see stupid typos like the one above.


Good idea... in fact this has already been suggested by someone else and is already on the todo list.

FYI, for future reference, please post Eclipse plugin issues and suggestions to this thread: viewtopic.php?f=34&t=42578
That will ensure these kinds of things will get tracked and subsequently addressed in future plugin releases.

Cheers
0 Kudos
EnTerr
Roku Guru

Re: Use of uninitialized variable

"Trevor" wrote:
Just to answer what I think you already figured out, but checking if the variable = invalid will tell you.

Just to put the record straight - no, that is not the case - you cannot check by comparing to invalid:
BSD> if someUninitializedVariable = invalid then print "it works"
Use of uninitialized variable. (runtime error &he9) in $LIVECOMPILE(105)
BSD> var
global &h0020 rotINTERFACE:ifGlobal
m &h0010 bsc:roAssociativeArray, refcnt=2
someuninitializedvariable &h0000 <uninitialized> val:Uninitialized

The confusion was probably caused by arrays and dictionaries returning "invalid" when non-existent element is requested - which IMO is a good thing. OTOH, failing of expressions with uninitialized variables is also a good thing.

I ran into this thread looking for discussion on "unitialized" and "invalid". I am considering a piece of code which may run 0 or many times and want to avoid creating a helper object when it won't be used (repeat=0) or re-creating it repeatedly in the loop. So i was looking for simple way to do it (like in loop check if null and if so, construct it).
0 Kudos
TheEndless
Channel Surfer

Re: Use of uninitialized variable

"EnTerr" wrote:
"Trevor" wrote:
Just to answer what I think you already figured out, but checking if the variable = invalid will tell you.

Just to put the record straight - no, that is not the case - you cannot check by comparing to invalid:
BSD> if someUninitializedVariable = invalid then print "it works"
Use of uninitialized variable. (runtime error &he9) in $LIVECOMPILE(105)
BSD> var
global &h0020 rotINTERFACE:ifGlobal
m &h0010 bsc:roAssociativeArray, refcnt=2
someuninitializedvariable &h0000 <uninitialized> val:Uninitialized

The confusion was probably caused by arrays and dictionaries returning "invalid" when non-existent element is requested - which IMO is a good thing. OTOH, failing of expressions with uninitialized variables is also a good thing.

I ran into this thread looking for discussion on "unitialized" and "invalid". I am considering a piece of code which may run 0 or many times and want to avoid creating a helper object when it won't be used (repeat=0) or re-creating it repeatedly in the loop. So i was looking for simple way to do it (like in loop check if null and if so, construct it).

You can check the Type() of an uninitialized variable without crashing. It should return "<uninitialized>".
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
greubel
Visitor

Re: Use of uninitialized variable

I ran into a problem today where type(val) returned "" and the debugger said it was type "UNKNOWN"
This was on a 3.x box
0 Kudos
EnTerr
Roku Guru

Re: Use of uninitialized variable

"greubel" wrote:
I ran into a problem today where type(val) returned "" and the debugger said it was type "UNKNOWN"
This was on a 3.x box
Oh that's fun. Can you re-produce in code snippet? Besides some chance of RokuCo fixing it, i was doing survey of the types and have not ran into this yet.

"TheEndless" wrote:
You can check the Type() of an uninitialized variable without crashing. It should return "<uninitialized>".
Yes, i considered that option for a while but have gut feeling type() being exactly the string "<uninitialized>" is less guaranteed than other features of the no-spec language that BS is. Think i will settle for an explicit assignment and check, feels less likely to mutate:
helper = invalid
while loop:
...
if helper = invalid then helper = createObject(...)
helper.reuse()
...
end while
0 Kudos
greubel
Visitor

Re: Use of uninitialized variable

Appears you can pass anything to AddReplace() !
All this talk about types caused me to get rid of all my checks for :
if Type(x) = "String" or Type(x) = "roString"
I replace all my checks with subroutines, IsString(),IsInt(),IsFloat(),and IsBoolean()
Saved a lot of code. But like I said my package worked fine on 5.x but fell down with 3.x

Before I found the actual bug, I put in a check : if Len(type(val)) > 0
So I guess the Len() was zero because it worked !

Oh, if you take out the call to IsString(), the Roku hangs on the "? type( o.time )"

o = {}
? "1"
o.AddReplace( "time", t ) <-------- "t" is not defined
? "2"
IsString( o.time )
? type( o.time )
end

------ Running ------
17:41:02.812 17:41:02.815
1
2
BrightScript Micro Debugger.
Enter any BrightScript statement, debug commands, or HELP.

Current Function:
2876: Function IsString( val as object ) as boolean
2877: if instr( 1, Type(val), "String" ) > 0 return true
2878: return false
2879: End Function
Type Mismatch. (runtime error &h18) in ...AA6yq1jA/pkg:/source/Subs.brs(2876)

2876: Function IsString( val as object ) as boolean
Backtrace:
Function isstring(val As <uninitialized>) As Boolean
file/line: /tmp/plugin/F...AA6yq1jA/pkg:/source/Subs.brs(2877)
Function runuserinterface(param As <uninitialized>) As
file/line: /tmp/plugin/FEAAAA6yq1jA/pkg:/source/Main.brs(107)

Local Variables:
val &h4000 (UNKNOWN!)
global &h0020 rotINTERFACE:ifGlobal
m &h0010 bsc:roAssociativeArray, refcnt=3
BrightScript Debugger> BrightScript Debugger>
0 Kudos