Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Unhappy ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    Hi all,

    I am trying to get the data from a html table and insert it into the database. My html table adds rows dynamically through javascript..My html code is as follows:

    XML Code:
    <tbody id="tbodyId">
    <tr>
              <td><input name="active" id="active" type="checkbox"  onclick="rowindex(this);"></td>
              <td><input name="resourceName" id="resourceName" type="text" ></td>
              <td><input name="expYears" id="expYears" type="text" ></td>
              <td><input name="primaryskill" id="primaryskill" type="text" ></td>
              <td><input name="available" id="available" type="text" ></td>
              <td><input name="expires" id="expires" type="text" ></td>
              <td><input name="companyName" id="companyName" type="text" ></td>
    </tr>
    </tbody>
    <tbody>
    <tr>
    		<td colspan="7">
       		<center><input value="Add" onclick="addRow();" type="button" class="button"></center>
                    </td>
    </tr>
    </tbody>
    </table>
    my java script is as follows:

    Java Code:
    function rowindex(obj){
    	
    	alert(obj.parentNode.rowIndex);
    }
    
    var rownumber=0;
    
    function addRow() {
    	
    	rownumber++;
        var table = document.getElementById('tbodyId');
    
        //var rowCount = table.rows.length;
        
        var row = table.insertRow(rownumber);
    
        
        var cell1 = row.insertCell(0);
        var element1 = document.createElement("input");
        element1.type = "checkbox";
        cell1.appendChild(element1);
    
        var cell2 = row.insertCell(1);
        var element2 = document.createElement("input");
        element2.type = "text";
        cell2.appendChild(element2);
        
    
        var cell3 = row.insertCell(2);
        var element3 = document.createElement("input");
        element3.type = "text";
        cell3.appendChild(element3);
        
        var cell4 = row.insertCell(3);
        var element4 = document.createElement("input");
        element4.type = "text";
        cell4.appendChild(element4);
        
        var cell5 = row.insertCell(4);
        var element5 = document.createElement("input");
        element5.type = "text";
        cell5.appendChild(element5);
        
        var cell6 = row.insertCell(5);
        var element6 = document.createElement("input");
        element6.type = "text";
        cell6.appendChild(element6);
        
        var cell7 = row.insertCell(6);
        var element7 = document.createElement("input");
        element7.type = "text";
        cell7.appendChild(element7);
      
        alert(row.rowIndex);
        
       // noofrows();  
    }
    my servlet code is :

    Java Code:
     String[] activeValues = request.getParameterValues("active");
    					String[] resourceValues = request.getParameterValues("resourceName");
    					String[] experienceValues = request.getParameterValues("expYears");
    					String[] SkillsValues = request.getParameterValues("primaryskill");
    					String[] availableValues = request.getParameterValues("available");
    					String[] expireValues = request.getParameterValues("expires");
    					String[] companyValues = request.getParameterValues("companyName");
    					
    			   for(int i=0;i<activeValues.length;i++){
    				   
    				   String active = activeValues[i];
    			           String resource = resourceValues[i];
    				   String experienceofresource =experienceValues[i];
    				   String Skills = SkillsValues[i];
    				   String availableon = availableValues[i];
    				   String expireson = expireValues[i];
    				   String companyofresource = companyValues[i];
    
    
    				/* from here my values are redirected to the DAO where i am inserting these values*/
    				   }
    I have two problems here:

    1.when I am entering data for two or more rows in my table , it inserts the first row into the database and then it gives an arrayoutofboundsexception: 1 . I tried finding out the problem and I found the second issue which is below.

    2. rowindex(this) returns "undefined".(I am assuming this could be the problem for getting the arrayindexoutofboundsexception)

    Could anyone please let me know how to rectify this problem..???

  2. #2
    Ronin is online now Senior Member
    Join Date
    Oct 2010
    Posts
    348
    Rep Power
    4

    Default Re: ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    Hi,

    The error is caused by your incremention of 'rownumber' before its use. Although you want to insert a row into the first row of the table this is in fact at index '0' and not '1' you are currently trying to access.

    To correct this either increment 'rownumber' after its use (The end of the method is the best place) or change the inital declared value from '0' to '-1'. Generally I would favour the first.

    Regards.

  3. #3
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Re: ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    Hi Ronin,

    Thanks a lot for the reply.. The arrayindexoutofboundsexception is gone.. But still only the data from the first row is getting added into the database.. the other rows are not getting added ..I found that my for loop is iterating only once..could there be any possible solution for that?? Please help meeee...
    Last edited by javabeginner29; 06-26-2012 at 01:54 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    my for loop is iterating only once
    What controls the number of iterations? The size of the array?
    How many elements are in the array? Do you need to make a bigger array with more elements?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,039
    Rep Power
    20

    Default Re: ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    Have a look at the parameters sent to the server, in the URL (or as POST data).
    I think that might answer your question.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Re: ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    hi norm,

    whenever we add a new row dynamically the size of the array should increase..which I think it is not..the number of iterations should be controlled by the number of rows present in the table. I am not really getting it .. why is the array size not incrementing??

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,039
    Rep Power
    20

    Default Re: ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    Just look at the parameters sent to the server.
    Indeed, you could just look at the table after adding a row and see what the new row looks like.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Re: ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    Not really able to identify what you are pointing to tolls..

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,039
    Rep Power
    20

    Default Re: ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    What does the full URL (including parameters) sent to the server look like?
    <your servlet>?<same parameter>=x&<some other parameter>=y
    That if it's a GET, or just the list of data if it's a POST.

    Or, as I say, look at the HTML after you've added a row (which you can do in several browsers these days using their debugging facilities, not sure about IE though).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Re: ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    Thank You all! I realized that the problem is I am not creating an array of the same elements.. I need to give the element.Id and element.name in the js to create an array of rows to detect the elements that are passed..

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,039
    Rep Power
    20

    Default Re: ArrayIndexOutofBoundsException: 1! rowIndex undefined!

    Exactly.
    They had no name (it's the name rather than the id, id should be unique as per html specs).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Why is my method undefined?
    By EscSequenceAlpha in forum New To Java
    Replies: 5
    Last Post: 05-06-2012, 01:42 AM
  2. operator undefined for argument type int, String
    By aconti in forum New To Java
    Replies: 8
    Last Post: 05-30-2011, 07:32 AM
  3. The constructor Bucket(int) is undefined
    By yairku in forum New To Java
    Replies: 7
    Last Post: 05-09-2011, 07:19 PM
  4. Form Parameter Undefined
    By macs_80 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 06-29-2010, 02:26 PM
  5. Undefined attribute name
    By Dieter in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-03-2010, 10:44 AM

Posting Permissions

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