The moose likes HTML, CSS and JavaScript and the fly likes uninitialized default global variable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "uninitialized default global variable" Watch "uninitialized default global variable" New topic
Author

uninitialized default global variable

Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 247


When gva is called, there is an error, and the problem is the variable scd. I assumed that scd would be initialized to an empty array when the page loads, but it doesn't seem to happen that way.
The variable scd is not initialized to anything in any function, but I assumed that scd would get the default value of an empty array.
I want to initialize scd only once, so I don't want to put the initialization in any function, because functions get called more than once.
When a variable is not initialized yet, what is it? A null? A default? A what?
And how can I check with code if it's initialized or not?
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

If line 01 is at the top level (not within a function) it will create and initialize a global variable named scd. (Not the best name choice, in my opinion.)

If the function is called after this initialization takes place, there should be no error.

You say "there is an error". Keeping the nature of the error a secret doesn't help us help you. (Please read ItDoesntWorkIsUseless.)

Where/when is the function gva() (please consider better naming) called? If it's before the initialization, yeah, there will be a problem.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 247


HTML is:
<INPUT TYPE="button" VALUE="Check" onclick="check()">


<TEXTAREA ROWS="10" ID="progress" COLS="50"></TEXTAREA>

I've passed Arrays, strings, integers to efgh(newStuff) and the result is always that what I pass gets displayed in the textarea called "progress". But in this case, nothing is displayed and there is a message at the bottom of the browser window that an error has happened, and also lines which follow the "problem line" efgh(scd); are not executed.
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

What's the exact nature of the error message. And you code still doesn't show how the code in question is being called.
Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 247
Thank you. I made a whole file which contained only the code lines which are relevant. Then I got no error message. So I realized that the problem must be caused somewhere else in the program. I looked at it closely and just above the line
scd=new Array();
there was this line:
rqp;
I thought that might be the problem. In java, you can declare field variables without initializing them, so I thought you could do that in javascript. Guess not. I tried changing
rqp;
to
rqp=0;
and now everything works fine.
What I think is that when the computer read the program, it stopped at the rqp line.
I'm just wondering, when does the browser read the global variable assignments? Is it when the page is loaded, or does the browser read the global variables only when the program attempts to reference a variable which does not yet have a value?
 
 
subject: uninitialized default global variable
 
Threads others viewed
Array Declaration
Sun Cirtification
How do arrays get created
Encapsulation and "getting" null references
one Construtor's problem?
MyEclipse, The Clear Choice