aspose file tools*
The moose likes Servlets and the fly likes Y my pgm is giving Array index out of bounds exception in Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Y my pgm is giving Array index out of bounds exception in Vector" Watch "Y my pgm is giving Array index out of bounds exception in Vector" New topic
Author

Y my pgm is giving Array index out of bounds exception in Vector

sowmya vasisht
Ranch Hand

Joined: Aug 23, 2001
Posts: 35
i m calling the servlet from jsp for add, remove and notify images passing vectors to display after adding and removing the elements of a vector the addition is functioning properly but for removal of elements from vector the following excpetion is thrown
ArrayIndexOutOfBoundsException for removeElementAt(index)
plz help me out
here is the jsp
<HTML>
<SCRIPT LANGUAGE="JScript">
function selectAll(chk)
{
for (var i=0;i < document.forms[0].elements.length;i++)
{
var e = document.forms[0].elements[i];
if (e.type == "checkbox")
{
e.checked = chk.checked
}
}
}
function findCheck()
{
alert(document.forms[0].check.value)
}

</SCRIPT>
<body bgcolor="#FFFFFF" text="#000000">
<div align="center"><center>
<form name=Form1 id=Form1 action="/nextcard/svltRecentNewAssigning" method=POST>
<%String UserId=(String)session.getValue("UserId");
System.out.println("sorry u r not able to come 2 JSP"+UserId);
session.putValue("UserId",UserId); %>
<table width="89%" border="1" height="100">
<tr align="left" valign="top">
<td colspan="2" height="62" bgcolor="#f6fdfe" valign="baseline">
<img src="/nextcard/Images/Testlogowhite.gif" width="152" height="80">
<img src="/nextcard/Images/index_r03_c04slkwhite.gif" width="104" height="17">
<font face="Arial, Helvetica, sans-serif" color="#660099" size="+2">
Assign Online Test</font> 
<img src="/nextcard/Images/index_r03_c04slkwhite.gif" width="216" height="19">
<img src="/nextcard/Images/index_r01_c01slkwhite.gif" width="102" height="73">
</td>
</tr>
</table>

<table width="89%" border="1" height="192">
<tr bgcolor="#f6fdfe">
<td width="48%" height="63" align="left" valign="top" bgcolor="#f6fdfe"> <p>
     
<P><b>Welcome <%=UserId%></b></P>
<P><b>Employees:</b>
<select name="Employees" style="width:136;">
<%@ page import=" TMSUtilities.*"%>
<%@ page import="java.util.Vector"%>
<% Vector vect=(Vector)session.getAttribute("vectDetails");
System.out.println(vect.size()+"kkkkkkkkkkkk");
for(int j=0; j<vect.size();j++)
{
EmployeeDetails empDet=(EmployeeDetails)vect.elementAt(j);%>
<OPTION value=<%=empDet.sEmpNames%>><%=empDet.sEmpNames>
</OPTION>
<%System.out.println(empDet.sEmpNames+"kkkkkkkkkkkk"+empDet.sEmpEmail);}%>
</P>
</select>
<b>Tests Available:</b>
<select name="Tests" style="width:136;">
<%@ page import=" TMSUtilities.*"%>
<%@ page import="java.util.Vector"%>
<% Vector vectTest=(Vector)session.getAttribute("vectTestNames");
System.out.println(vectTest.size());
for(int j=0; j<vectTest.size();j++)
{
EmployeeDetails empDet=(EmployeeDetails)vectTest.elementAt(j);%>
<option value=<%=empDet.sTestNames%>><%=empDet.sTestNames>
</option>
<%System.out.println(empDet.sTestNames);
}%>
</select>
<STRONG>        
Time:
<input name="Time" size="10" >     
Date:</STRONG>
<input name="Date" size="15" >
</P></P>
<input type=IMAGE src="/nextcard/images/Notify.gif" width="150" height="20" name="OK" action="/nextcard/svltRecentNewAssigning" method=post>
              
              
              
              
    
