...

View Full Version : error: null or not an object in javascript



pashah72
07-11-2006, 01:04 PM
Hello

I am using a timer which goes down from 2 minutes to zero when 'jsp' is called. Timer is written in javascript. I am getting an error that document.form1.timerMin.value=<%=min%> is null or not an object.
Here is the code (t.jsp):
<%
/* Default timer */
int min = 2;
int sec = 5;

/* Check for timer params */
try {
String timerMin = request.getParameter("timerMin");
if (timerMin==null)timerMin="";
if (timerMin != null) {
min = Integer.parseInt(timerMin);
}
String timerSec = request.getParameter("timerSec");
if (timerSec==null)timerSec="";
if (timerSec != null) {
sec = Integer.parseInt(timerSec);
}
}

catch(Exception m) {
System.out.println("generated exception..............:"+m);
}

%>

<script language="JavaScript" type="text/javascript">
<!--

/* Initialise timer */
var min = <%=min%>; // set the minutes
var sec = <%=sec%>; // set the seconds
function countDown() {
sec--;
if (sec == -1) {
sec = 59;
min--;
}

/* Write timer state to hidden form fields */
document.form1.timerMin.value=<%=min%>
document.form1.timerSec.value=<%=sec%>;

time = (min<=9 ? "0" + min : min) + " min and " + (sec<=9 ? "0" + sec : sec) + " sec ";
document.getElementById('theTime').innerHTML = time;

SD=window.setTimeout("countDown()", 1000);
if (min == 0 && sec == 0) {
window.clearTimeout(SD);
}
}

window.onload = countDown();
-->
</script>

<style type="text/css">
<!--
.timeClass {
font-family:arial,verdana,helvetica,sans-serif;
font-weight:normal;
font-size:10pt;
}
-->
</style>

<table width="100%">
<tr><td width="100%" align="right">
<span id="theTime" class="timeClass"></span>
</td></tr>
<form method="post" name="form1" id="form1" action="t.jsp"
<td width="50%" valign="middle">
<input type="hidden" name="timerMin" value="<%=min%>">
<input type="hidden" name="timerSec" value="<%=sec%>">
<div align="center"><a href="javascript:form1.submit()" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image3','','/lms/images/submit_0.gif',1)"><img name="Image3" border="0" src="/lms/images/submit_0.gif" width="70" height="30"></a></div>
</td>

</form>

Looking forward for an early reply.:confused:

Thanks

VR2
07-11-2006, 01:17 PM
I think its because


document.form1.timerMin.value=<%=min%>

is executed as the page is downloaded, not after (or ON) the page load, where it SHOULD be.

Y'see, at the time you execute this - there is NO DOCUMENT. Thus "document" is undefined and trying to access any of its properties or methods will throw an exception.

Actually, my bad - the indentation threw me (lack of) - the countDown() function goes on longer doesn't it!

Ok so


document.form1.timerMin.value=<%=min%>

Is missing a semi colon. Also it should have quotes around the value, since its effectively string (as a form field value).

Other than that - I'm not understanding the code, since <%=min%> is always a static "hardcoded" number, why are you using it in your countDown loop? Should it be..


document.form1.timerMin.value= min;

as in the client side Javascript variable called "min"?

I think maybe using Java on the server and javascript on the client is kinda confusing to deal with!!!

Beagle
07-11-2006, 02:54 PM
if your going to be printing numbers from your JSP page you don't need quotes around <%=min%> but if you're using strings, you need "<%=min%>"

The semicolon is not a requirement at the end of statements in javascript.

You are missing the closing angle bracket on your form tag, this is invalid markup and could definitely cause you problems.

Also, if your script attempts to access an element before it is loaded, as the previous poster suggested, you will have an error, and it's best to begin your script onload.

pashah72
07-12-2006, 07:37 AM
I did like just putting min instead of <%=min%> or "<%=min%>", even then it didn't worked. I gave whole code, if U copy, paste it & run the script, actual error will be notice.
Looking forward for an early reply.

Beagle
07-12-2006, 03:36 PM
got it, it's this line:

window.onload = countDown();

Two problems:
1) it should be document.onload
2) it should be countDown; without the parens

with the parens, you're actually calling the function and assigning its return value to window.onload, which is not what you want, you want the function assigned, not the return of the function.

So make the line:

document.onload = countDown;

pashah72
07-13-2006, 11:24 AM
Hi Beagle
Thanks error got removed but timer is not shown on screen. Actually, countdown timer starts from 2 minutes (2.00) & ends at (0.0) . When jsp (t.jsp) is call, timer starts with 2:00 & goes upto 1:30 then I click submit & that jsp(t.jsp) is called again & timer starts from 1:30 & gets decremented & again I click submit & same jsp(t.jsp) is called again & so on. Though error is removed but countdown timer is not shown.
When I gave -->alert(sec--); it didn't executed that means timer is not working.
Looking forward for an early reply.
:confused:

