1

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)
4
  • 3
    Where is input defined and initialized? Oct 27, 2013 at 6:56
  • 1
    What is it reading if it is not existing the loop? What do you see in the output? Are you sure it is not reading another file? Oct 27, 2013 at 6:58
  • I do not see anything in the console after I print the 4 records in the file. As after input.close() I have added a print command "Reading Done" to check if it coming out or not, which it is not printing. Oct 27, 2013 at 7:03
  • Instead of adding more and more comments edit your question to reflect what you mean.
    – Prateek
    Oct 27, 2013 at 7:14

1 Answer 1

2

Since you invoke viewDetails(); in try-catch block, but you don't handle exception (you don't even print its stack trace)

} catch (Exception e) {
    //nothing here... :/
}

I assume that exception was thrown inside your loop before "Reading Done..." massage. Exception could be caused by many things like if your file would have some empty line your loop you would read it "" and to split it on , which would create one element array {""} and then by using split[1] you could get IndexOutOfBoundException. But that is just one of possibilities. To make sure what is wrong with your code add

e.printStackTrace(); 

to your catch (Exception e) block.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.