'length' is null or not an object? IE 8

19,840

Solution 1

Just check for the object being null or empty:

if (json && json.length) {
  // ...
}

C'mon gang this was glaringly obvious :-)

Solution 2

pop the JSON in a span then clip it and paste it here so we can see it:

<span id="JSObject2">empty</span>

with the json2.js from here: (link for it at bottom of the page) http://www.json.org/js.html

myJSON = JSON.stringify(json);
$('#JSObject2').text(myJSON);

Using that, we can help you better, and you can see what you have!

Solution 3

JSON objects (returned by jQuery or otherwise) do not have a length property. You'll need to iterate over the properties, most likely, or know the structure and simply pull out what you want:

$.getJSON( ..., ..., function( json ) {
  for( var prop in json ) {
    if( !json.hasOwnProperty( prop ) ) { return; }
    alert( prop + " : " + json[prop] );
  }
} );

Alternatively, grab a library like json2 and you'll be able to stringify the object for output/debugging.

Share:
19,840

Related videos on Youtube

Scarface
Author by

Scarface

Updated on April 28, 2022

Comments

  • Scarface
    Scarface about 2 years

    I get the following error in IE8:

    length is null or not an object

    Anyone have any ideas? Feedback greatly appreciated.

    function refresh() {
      $.getJSON(files+"handler.php?action=view&load=update&time="+lastTimeInterval+"&username="+username+"&topic_id="+topic_id+"&t=" + (new Date()), function(json) {
        if(json.length) {
          for(i=0; i < json.length; i++) {
            $('#list').prepend(prepare(json[i]));
            $('#list-' + count).fadeIn(1500);
          }
          var j = i-1;
          lastTimeInterval = json[j].timestamp;
        }
      });
    }
    
  • Scarface
    Scarface almost 14 years
    it just says [object Object],[object Object],[object Object],
  • Scarface
    Scarface almost 14 years
    when I use alert(json); after if(json.length) {
  • Scarface
    Scarface almost 14 years
    thanks for that mark, at the start, there is nothing because the function is designed to only return new information on a settimeout period. When information is sent, it looks like this for example [{"timestamp":"1279573958","user":"dfgdfgd","message":"t","c‌​reator":""}]
  • Scarface
    Scarface almost 14 years
    I am not exactly sure what you mean from making the json from the backend structure. I am gathering the information from a php document which gets the information from the database and puts it into a json array like so $data[] = $row; $out = json_encode($data); print $out;
  • meder omuraliev
    meder omuraliev almost 14 years
    Why can't you just show us that real JSON? Or mimic it exactly.
  • Mark Schultheiss
    Mark Schultheiss almost 14 years
    Thus, there is no length. But, if you do alert(json[0].timestamp); you should see 1279573958 in the alert box.
  • Scarface
    Scarface almost 14 years
    lol I think that may have fixed it, I will check back in a little bit after some testing
  • Scarface
    Scarface almost 14 years
    ps I like your picture, classic painting
  • Scarface
    Scarface almost 14 years
    I did lol see the comment I made to Mark Schultheiss, after he showed me how to actually show the raw json, since it was getjson function, I did not know how to show the json but he showed me
  • meder omuraliev
    meder omuraliev almost 14 years
    Uh. Just go to your JSON page directly...And download it or whatever. You don't need to do it in JS.
  • Pointy
    Pointy almost 14 years
    @OK great! Yes it's an amazing painting, though I feel a little sorry for the poor girl. I always imagine her weeping while her slob of a dad yells,"You're marrying Mr Arnolfini, and that is that!"
  • Scarface
    Scarface almost 14 years
    lol, I suppose that was a reality in those times. I am pretty sure the error is gone lol. Thanks again for your help.
  • Scarface
    Scarface almost 14 years
    lol there were too many undefined variables and complications, since I was retrieving information not posting it and retrieving, (it was not a form posting), which yes, I do admit that would have been easy. Mark had the right idea.
  • Scarface
    Scarface almost 14 years
    yeah I knew there was no length, but it was somehow defined everytime except ofcourse on load when it would not be so I figured it was a javascript standard variable or something lol