Why am I receiving 'undefined' error when setting a variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • damicomj
    New Member
    • Sep 2010
    • 34

    Why am I receiving 'undefined' error when setting a variable?

    I am trying to compare a variable to what the variable previously was in the for loop. I run into a problem when I try to reset 'PAsubtotal'

    The problem arises on the line:

    PAsubtotal=0;

    I get the error: "'undefined ' is null or not an object"


    Code:
    var details = new Array();
    	var cntr=0;
    	var PAtemp = new String();
    	var PAsubtotal;
    	
    	for( var current in me.items ){  
        	var item = me.items[current];     			
    		/*This is where I set matrix values*/		
    PAsubtotal = PAsubtotal + item.price*item.quantity
    		
    		if (cntr > 0){
    			if (!(item.size == PAtemp)){
    				cntr++;
    				details[cntr][5]= "<TD><b>" + PAsubtotal + "</b></TD></TR>";
    				PAsubtotal=0;
    			}
    		}
    		
    		cntr++;
    		PAtemp = item.size;
    		}
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    this error usually appears when an object method is called.

    in your case it is probably on line 15. if details[cntr] is not defined (e.g. if cntr is the same as the array length) details[cntr][5] will throw this error.

    Comment

    • damicomj
      New Member
      • Sep 2010
      • 34

      #3
      Sorry, I left some code out. Up above I have, /*This is where I set matrix values*/

      This is the code I have in there:

      Code:
      		details[cntr]=new Array(5);
      		details[cntr][0]= "<TR><TD>" + item.name + "</TD>";
      		details[cntr][1]= "<TD>" + item.size + "</TD>";
      		details[cntr][2]= "<TD>" + item.color + "</TD>";
      		details[cntr][3]= "<TD>" + item.price + "</TD>";
      		details[cntr][4]= "<TD>" + item.quantity + "</TD>";
      		details[cntr][5]= "<TD>" + item.price*item.quantity + "</TD></TR>";
      		PAsubtotal = PAsubtotal + item.price*item.quantity
      		
      		alert(details[cntr]);

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        the problem is still the same, if cntr reaches details.length, you try to access a not existing array object.

        Comment

        • damicomj
          New Member
          • Sep 2010
          • 34

          #5
          I am a bit confused. I add one to cntr in the if statement, which should bring the matrix to the next line. I just this to fill in the other columns, but it didn't help. Can you provide a suggestion for me?

          Code:
          				details[cntr][0]= "<TR><TD> </TD>";
          				details[cntr][1]= "<TD> </TD>";
          				details[cntr][2]= "<TD> </TD>";
          				details[cntr][3]= "<TD> </TD>";
          				details[cntr][4]= "<TD> </TD>";
          				details[cntr][5]= "<TD><b>" + PAsubtotal + "</b></TD></TR>";
          				PAsubtotal=0;

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            no. you have a two-dimensional matrix, but your counter variable only increments the first level array.

            Comment

            • damicomj
              New Member
              • Sep 2010
              • 34

              #7
              Okay, as an example, let's say cntr = 1.

              The code in the If statement will get executed because cntr > 0. cntr++ will increment itself to 2.
              Code:
              details[2][5]= "<TD><b>" + PAsubtotal + "</b></TD></TR>";
              is giving me the error. I don't see what is wrong with this line because I was using the same code outside of the If statement. Sorry if this is annoying, I am trying to talk myself through it.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                what does details.length give you?

                Comment

                • damicomj
                  New Member
                  • Sep 2010
                  • 34

                  #9
                  Okay, I defined the array inside of the If statement like you said, and I think I am all set now.

                  details[cntr]=new Array(5);

                  thank you

                  Comment

                  Working...