_Aerospace_Eng_
07-13-2006, 11:32 AM
2 minutes in javascript is 120000 not 2 and 5 seconds in javascript is 5000 not 5.

pashah72
07-14-2006, 08:18 AM
Actually, my application is of online test. Student starts the test, timer is displayed, a Question with 3 options is displayed, selecting 1 option(radio button) he clicks submit & jsp(t.jsp) is called with different question & options with timer decremented. After selecting 1 option(radio button) he again clicks submit & that same jsp(t.jsp) is called with timer decremented & so on. So for e.g if I have set test for 2 minutes, 5 seconds, timer will decrement to zero. But after doing below changes as suggested by Beagle, I found timer is not functioning since alert message is also not show. So how to solve?:confused:
Looking forward for an early reply.
Thanks

pashah72
07-17-2006, 12:37 PM
Hey, no one gave answer to my query.:confused:

Beagle
07-17-2006, 02:51 PM
Try putting the countDown function back on window.onload, I think I was wrong about that one.

pashah72
07-18-2006, 08:25 AM
I tried window.onload even then error message is display that's why I said just copy paste the code & run it.
Looking forward for an early reply.
Thanks

Beagle
07-18-2006, 03:28 PM
I don't have a JSP setup anywhere, so I can't just copy your code.
Also, you haven't posted your upated code, so I can't test it anyway.

Please post all of your new code after the changes you've made so I can take a look at it.

pashah72
07-18-2006, 04:03 PM
No change in code except window.onload = countDown();

If I use window.onload = countDown; (without parenthesis) then no error but no display of timer that is timer (countdown function) is not excuted. If I give alert in countdown function, it doesn't show any alert messg.

If I use window.onload = countDown();(with parenthesis) then error messg of
document.form1.timerMin.value= min; is null or not an object is display.

So what's the way out?:confused:

Looking forward for an early reply.

Thanks

Beagle
07-18-2006, 04:24 PM
Delete this:


SD=window.setTimeout("countDown()", 1000);
if (min == 0 && sec == 0) {
window.clearTimeout(SD);
}
}

window.onload = countDown();


replace with:



var SD;
window.onload = function()
{
SD = window.setTimeout("countDown()", 1000);
}
-->
</script>


Try that, I think the problem is that I wasn't understanding everything your code was doing (I'm kind of rushed at work sometimes). This might cause your countdown to go on forever, but try it anyway, and get back to me.

pashah72
07-19-2006, 01:28 PM
Hi Beagle

I replace the code even then it didn't worked:confused: . Here is the complete code :

<script language="JavaScript" type="text/javascript">
<!--
<%
/* Default timer */
int min = 2;
int sec = 5;

/* Check for timer params */
try {
String timerMin = request.getParameter("timerMin");
if (timerMin==null)timerMin="";
System.out.println("timerMin..."+timerMin);
if (timerMin != null) {
min = Integer.parseInt(timerMin);
}
String timerSec = request.getParameter("timerSec");
if (timerSec==null)timerSec="";
System.out.println("timerSec..."+timerSec);
if (timerSec != null) {
sec = Integer.parseInt(timerSec);
}
}

catch(Exception m) {
System.out.println("generated exception..............:"+m);

}

%>
/* Initialise timer */
var min = <%=min%>; // set the minutes
alert (min);
var sec = <%=sec%>; // set the seconds
alert (sec);
alert("hi");
function countDown() {
sec--;
alert(sec--);
if (sec == -1) {
sec = 59;
min--;
}

/* Write timer state to hidden form fields */
alert(<%=min%>);

document.form1.timerMin.value=min;
document.form1.timerSec.value=sec;

time = (min<=9 ? "0" + min : min) + " min and " + (sec<=9 ? "0" + sec : sec) + " sec ";
document.getElementById('theTime').innerHTML = time;


var SD;
window.onload = function()
{
SD = window.setTimeout("countDown()", 1000);
}
}

window.onload = countDown;
-->
</script>

<style type="text/css">
<!--
.timeClass {
font-family:arial,verdana,helvetica,sans-serif;
font-weight:normal;
font-size:10pt;
}
-->
</style>
<span id="theTime" class="timeClass"></span>
<form method="post" name="form1" id="form1" action="t.jsp">
<input type="hidden" name="timerMin" value="<%=min%>">
<input type="hidden" name="timerSec" value="<%=sec%>">
</form>

Beagle
07-19-2006, 03:25 PM
delete this line, as I stated above:

window.onload = countDown;

pashah72
07-25-2006, 11:01 AM
I deleted the line, its not working. Anyway, forget it since lots of time is wasted. Thanks for everything.

cyruspak
08-22-2011, 02:58 AM
Can you tell me why my top:HIDDEN in my following order.htm file returns null or not an object. The other files which work with my order.htm file are javascript.htm and valucompl.htm.Thanks.

