这是我试图从文件中读取的代码片段。我的文件目前有4行,但读取这些行后代码不会出现在循环中。
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);
}
}
*现在添加了整个servlet类。
控制台显示:
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
在文件中添加printStackTrace()后,我收到的异常将在下面附上。正如@Pshemo所说,最后一行是空的是给出了这个例外。我无法通过查看文件并添加printStackTrace来解决这个问题。非常感谢@Pshemo和其他人的帮助!
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)
答案 0 :(得分:2)
因为你在try-catch块中调用viewDetails();
,但你没有处理异常(你甚至不打印它的堆栈跟踪)
} catch (Exception e) {
//nothing here... :/
}
我认为在"Reading Done..."
按摩之前,你的循环中会抛出异常。许多事情都可能导致异常,例如,如果你的文件在你的循环中有一些空行你会读它""
并将它拆分在,
上,这将创建一个元素数组{""}
然后使用split[1]
即可获得IndexOutOfBoundException
。但这只是其中一种可能性。要确保您的代码添加
e.printStackTrace();
到catch (Exception e)
区块。