Error in Array in IE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Treetop

    #1

    Error in Array in IE

    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","Mo n","Tue","Wed", "Thu","Fri","Sa t")
    var montharray=new
    Array("Jan","Fe b","Mar","Apr", "May","Jun","Ju l","Aug","Sep", "Oct","Nov
    ","Dec")

    document.write( '<table>');

    var ae=new Array();

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


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

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




  • DB McGee

    #2
    Re: Error in Array in IE

    "Treetop" <treetop@netfro nt.net> wrote in message
    news:buopv7$kcp vj$1@ID-221536.news.uni-berlin.de...[color=blue]
    > 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[/color]


    [ ...snip ... ]

    [color=blue]
    >
    > var ae=new Array();
    >
    > ae[10]=new Array("2004/1/16 23:59:59","<b>S tick and Puck Hockey<\/b> -
    > 11:30am-1:00pm");
    > ae[9]=new Array("2004/3/26 23:59:59","<b>D iesel Football Home
    > Opener<\/b> vs Casper, WY - 7:30 pm");
    >
    >
    > for (var i=ae.length-1;i>=0;i--)
    > {
    > var date = new Date(ae[i][0])
    > var year = 1900 + date.getYear()% 1900 // < AD 3800
    > if (today.getTime( ) <= date.getTime()) {
    > document.write( '<tr><td valign=top>' + dayarray[date.getDay()]+",
    > "+montharra y[date.getMonth()]+" "+date.getDate( )+", "+year+" " +
    > '&nbsp;&nbsp; </td><td> ' + ae[i][1] + '</td></tr>');
    > document.write( '<tr><td colspan="2">&nb sp;</td></tr>');
    > }
    > }
    >
    > document.write( '</table>');[/color]

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


    Comment

    • Michael Winter

      #3
      Re: Error in Array in IE

      On Thu, 22 Jan 2004 09:25:15 -0600, Treetop <treetop@netfro nt.net> wrote:
      [indentation and new lines fixed; whitespace added between operators]
      [color=blue]
      > var today = new Date();
      > var dayarray = new Array("Sun","Mo n","Tue","Wed", "Thu","Fri","Sa t")
      > var montharray = new Array("Jan","Fe b","Mar","Apr", "May","Jun" ,
      > "Jul","Aug","Se p","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[i][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[i][1] +
      > '</td></tr>');
      > document.write( '<tr><td colspan="2">&nb sp;</td></tr>');
      > }
      > }
      >
      > document.write( '</table>');[/color]

      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

      --
      Michael Winter
      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

      Comment

      • Chris Riesbeck

        #4
        Re: Error in Array in IE

        "Treetop" <treetop@netfro nt.net> wrote in message news:<buopv7$kc pvj$1@ID-221536.news.uni-berlin.de>...[color=blue]
        > I get the following error in IE but not in Netscape.[/color]

        Netscape is being overly friendly and hiding your bug.
        [color=blue]
        > 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>S tick and Puck Hockey<\/b> -
        > 11:30am-1:00pm");
        > ae[9]=new Array("2004/3/26 23:59:59","<b>D iesel Football Home
        > Opener<\/b> vs Casper, WY - 7:30 pm");
        >
        >
        > for (var i=ae.length-1;i>=0;i--)
        > {
        > var date = new Date(ae[i][0])[/color]

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

        Comment

        • Treetop

          #5
          Re: Error in Array in IE


          "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          news:opr16p8qq8 5vklcq@news-text.blueyonder .co.uk...[color=blue]
          > On Thu, 22 Jan 2004 09:25:15 -0600, Treetop <treetop@netfro nt.net>[/color]
          wrote:[color=blue]
          >
          >[indentation and new lines fixed; whitespace added between[/color]
          operators][color=blue]
          >[color=green]
          > > var today = new Date();
          > > var dayarray = new[/color][/color]
          Array("Sun","Mo n","Tue","Wed", "Thu","Fri","Sa t")[color=blue][color=green]
          > > var montharray = new Array("Jan","Fe b","Mar","Apr", "May","Jun" ,
          > > "Jul","Aug","Se p","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[i][0] )
          > > var year = 1900 + date.getYear() % 1900 // < AD 3800
          > > if (today.getTime( ) <= date.getTime()) {
          > > document.write( '<tr><td valign=top>' + dayarray[date.getDay()][/color][/color]
          +[color=blue][color=green]
          > > "," + montharray[date.getMonth()] + " " + date.getDate() +
          > > ", " + year + " " + '&nbsp;&nbsp; </td><td> ' + ae[i][1] +
          > > '</td></tr>');
          > > document.write( '<tr><td colspan="2">&nb sp;</td></tr>');
          > > }
          > > }
          > >
          > > document.write( '</table>');[/color]
          >
          > The algorithm, as implemented is not at fault, but your test data[/color]
          is. The[color=blue]
          > 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[/color]
          test[color=blue]
          > data only goes to 8. As soon as the browser tries to resolve element[/color]
          7, it[color=blue]
          > finds that there's no data and gives you the error. If you've only[/color]
          got two[color=blue]
          > pieces of test data, you can't create an array with ten elements.[/color]

          by changing the array so the last number is a 0 now did the trick.
          Thanks everyone
          [color=blue]
          >
          > Some suggestions:
          >
          > - Make sure you add all the necessary semi-colons (;). There are a[/color]
          few[color=blue]
          > statements above that don't have them, and while technically legal[/color]
          (in[color=blue]
          > certain circumstances), it is still good style to include them.[/color]

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

          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?
          [color=blue]
          > - Some of the forward-slashes that compose the closing tags you will
          > insert have been escaped, which is good. However, the majority[/color]
          haven't.[color=blue]
          > Make sure you change all instances of '</' to '<\/'.[/color]

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




          Comment

          • Douglas Crockford

            #6
            Re: Error in Array in IE

            > > - Your 'ae' array isn't much of an array. However, it would make a[color=blue][color=green]
            > > good object. Consider revising it.[/color][/color]
            [color=blue]
            > 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?[/color]

            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:


            The object is one of the central ideas in JavaScript. Programming without is
            like driving a car without knowing what the pedals do.
            [color=blue][color=green]
            > > - 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 '<\/'.[/color][/color]
            [color=blue]
            > I asked this group a while ago the difference between </ and <\/ and
            > the impression I got is that the <\/ is not used.[/color]

            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

            Comment

            • Michael Winter

              #7
              Re: Error in Array in IE

              On Thu, 22 Jan 2004 19:49:26 GMT, Treetop <treetop@netfro nt.net> wrote:
              [color=blue]
              > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
              > news:opr16p8qq8 5vklcq@news-text.blueyonder .co.uk...
              >[color=green]
              >> - 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.[/color]
              >
              > The day and month arrays have semi-colons on them now. Are there any
              > other statements that I have missed?[/color]

              The two statement that begin "var date=" and "var year =" at the start of
              the for-loop.
              [color=blue][color=green]
              >> - Your 'ae' array isn't much of an array. However, it would make a
              >> good object. Consider revising it.[/color]
              >
              > 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?[/color]

              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.
              [color=blue][color=green]
              >> - 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 '<\/'.[/color]
              >
              > I asked this group a while ago the difference between </ and <\/ and
              > the impression I got is that the <\/ is not used.[/color]

              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.

              --
              Michael Winter
              M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

              Comment

              Working...