<HTML>
<BODY BGCOLOR=#A0D0E0 onKeyPress="JavasScript:top.HIDDEN.keyPressed()">
<TABLE>
<TR><TD><IMAGE SRC="comp2.jpg"></TD>
<FORM NAME=OrderForm>
<TD ALIGN=CENTER VALIGN=BOTTOM><H2>Welcome to the Val-U Computers Online Shopping Site</H2></TD>
<TD><IMAGE SRC="comp3.jpg"></TD></TR>
</TABLE>
<CENTER>
<BR><HR>
Press 'V' to view your shopping cart or click <A HREF="JavaScript:top.HIDDEN.viewCart();">here</A>
<BR><BR>
<TABLE BORDER=1 CELLSPACING=1 CELLPADDING=3 BGCOLOR=LIGHTYELLOW>
<TR><TD>Pentium&reg; III 650 MHz</TD>
<TD>Pentium&reg; III 650 MHz with 128 Megabytes of RAM, Network Card, 10 GB Hard Drive<BR>
<A HREF="JavaScript:top.HIDDEN.addtoCart('Pentium&reg; III 650 MHz', 950)">Add to Shopping Cart</A>
</TD>
<TD>$950</TD>
</TR>
<TR><TD>Pentium&reg; III 600 MHz</TD>
<TD>Pentium&reg; III 600 MHz with 128 Megabytes of RAM, Network Card, 10 GB Hard Drive<BR>
<A HREF="JavaScript:top.HIDDEN.addtoCart('Pentium&reg; III 600 MHz', 850)">Add to Shopping Cart</A>
</TD>
<TD>$850</TD>
</TR>
<TR><TD>Pentium&reg; III 550 MHz</TD>
<TD>Pentium&reg; III 550 MHz with 64 Megabytes of RAM, Network Card, 8 GB Hard Drive<BR>
<A HREF="JavaScript:top.HIDDEN.addtoCart('Pentium&reg; III 550 MHz', 800)">Add to Shopping Cart</A>
</TD>
<TD>$800</TD>
</TR>
<TR><TD>AMD Athlon™ 650 MHz</TD>
<TD>AMD Athlon™ 650 MHz with 128 Megabytes of RAM, Modem, 12 GB Hard Drive<BR>
<A HREF="JavaScript:top.HIDDEN.addtoCart('AMD Athlon™ 650 MHz', 875)">Add to Shopping Cart</A>
</TD>
<TD>$875</TD>
</TR>
<TR><TD>AMD Athlon™ 600 MHz</TD>
<TD>AMD Athlon™ 600 MHz with 128 Megabytes of RAM, Modem, 12 GB Hard Drive<BR>
<A HREF="JavaScript:top.HIDDEN.addtoCart('AMD Athlon™ 600 MHz', 825)">Add to Shopping Cart</A>
</TD>
<TD>$825</TD>
</TR>
<TR><TD>Pentium&reg; II 500 MHz</TD>
<TD>Pentium&reg; II 500 MHz with 128 Megabytes of RAM, Network Card, 8 GB Hard Drive<BR>
<A HREF="JavaScript:top.HIDDEN.addtoCart('Pentium&reg; II 500 MHz', 650)">Add to Shopping Cart</A>
</TD>
<TD>$650</TD>
</TR>
<TR><TD>AMD-K6&reg; 500 MHz</TD>
<TD>AMD-K6&reg; 500 MHz with 64 Megabytes of RAM, Modem, 6 GB Hard Drive<BR>
<A HREF="JavaScript:top.HIDDEN.addtoCart('AMD-K6&reg; 500 MHz/Modem', 550)">Add to Shopping Cart</A>
</TD>
<TD>$550</TD>
</TR>
<TR><TD>AMD-K6&reg; 500 MHz</TD>
<TD>AMD-K6&reg; 500 MHz with 64 Megabytes of RAM, Network Card, 6 GB Hard Drive<BR>
<A HREF="JavaScript:top.HIDDEN.addtoCart('AMD-K6&reg; 500 MHz/Network', 575)">Add to Shopping Cart</A>
</TD>
<TD>$575</TD>
</TR>
</TABLE>
</FORM>
E-mail us for more information: <A HREF="mailto:valucomputers@hotmail.com"><FONT SIZE="-1" COLOR="BLUE" FACE="Arial">valucomputer@hotmail.com</FONT></A>
<BR><BR>
</CENTER>
</BODY>
</HTML>

Philip M
08-22-2011, 07:31 AM
Instead of reviving an ancient and long-resolved thread, you should prefer to start a new thread of your own. And I am afraid that you have not given enough information to enable us to help you. The simple answer is that HIDDEN is not defined.

BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.


Straight On For Festival Of History And Diverted Traffic - road sign in Northampton



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum