1

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;

}
}
jva
  • 13
  • 1
  • 4

1 Answers1

0

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.

Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
  • It a `NullPointerException`. – Boris the Spider Apr 05 '15 at 12:05
  • @viraj database Intializer `public interface DBIntializer { String DRIVER="com.mysql.jdbc.Driver"; String CON_STRING="jdbc:mysql://location:3306:demo"; String USERNAME="root"; String PASSWORD="root"; }` – jva Apr 05 '15 at 12:06
  • yes sir it is an null pointer but check the possible reason for it.. @BoristheSpider atleast have a look at the code before down voting.. – Viraj Nalawade Apr 05 '15 at 12:06
  • @user4751433 Can you replace String CON_STRING="jdbc:mysql://location:3306:demo" with String CON_STRING="jdbc:mysql://localhost/demo" and try.. I am quite sure we will move ahead doing this.. – Viraj Nalawade Apr 05 '15 at 12:13
  • @viraj i tried with this as you suggested but getting same error again – jva Apr 05 '15 at 12:17
  • @user4751433 ok but still we are sure that con is null.. Can we have a look at GetCon.getCon methods code..? – Viraj Nalawade Apr 05 '15 at 12:19
  • @viraj I've added GetCon class in above question now – jva Apr 05 '15 at 12:25
  • @user4751433 does it anywhere print "Exception in GetCon" ?? – Viraj Nalawade Apr 05 '15 at 12:30
  • @viraj its not printing "Exception in GetCon" but in GetCon.java class if i tries to print any normal line after class.forName(); syso(" print here") ; nothing gets printed in console – jva Apr 05 '15 at 12:47
  • If it prints "Exception in GetCon" check the message with SqlException using e.getMessage(); There could be a issue with missing driver, username password , access write database or something else.. – Viraj Nalawade Apr 05 '15 at 12:47
  • Ok so may be the problem is something else so can you just add one more catch statement (like catch (Exception e) {e.printStackTrace();System.out.print(e.getMessage());} after sqlexception catch closes) to catch all exceptions so that we dig into actual problem..? – Viraj Nalawade Apr 05 '15 at 12:51
  • Hi that means 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. – Viraj Nalawade Apr 05 '15 at 12:54
  • @viraj thanks for your reply problem solved it was related to jar file i did add jar in Libraries but it was not working then i added jar file in web-inf/lib folder – jva Apr 06 '15 at 14:04