-3

I am making project on online examination in java. I am facing a problem.

On starting base I have 15 questions in my database and I am fetching those sequentially. The problem is that if I attempt all the answers I get the results otherwise I get error 500 and NullPointerException. The questions are multiple choice. Every question has four options. If I don't attempt all the questions then I get the above error.

<%@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");
%>
pap
  • 27,064
  • 6
  • 41
  • 46
Harsh
  • 63
  • 1
  • 4

3 Answers3

0

I would say that your answers are simply not submitted back with your request. If the radio group for a question does not have a selected value, that radio group is not going to be part of the POST request that you are processing after the form submission.

That is why you get the Null pointer exception in the first place. I cannot runt your example above, but i assume that the error is happening in the comparison line at the end of your example:

if (st[i].equals(ans[i])) {

Update
For a quick fix, just switch the evaluated values: st[i].equals(ans[i]) to ans[i].equals(st[i]). That way you can always do a equals against null and get the correct count.

ljubomir
  • 1,495
  • 1
  • 21
  • 40
  • Never write logic that assumes certain parameter will or will not be included from the client. Handle the Null scenario on the server instead. – pap Aug 22 '12 at 07:55
  • @pap you are right about that. i just wanted to throw a quick fix to the problem, which is already a mess, and that doesn't help much. – ljubomir Aug 22 '12 at 08:05
0

At the start of your code you're initializing your st[] with request.getParameter("radio" + j); This might come as null. As per the javadoc for getParameter():

Returns the value of a request parameter as a String, or null if the parameter does not exist.

So when you try to execute this following piece of code:

for (int i = 0; i < 15; i++) {
    if (st[i].equals(ans[i])) {
        count++;
    }
}

There is a chance that st[i] is in fact null. This might possibly be a reason for the NullPointerException in your code

Sujay
  • 6,753
  • 2
  • 30
  • 49
0

You should be enclosing your logic in try catch and in finally handle the SQL exception for connection, rs (in separate try catch blocks) Also, try lloking at the stack trace, as to which line gives the null pointer exception

Seeder
  • 113
  • 7