This is my piece of code where I am trying to read from a file. My file currently has 4 lines but code is not coming out of the loop after reading those lines.
public class ViewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
System.out.println("Logging in..");
// retrieving values entered in form
String uname = request.getParameter("uname");
String pwd = (String) request.getParameter("pwd");
if (uname.equals("admin") && pwd.equals("admin")) {
try {
viewDetails();
System.out.println("Redirecting to view page..");
response.sendRedirect("view.jsp");
} catch (Exception e) {
}
}
}
public void viewDetails() throws Exception{
System.out.println("Reading Data..");
BufferedReader input = new BufferedReader(new FileReader("H:/Workspace/teamRecordsApp/WebContent/records.txt"));
String record;
String[] split =new String[5];
ArrayList cuid = new ArrayList();
ArrayList fname = new ArrayList();
ArrayList lname = new ArrayList();
ArrayList mobile = new ArrayList();
ArrayList desk = new ArrayList();
int count = 0;
while((record=input.readLine()) != null){
split = record.split(",");
System.out.println("Record is : "+split[0]+","+split[1]+","+split[2]+","+split[3]+","+split[4]);
cuid.add(split[0]);
fname.add(split[1]);
lname.add(split[2]);
mobile.add(split[3]);
desk.add(split[4]);
count = count+1;
}
input.close();
System.out.println("Reading Done...");
Utils utils = new Utils();
utils.setCuid(cuid);
utils.setFname(fname);
utils.setLname(lname);
utils.setMobile(mobile);
utils.setDesk(desk);
utils.setCount(count);
System.out.println("Total records : "+count);
}
}
*added whole servlet class now.
Console shows:
Logging in..
Reading Data..
Record is : 12,e,e,1234567890,1234
Record is : 12,e,e,1234567890,1234
Record is : 12,e,e,1234567890,1234
Record is : 12,e,e,1234567890,123
After adding printStackTrace() in file,the exception I was getting is attached below. As said by @Pshemo, the last line which was empty was giving this exception. I was not able to figure this out by seeing the file and adding printStackTrace did the trick. Thanks a lot @Pshemo and others for helping!
java.lang.ArrayIndexOutOfBoundsException: 1
at ViewServlet.viewDetails(ViewServlet.java:54)
at ViewServlet.doPost(ViewServlet.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)