Error in Array in IE

T

Treetop

I get the following error in IE but not in Netscape. The code works,
and the error only shows up if you click on the icon in the bottom
left corner of the screen, next to where it says Error on Page... I
would like to know what the error means and how to fix it.

The site is at www.tricityarena.com

error ae[...].0' is null or not an object

here is my code


var today = new Date();
var dayarray=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
var montharray=new
Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov
","Dec")

document.write('<table>');

var ae=new Array();

ae[10]=new Array("2004/1/16 23:59:59","<b>Stick and Puck Hockey<\/b> -
11:30am-1:00pm");
ae[9]=new Array("2004/3/26 23:59:59","<b>Diesel Football Home
Opener<\/b> vs Casper, WY - 7:30 pm");


for (var i=ae.length-1;i>=0;i--)
{
var date = new Date(ae[0])
var year = 1900 + date.getYear()%1900 // < AD 3800
if (today.getTime() <= date.getTime()) {
document.write('<tr><td valign=top>' + dayarray[date.getDay()]+",
"+montharray[date.getMonth()]+" "+date.getDate()+", "+year+" " +
'&nbsp;&nbsp;</td><td> ' + ae[1] + '</td></tr>');
document.write('<tr><td colspan="2">&nbsp;</td></tr>');
}
}

document.write('</table>');
 
D

DB McGee

Treetop said:
I get the following error in IE but not in Netscape. The code works,
and the error only shows up if you click on the icon in the bottom
left corner of the screen, next to where it says Error on Page... I
would like to know what the error means and how to fix it.

The site is at www.tricityarena.com

error ae[...].0' is null or not an object

here is my code


[ ...snip ... ]

var ae=new Array();

ae[10]=new Array("2004/1/16 23:59:59","<b>Stick and Puck Hockey<\/b> -
11:30am-1:00pm");
ae[9]=new Array("2004/3/26 23:59:59","<b>Diesel Football Home
Opener<\/b> vs Casper, WY - 7:30 pm");


for (var i=ae.length-1;i>=0;i--)
{
var date = new Date(ae[0])
var year = 1900 + date.getYear()%1900 // < AD 3800
if (today.getTime() <= date.getTime()) {
document.write('<tr><td valign=top>' + dayarray[date.getDay()]+",
"+montharray[date.getMonth()]+" "+date.getDate()+", "+year+" " +
'&nbsp;&nbsp;</td><td> ' + ae[1] + '</td></tr>');
document.write('<tr><td colspan="2">&nbsp;</td></tr>');
}
}

document.write('</table>');


Your array contains ae[10] and ae[9] yet your 'for' loop starts and ends at
ae[1] - therein lies your problem.
 
M

Michael Winter

[indentation and new lines fixed; whitespace added between operators]
var today = new Date();
var dayarray = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
var montharray = new Array("Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec")

document.write('<table>');

var ae = new Array();

ae[10] = new Array("2004/1/16 23:59:59",
"<b>Stick and Puck Hockey<\/b> - 11:30am-1:00pm");
ae[9] = new Array("2004/3/26 23:59:59",
"<b>Diesel Football Home Opener<\/b> vs Casper, WY - 7:30 pm");

for (var i = ae.length - 1; i >= 0; i--) {
var date = new Date( ae[0] )
var year = 1900 + date.getYear() % 1900 // < AD 3800
if (today.getTime() <= date.getTime()) {
document.write('<tr><td valign=top>' + dayarray[date.getDay()] +
"," + montharray[date.getMonth()] + " " + date.getDate() +
", " + year + " " + '&nbsp;&nbsp;</td><td> ' + ae[1] +
'</td></tr>');
document.write('<tr><td colspan="2">&nbsp;</td></tr>');
}
}

document.write('</table>');


The algorithm, as implemented is not at fault, but your test data is. The
loop will start at 9 (the last element in the array) and attempt to
decrement to 0. This is fine, for a fully working example, but your test
data only goes to 8. As soon as the browser tries to resolve element 7, it
finds that there's no data and gives you the error. If you've only got two
pieces of test data, you can't create an array with ten elements.

Some suggestions:

- Make sure you add all the necessary semi-colons (;). There are a few
statements above that don't have them, and while technically legal (in
certain circumstances), it is still good style to include them.
- Your 'ae' array isn't much of an array. However, it would make a good
object. Consider revising it.
- Some of the forward-slashes that compose the closing tags you will
insert have been escaped, which is good. However, the majority haven't.
Make sure you change all instances of '</' to '<\/'.

Mike
 
C

Chris Riesbeck

Treetop said:
I get the following error in IE but not in Netscape.

Netscape is being overly friendly and hiding your bug.
error ae[...].0' is null or not an object

here is my code

var ae=new Array();

ae[10]=new Array("2004/1/16 23:59:59","<b>Stick and Puck Hockey<\/b> -
11:30am-1:00pm");
ae[9]=new Array("2004/3/26 23:59:59","<b>Diesel Football Home
Opener<\/b> vs Casper, WY - 7:30 pm");


for (var i=ae.length-1;i>=0;i--)
{
var date = new Date(ae[0])


You only put values in elements 9 and 10. So when i = 8, ae[8] is null
and ae[8][0] is undefined.
 
T

Treetop

Michael Winter said:
[indentation and new lines fixed; whitespace added between operators]
var today = new Date();
var dayarray = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
var montharray = new Array("Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec")

document.write('<table>');

var ae = new Array();

ae[10] = new Array("2004/1/16 23:59:59",
"<b>Stick and Puck Hockey<\/b> - 11:30am-1:00pm");
ae[9] = new Array("2004/3/26 23:59:59",
"<b>Diesel Football Home Opener<\/b> vs Casper, WY - 7:30 pm");

for (var i = ae.length - 1; i >= 0; i--) {
var date = new Date( ae[0] )
var year = 1900 + date.getYear() % 1900 // < AD 3800
if (today.getTime() <= date.getTime()) {
document.write('<tr><td valign=top>' + dayarray[date.getDay()] +
"," + montharray[date.getMonth()] + " " + date.getDate() +
", " + year + " " + '&nbsp;&nbsp;</td><td> ' + ae[1] +
'</td></tr>');
document.write('<tr><td colspan="2">&nbsp;</td></tr>');
}
}

document.write('</table>');


The algorithm, as implemented is not at fault, but your test data is. The
loop will start at 9 (the last element in the array) and attempt to
decrement to 0. This is fine, for a fully working example, but your test
data only goes to 8. As soon as the browser tries to resolve element 7, it
finds that there's no data and gives you the error. If you've only got two
pieces of test data, you can't create an array with ten elements.


by changing the array so the last number is a 0 now did the trick.
Thanks everyone
Some suggestions:

- Make sure you add all the necessary semi-colons (;). There are a few
statements above that don't have them, and while technically legal (in
certain circumstances), it is still good style to include them.

The day and month arrays have semi-colons on them now. Are there any
other statements that I have missed?
- Your 'ae' array isn't much of an array. However, it would make a good
object. Consider revising it.

I am affraid that I don't understand. What is an object? Is it an
element such as a VAR? ( I am very new to javascript. I learn by
taking other scripts and learning what they do, then create my own )
How is this different from an Array?
- Some of the forward-slashes that compose the closing tags you will
insert have been escaped, which is good. However, the majority haven't.
Make sure you change all instances of '</' to '<\/'.

I asked this group a while ago the difference between </ and <\/ and
the impression I got is that the <\/ is not used.
 
D

Douglas Crockford

- Your 'ae' array isn't much of an array. However, it would make a
I am affraid that I don't understand. What is an object? Is it an
element such as a VAR? ( I am very new to javascript. I learn by
taking other scripts and learning what they do, then create my own )
How is this different from an Array?

This is the worst possible way to learn this language. Most of the scripts out
there are dreadful. Much of what you are learning is wrong. You are missing some
really important knowledge. You are picking up some very bad habits.

There is a good book out there by Flanagan. Get the 4th edition of it.
Meanwhile, check this out to find the difference between an object and array:
http://www.crockford.com/javascript/survey.html

The object is one of the central ideas in JavaScript. Programming without is
like driving a car without knowing what the pedals do.
I asked this group a while ago the difference between </ and <\/ and
the impression I got is that the <\/ is not used.

You got the wrong impression. <\/ should be used in strings in html files. This
tool will help you find them all: http://www.crockford.com/javascript/lint.html
 
M

Michael Winter

The day and month arrays have semi-colons on them now. Are there any
other statements that I have missed?

The two statement that begin "var date=" and "var year =" at the start of
the for-loop.
I am affraid that I don't understand. What is an object? Is it an
element such as a VAR? ( I am very new to javascript. I learn by
taking other scripts and learning what they do, then create my own )
How is this different from an Array?

There are plenty of resources concerning the Object-Oriented Paradigm[1]
on the Internet and in books (Mr Crockford gave you one of each). Most
modern programming languages incorporate it in some way, including
JavaScript. It is certainly something worth while learning.

Briefly, objects are a way of abstracting something within a program. You
can take a complex, real-life system and break it down into properties
(attributes) that describe the entity, and the actions (methods) that it
can perform.

The arrays that your 'ae' array contained, represented events in a
recreation centre (of some description). Though not 'complex', those
events compose a real-life system, and they have properties; in this case
the time of the event and it's name.

Though possibly over-kill for something so simple, an object is a more
logical and appropriate representation for the events.
I asked this group a while ago the difference between </ and <\/ and
the impression I got is that the <\/ is not used.

To be honest, I wasn't sure if this was another old hold-over (like script
hiding). It certainly wouldn't have done anything negative, other than
adding a byte to the file size. However, Mr Crockford just cleared that up.

The only time that you can avoid escaping the '</' sequence in a string is
in external script files. If the script is inline, you need to escape to
'<\/'.

Mike

[1] You'll also find information under Object-oriented Programming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top