jva jva - 6 months ago 8
Java Question

servlet :The server encountered an internal error that prevented it from fulfilling this request

I'v created registration page with jsp page and servlet , i m getting error as
description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
com.javatpont.RegisterUser.register(RegisterUser.java:13)
com.javatpont.CreateServlet.doGet(CreateServlet.java:33)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


this is --->Register.jsp page

<form name=F1 onSubmit="return dil(this)" action="CreateServlet">
<table height="350" cellspacing="5" cellpadding="3">
<tr>
<td>USER NAME:</td><td> <input type="text" name="username"/></td>
</tr>
<tr>
<td>PASSWORD:</td><td> <input type="password" name="password"/></td>
</tr>
<tr>
<td>RE-PASSWORD:</td><td> <input type="password" name="repassword"/></td>
</tr>
<tr>
<td>PHONE:</td><td> <input type="text" name="phone"/></td>
</tr>
<tr>
<td>ADDRESS:</td><td> <textarea rows=4 cols=40 wrap=virtual name="adderess"></textarea></td>
</tr>
<TR>
<TD> Select your designation:</TD>
<TD>
<SELECT NAME="syd">
<option>student
<option>professor
<option>Associate-Professor
<option>lecturer
<option>non-teaching staff
</SELECT>
</TD>
</TR>
<tr><td></td><td><input type="submit" value="Submit"/>
<INPUT TYPE=RESET VALUE="CLEAR"></td></tr>
</table>
</form>


and this is CreateServlet.java

package com.javatpont;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.Naming;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class CreateServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out=response.getWriter();

String username=request.getParameter("username");
System.out.println("-----------------------"+username);
String password=request.getParameter("password");
String repassword=request.getParameter("repassword");
String ph=request.getParameter("phone");
double phone=Double.parseDouble(ph);

String adderess=request.getParameter("adderess");

String syd=request.getParameter("syd");

// line number 33 int status=RegisterUser.register(username, password, repassword,phone, adderess,syd);
System.out.println("***********************"+username);

if(status>0){
System.out.println("==========================="+username);
request.setAttribute("welcome","WELCOME! YOU HAVE BEEN REGISTERD");
RequestDispatcher rd=request.getRequestDispatcher("member.jsp");
rd.include(request, response);
}
else{
out.print("Sorry,Registration failed. please try later");
RequestDispatcher rd=request.getRequestDispatcher("member.jsp");
rd.include(request, response);
}

out.close();
}

}


and this is RegisterUser.java

package com.javatpont;
import java.sql.*;
public class RegisterUser {
static int status=0;
//int accountno=1;
public static int register(String username,String password,String repassword,double phone,String adderess,String syd){
//public static int register(String email,String password,String gender,String country,String name){

Connection con=GetCon.getCon();
PreparedStatement ps;

try {
System.out.println("here is the problem line number 13");
//line number13-> ps = con.prepareStatement("Insert into NEWMEMBER values(?,?,?,?,?,?,?)");
int nextvalue1=GetCon.getPrimaryKey();
ps.setInt(1,nextvalue1);
ps.setString(2,username);
ps.setString(3,password);
ps.setString(4,repassword);
ps.setDouble(5,phone);
ps.setString(6,adderess);
ps.setString(7,syd);

status=ps.executeUpdate();
System.out.println(status);
} catch (SQLException e) {

e.printStackTrace();
}
return status;

}
}


this is--> GetCon.java

public class GetCon {
private GetCon(){}

public static Connection con;
static{
try {
Class.forName(DBIntializer.DRIVER);
con=DriverManager.getConnection(DBIntializer.CON_STRING,DBIntializer.USERNAME,DBIntializer.PASSWORD);
} catch (ClassNotFoundException e) {

e.printStackTrace();
} catch (SQLException e) {

System.out.println("Exception in GetCon");
}

}
public static Connection getCon(){
return con;
}



public static int getPrimaryKey(){
int nextvalue=0;
Connection con=GetCon.getCon();
PreparedStatement ps2;
try {

ps2=con.prepareStatement("select ff from dual");

ResultSet rs=ps2.executeQuery();
rs.next();
nextvalue=rs.getInt(1);



} catch (SQLException e) {

e.printStackTrace();
}
return nextvalue;

}
}

Answer

Hi I think your actual problem is here

Connection con=GetCon.getCon();

You are able to get the connection and still try and do this..

ps = con.prepareStatement(...)

We need to first find out why the connection is failing .. check the URL you give there is it like this "jdbc:mysql://localhost/DATABASE_NAME" .. localhost if the db is on your machine itself or else that ip.. Also make sure you have the required jdbc driver in classpath..

EDIT:

Seeing the latest comments and code it looks like its a missing jar problem.. You need to add a connector library to the Runtime classpath `

java -cp .;mysql-connector-java-5.1.25-bin.jar GetCon` 

My example uses Windows classpath separator ";", on other systems it may be different (":" on Linux). It also assumes, that mysql-connector-java-5.1.25-bin.jar is located on the same folder. If it's not the case, then put a path to the library instead of the plain name.

Comments