SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    App crashing when I try to instantiate an array

    I have some java script in my vb.net application and I've been trying to figure out why my web app keeps crashing when i try to instantiate an array (i placed a comment where the app is crashing in the code below). Here's my code...

    Code:
    function CopyItem(plyrDuplicate,copyPlyrPostback) 
    {
        var plyrDuplicate = (typeof plyrDuplicate == "undefined")?'defaultValue':plyrDuplicate;
        var firstListBox = document.getElementById('<%= lstPlayerSelect.ClientID %>');
        var secondListBox = document.getElementById('<%= lstPlayerQueue.ClientID %>');
        var postBack = -1;
        if (copyPlyrPostback == 0){
        postBack = 0;
        }
        //alert("in the function");
        //alert(postBack);
        if (postBack == -1)
         {
            for (var i = 0; i < firstListBox.options.length; i++) 
            { 
                if (firstListBox.options[i].selected) 
                {
                    var newOption = document.createElement("option");
                    newOption.text = firstListBox.options[i].text;
                    newOption.value = firstListBox.options[i].value;
                    playerVal = newOption.value;
                    CheckDuplicate(playerVal);
                    var printRec =  CheckDuplicate(playerVal);
                    alert(playerVal);
                    alert(CheckDuplicate(playerVal));
                    if (printRec != true) 
                    {
                     //alert(secondListBox.options[secondListBox.options.length]);
                     //alert(secondListBox.options.length);
                     secondListBox.options[secondListBox.options.length] = newOption;
                    }
                }         
            }
            if (document.getElementById('ErrorMsg').style.display = 'inherit') 
            {
                document.getElementById('ErrorMsg').style.display = 'none';
            }
         }
        else
         {
            alert("inside other else block, hurray~!!!");
            hdnSessionVariable = '<%=Session("playerId")%>';
            hdnSessionVariable = hdnSessionVariable.split(",");
            for (var ii = 0; ii < hdnSessionVariable.length; ii++)
                {
                for (var i = 0; i < firstListBox.options.length; i++) 
                    { 
                      if (hdnSessionVariable[ii] ==  firstListBox.options[i].value) 
                        {
                         //alert("we have a match!!!");
                            var newOption = document.createElement("option");
                            newOption.text = firstListBox.options[i].text;
                            newOption.value = firstListBox.options[i].value;
                            playerVal = newOption.value;
                            CheckDuplicate(playerVal);
                            var printRec =  CheckDuplicate(playerVal);
                            alert(playerVal);
                            alert(CheckDuplicate(playerVal));
                            alert(newOption.value);
                            if (printRec != true) {
                            secondListBox.options[secondListBox.options.length] = newOption; //app crashes on this line
                        }
                    } 
                 }       
         }
        Updatelist();
        return false;
    }
    Hopefully the code above is enough for to figure out the issue. However, here's a mini recap of what's going on. A user selected a value from firstListBox and the function above copies it over to secondListBox. The code in the first condition works fine, but the code in the else block doesn't. Now, here's the catch/difference between the if and else statement...the Else block is basically doing the same thing as the if statement, except the values that needs to be copied are coming from a session variable (hdnSessionVariable = '<%=Session("playerId")%>'. I've commented out every line in the else statement and the app works until it hits the secondListBox.options[secondListBox.options.length] = newOption line. Anybody have any idea what's wrong here?

  2. #2
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    edit: doh, its a dom object, not a usual array. try this:

    secondListBox.appendChild(newOption);

  3. #3
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your post made me recheck my code, and sure enough, I found the issue...I was missing a freaking closing curly bracket!!! Wow, i feel like a jackass.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by vb89 View Post
    Your post made me recheck my code, and sure enough, I found the issue...I was missing a freaking closing curly bracket!!! Wow, i feel like a jackass.
    Just wait until you want to improve your code and run it through jslint.com - with "The Good Parts" enabled. It's known to make grown men cry
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •