我正在用java制作在线考试项目。我面临一个问题。
在开始时,我的数据库中有 15 个问题,我正在按顺序获取这些问题。问题是,如果我尝试所有答案,我会得到结果,否则我会得到error 500
and NullPointerException
。题目为多项选择题。每个问题都有四个选项。如果我不尝试所有问题,那么我会收到上述错误。
<%@page import="java.sql.*"%>
<%
String st[] = new String[20];
String ans[] = new String[20];
int k=0;
//int length = Integer.parseInt(request.getAttribute("length").toString());
for (int i = 0; i < 15; i++)
{
int j = i + 1;
st[i] = request.getParameter("radio" + j);
System.out.println(st[i]);
}
Class.forName("oracle.jdbc.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "root", "root");
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("Select ANS from ANSWERS order by ID");
//String ans = "";
int t;
while (rs.next()) {
ans[k] = rs.getString("ans");
k++;
System.out.println(ans[k]);
}
int count = 0;
//String answers[] = ans.split(" ");
for (int i = 0; i < 15; i++) {
if (st[i].equals(ans[i])) {
count++;
}
}
out.println("Your " + count + " answers are correct");
%>