<input type=IMAGE src="/nextcard/images/Add.gif" width="90" height="20" name="add" action="/nextcard/svltRecentNewAssigning" method=post>
              
          
           
       
<input type=IMAGE src="/nextcard/images/Delete.gif" width="90" height="20" name="remove" action="/nextcard/svltRecentNewAssigning" method=post>
<div id="MyLayer" style="width:70%; height:100; overflow:auto">
<table width="100%" border="0" cellspacing="1" cellpadding="1" height="28" align="center" bordercolor="#9999ff">
<tr bgcolor="#ccccff" align="middle" valign="center">
<td width="25%" height="2"><b> Check All</b>
<input type="checkbox" name="checkbox" value="checkbox" onclick=selectAll(this)>

</td>
<td width="25%" height="2"><b> Employee Names</b></td>
<td width="20%" height="2"><b>Online Tests</b></td>
<td width="25%" height="2"><b> Date</b></td>
<td width="20%" height="2"><b>Time</b></td>
</tr>
<%@ page import="java.util.*"%>
<%@ page import="TMSUtilities.*"%>
<%System.out.println("kkkkkkkkkkkk----------------------1");
Vector newVect1 = (Vector)session.getAttribute("vectEmpNm");
System.out.println("kkkkkkkkkkkk----------------------2");
Vector newVect2 = (Vector)session.getAttribute("vectTests");
Vector newVect3 = (Vector)session.getAttribute("vectTime");
System.out.println("kkkkkkkkkkkk----------------------3");
Vector newVect4= (Vector)session.getAttribute("vectDate");
System.out.println("kkkkkkkkkkkk---------------------4");
if(newVect1==null)
{
System.out.println("mnohting is therllllllllllllllllllllll");
}
else
{%>
<%for(int i=0; i<newVect1.size(); i++)
{
if(i%2==0)
{%>
<tr bgcolor="#deeefd" align="middle" valign="center">
<%}
else
{%>
<tr bgcolor="f6fdfe" align="middle" valign="center">
<%}%>
<td width="25%" height="2">
<%
System.out.println(i+" --->>>>>"+newVect1.size()); %>
<input type="checkbox" name="check" value=<%=i%> ></td></b>
<td width="25%" height="2"><b><%=newVect1.elementAt(i)%></b></td>
<td width="25%" height="2"><b><%=newVect2.elementAt(i)%></b></td>
<td width="25%" height="2"><b><%=newVect4.elementAt(i)%></b></td>
<td width="25%" height="2"><b><%=newVect3.elementAt(i)%></b></td>
</tr>
<%}%>
</div>
</table>
</form>
</div>
</BODY>
</HTML>
and here is the called servlet which adds remove and notifies thro email

import java.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import TMSUtilities.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class svltRecentNewAssigning extends HttpServlet
{

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
HttpSession session=request.getSession(true);
Vector vEmpNames = (Vector)session.getValue("vect1");
Vector vAssTests = (Vector)session.getValue("vect2");
Vector vTimes = (Vector)session.getValue("vect3");
Vector vDates = (Vector)session.getValue("vect4");
String sProjId = (String)session.getValue("ProjId");
int iPId = Integer.parseInt(sProjId);
String checkbox[] = request.getParameterValues("check");
System.out.println(iPId+"prntng the values oaf hceckboxexsssssssssssss0000 "+checkbox);
Vector vEmpEmails=new Vector();
Vector vEmpIds = new Vector();
Vector vTestIds = new Vector();
int iEmpId=0, iProjectId=0;
String sEmpNames="";
String sAssTests="";
String sTimes ="";
String sDates ="";
String sEmpEmail="";
try
{
OpenDatabase DB = new OpenDatabase("TMSDB");
DB.Open();
Connection con = DB.GetConnectingObject();
Statement stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
for(int k=0;k<vEmpNames.size();k++)
{
EmployeeDetails empDet = new EmployeeDetails();
String sQueEL = "SELECT * FROM Employees WHERE EmpNames='"+vEmpNames.elementAt(k)+"'";
System.out.println(sQueEL);
ResultSet rsltEL=stmt1.executeQuery(sQueEL);
while(rsltEL.next())
{
empDet.iEmpId = rsltEL.getInt("EmpId");
iProjectId = rsltEL.getInt("ProjectId");
sEmpEmail=rsltEL.getString("Txt_EmpEmail");
System.out.println("the email leader"+sEmpEmail);
vEmpEmails.addElement(sEmpEmail);
vEmpIds.addElement(empDet);
}
}
}
catch(SQLException sqle)
{
System.out.println(" SQLE exceptong while fndng out empl emals"+sqle.getMessage());
}
catch(Exception e)
{
System.out.println("exceptong while fndng out empl emals"+e.getMessage());
}

String button = request.getParameter("button");
if(null!=request.getParameter("add.x"))
{
sEmpNames = request.getParameter("Employees");
System.out.println(iProjectId+"printing sempnames"+sEmpNames);
sAssTests = request.getParameter("Tests");
sTimes = request.getParameter("Time");
sDates = request.getParameter("Date");
vEmpNames.addElement(sEmpNames);
vAssTests.addElement(sAssTests);
vTimes.addElement(sTimes);
vDates.addElement(sDates);
session.setAttribute("vectEmpNm", vEmpNames);
session.setAttribute("vectTests", vAssTests);
session.setAttribute("vectTime", vTimes);
session.setAttribute("vectDate", vDates);
System.out.println("sEmpNames"+sEmpNames+"AssignedTests"+sAssTests+"sTimes"+sTimes+"sDates"+sDates);
response.sendRedirect("/nextcard/RecentNewAssigning.jsp");
}
if(null!=request.getParameter("remove.x"))
{
try
{
if(checkbox==null)
{
response.sendRedirect("/nextcard/RecentNewAssigning.jsp");
}
}
catch(NullPointerException npe)
{
}
for(int i=0;i<checkbox.length;i++)
{
System.out.println(checkbox[i]+"checkbox[i]"+checkbox.length);
}
try
{
if(checkbox.length>0)
{
int iIndex=0;
for(int j=0;j<=checkbox.length;j++)
{
//iIndex = vEmpIds.indexOf(checkbox[j]);
iIndex = Integer.parseInt(checkbox[j]);
System.out.println("vEmpNames.elementAt
(iIndex)"+vEmpNames.elementAt(iIndex));
System.out.println( "vAssTests.elementAt(iIndex)"+vAssTests.elementAt(iIndex));
System.out.println("vTimes.elementAt(iIndex)"+vTimes.elementAt(iIndex));
System.out.println("vDates.elementAt(iIndex)"+vDates.elementAt(iIndex));
vEmpNames.removeElementAt(j);
vAssTests.removeElementAt(j);
vTimes.removeElementAt(j);
vDates.removeElementAt(j);
}
}
}
catch(ArrayIndexOutOfBoundsException aiobe)
{
System.out.println("index out of bounds excpetion "+aiobe.getMessage());
}
session.setAttribute("vectEmpNm", vEmpNames);
session.setAttribute("vectTests", vAssTests);
session.setAttribute("vectTime", vTimes);
session.setAttribute("vectDate", vDates);
for(int k=0; k<vEmpNames.size(); k++)
{
System.out.println(vEmpNames.size()+"printng after removing elementsat()"+vEmpNames.elementAt(k));
}
System.out.println("printng the element in the vector"+vEmpNames.size());
response.sendRedirect("/nextcard/RecentNewAssigning.jsp");
}
System.out.println("outside the OKkkkkkkkkkkkkkkk");
if(null!=request.getParameter("OK.x"))
{
String sLeadName=(String)session.getValue("UserId");
System.out.println("@@@@@@@@@@@@@@@@@@@@@@"+sLeadName);
String sQuery,sLeadEmail="";
CandidateNum canNum = new CandidateNum();
int iCanNum = canNum.getNextCandidateId();
String sURL="http://sharath:7001/TMSAddAccount.jsp";
String sTempEmp="";
int iLeadId=0, iIdQType=0;
try
{
OpenDatabase DB = new OpenDatabase("TMSDB");
DB.Open();
Connection con = DB.GetConnectingObject();
Statement stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sQueEL = "SELECT * FROM Leaders WHERE LeadNames='"+sLeadName+"'";
System.out.println(sQueEL);
ResultSet rsltEL=stmt1.executeQuery(sQueEL);
while(rsltEL.next())
{
iLeadId = rsltEL.getInt("LeadId");
sLeadEmail=rsltEL.getString("Txt_LeadEmail");
}
System.out.println("the email leader"+sLeadEmail);
Statement stmtId = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
for(int iId=0; iId<vAssTests.size(); iId++)
{
EmployeeDetails oIds = new EmployeeDetails();
String sQueryId3 = "SELECT Id_Type from QuestionTypes where Txt_Type='"+vAssTests.elementAt(iId)+"'";
System.out.println(sQueryId3);
ResultSet rsltId3 = stmtId.executeQuery(sQueryId3);
while(rsltId3.next())
{
oIds.iTestId = rsltId3.getInt("Id_Type");
vTestIds.addElement(oIds);
}
}
int iTempEmpId=0, iIdTest=0;
float fMarks = 0;
String sDt="";
String sTm ="";
try
{
for(int iEmp=0;iEmp<=vEmpEmails.size();iEmp++)
{
EmployeeDetails empids = (EmployeeDetails)vEmpIds.elementAt(iEmp);
EmployeeDetails oTestIds = (EmployeeDetails)vTestIds.elementAt(iEmp);
System.out.println(sURL+" prntng for cautonVVVVVVVVVVVVVVVVVVVVVVVVVVVVVv");
PreparedStatement stmtInsertDB = con.prepareStatement( "INSERT INTO Test_Candidates VALUES (?,?,?,?,?,?,?,?,?,?,?,?)",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
sTempEmp=(String)vEmpNames.elementAt(iEmp);
String sTopic = (String)vAssTests.elementAt(iEmp);
sDt =(String)vDates.elementAt(iEmp);
sTm = (String)vTimes.elementAt(iEmp);
iIdTest = oTestIds.iTestId;
iTempEmpId = empids.iEmpId;
stmtInsertDB.setInt(1,iCanNum);
stmtInsertDB.setInt(2,5);
stmtInsertDB.setInt(3,iIdTest);
stmtInsertDB.setInt(4,iLeadId);
stmtInsertDB.setInt(5,iProjectId);
stmtInsertDB.setInt(6,iTempEmpId);
stmtInsertDB.setString(7,sTempEmp);
stmtInsertDB.setString(8,sDt);
stmtInsertDB.setString(9,sTm);
stmtInsertDB.setFloat(10,fMarks);
stmtInsertDB.setString(11,sURL);
stmtInsertDB.setString(12,sTopic);
System.out.println(iCanNum+"print the vecotr"+vEmpNames.elementAt(iEmp)+"printng sze"+vEmpNames.size());
stmtInsertDB.executeUpdate();
canNum.getNextCandidateId();
iCanNum++;
System.out.println("printng fnaly the icanNum"+iCanNum);
}
}
catch(SQLException sqle)
{
System.out.println("EXCEPTIONwthin the ssssssssss IN sql"+ sqle.getMessage());
}

catch(Exception ex)
{
System.out.println("EXCEPTIONwithhn the ssssssss "+ ex.getMessage());
}
}
catch(SQLException sqle)
{
System.out.println("EXCEPTION IN sql"+ sqle.getMessage());
}

catch(Exception ex)
{
System.out.println("EXCEPTION "+ ex.getMessage());
}

String msgText1 = "";
String msgText2 = "",sSubject="";
String sFrom="";
String to;
String sDate = request.getParameter("Dt_Schedule");
String sIdStatus;
try
{
//Reading the text Message Part from the "Message.txt" file
FileInputStream fstream = new FileInputStream("D:/weblogic/wlserver6.0/config/mydomain/applications/nextcard/WEB-INF/classes/MailMsg.txt");
DataInputStream in = new DataInputStream(fstream);
String sTemp;
// if(in.available() == 0)
sSubject = in.readLine(); //Getting the E-Mail Subject
System.out.println("Subject:"+ sSubject);
//sFrom = in.readLine(); //Getting the From E-mail Id System.out.println("From Id:"+ sSubject);
sFrom=sLeadEmail;
System.out.println ("prntn the ffffffffffffffffffffffffffffff"+sFrom);
while (in.available() != 0)
{
// Print file line to screen
sTemp = in.readLine();
msgText1 += sTemp;
msgText1 += "\n";
System.out.println ("prntn the meeeeeeeeeeeeeeeeeeeeeeeeeeeees"+sTemp);
}
in.close();
}
catch (Exception e)
{
System.err.println("File input error");
}

msgText1 += "\n";
msgText1 += "this mail s sent By: "+sLeadName;
String host = "192.168.10.2";
boolean debug = true;
// create some properties and get the default rSession
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session session1 = Session.getDefaultInstance(props, null);
session1.setDebug(debug);
try
{
// create a message
MimeMessage msg = new MimeMessage(session1);
msg.setFrom(new InternetAddress(sFrom));
//Sending mails to all the Candidates
for(int iEmails=0; iEmails<vEmpEmails.size(); iEmails++)
{
System.out.println("prntng the selecteed empl emals"+vEmpEmails.elementAt(iEmails));
msgText2 += "Date:" +vDates.elementAt(iEmails);
msgText2 += "Time:" +vTimes.elementAt(iEmails);
msgText2 += "\n";
msgText2 += sURL;
to ="sowmyah@slk-soft.com";//(String)vEmpEmails.elementAt(iEmails);
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(sSubject);
msg.setSentDate(new java.util.Date());
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1+msgText2);
// create and fill the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// Use setText(text, charset), to show it off !
mbp2.setText("Route.gif", "us-ascii");
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// send the message
msgText2="";
Transport.send(msg);
}
} //TRY End
catch (MessagingException mex)
{
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null)
{
ex.printStackTrace();
}
}
session.putValue("vectEmpNames",vEmpNames);
session.putValue("vectAssTests",vAssTests);
session.putValue("vectDates",vDates);
session.putValue("vectTime",vTimes);
response.sendRedirect("/nextcard/confirmsharath.jsp");
}

}
}

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
No need to post all that code, it really is not helpful. Just look at the Vector javadocs - clearly states that you can get an ArrayIndexOutOfBoundsException from removeElementAt if the index is out of range of the current vector contents.
Therefore, somewhere in the code you are trying to use a bad index. Be aware that when you remove an element, all the higher elements move down one, so your for loop generates the wrong index. You could go through the Vector from high to lo.
Bill
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

I tried to follow your code, but its way too much...
Could you edit your post so that you have ONLY
the remove portion of the code above.
First I would see if you are removing objects from the
Vector in a for loop....$0.02.
This is one reason I can think of where you can get the
subject Exception.
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Hi,
hey remember that when u do
vector.removeElementAt(j);
the element will be removed and the element below j will now be the element at index j.
So in ur case what u should do is, after vector.removeElementAt(j); u should do j--;
Also i think for(int j=0;j<=checkbox.length;j++) should be replaced by for(int j=0;j<checkbox.length;j++)
Let me know if this works
Regards
Saj

sowmya vasisht
Ranch Hand

Joined: Aug 23, 2001
Posts: 35
hi sajee
thanx for your cooperation but the solution what u gave didnt work out it is removing all the last elements which i check. plz if you have any other suggestions forward it. it will be of a great help for me.
thanx
sowmya vasisht
Ranch Hand

Joined: Aug 23, 2001
Posts: 35
hello
thanx a lot for your suggestion and it worked really fine
regards
sowmya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Y my pgm is giving Array index out of bounds exception in Vector