0

I am full beginner in JSP and Java. When I try to open a web application developed in Java, this error shows up when I try to register an account in the system. The interface of the system displayed correctly. However, everytime I try to key in some data it pops up the error.

Error Screenshot

The Netbeans had connected to Oracle database and the database is start. I have no idea what causing this, I checked the GlassFish Server Logs and still could not find out the problem. Below are the GlassFish server logs:

    Warning:   StandardWrapperValve[CreateServlet]: Servlet.service() for servlet CreateServlet threw exception
java.lang.NullPointerException
    at g.RegisterUser.register(RegisterUser.java:14)
    at g.CreateServlet.doGet(CreateServlet.java:37)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)

The problem start at CreateServlet.java. Here are the code in CreateServlet.java

package g;
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 {

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

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

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String  repassword=request.getParameter("repassword");

        String amoun=request.getParameter("amount");
        double amount=Double.parseDouble(amoun);

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

        String ph=request.getParameter("phone");
        double phone=Double.parseDouble(ph);
        //double mname=Double.parseDouble(num);
        //String country=request.getParameter("country");

        int status=RegisterUser.register(username, password, repassword, amount, adderess,phone);


        if(status>0){
            out.print("WELCOME! YOUR ACCOUNT HAS OPENED");
            RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
            rd.include(request, response);
        }
        else{
            out.print("Sorry,Registration failed. please try later");
            RequestDispatcher rd=request.getRequestDispatcher("MyHtml.html");
            rd.include(request, response);
        }

    out.close();    
    }

}

The RegisterUser class code:

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

    Connection con=GetCon.getCon();
    PreparedStatement ps;
    try {
        ps = con.prepareStatement("Insert into NEWACCOUNT values(?,?,?,?,?,?,?)");
        int nextvalue1=GetCon.getPrimaryKey();
        ps.setInt(1,nextvalue1);
        ps.setString(2,username);
        ps.setString(3,password);
        ps.setString(4,repassword);
        ps.setDouble(5,amount);
        ps.setString(6,adderess);
        ps.setDouble(7,phone);

        status=ps.executeUpdate();

    } catch (SQLException e) {

        e.printStackTrace();
    }
    return status;

}
}

And here is the MyListener.java code, here had the java.lang.NullPointerException at line 13 as well.

package g;
import javax.servlet.*;
import java.sql.*;

public class MyListener implements ServletContextListener{

    public void contextInitialized(ServletContextEvent arg0) {
        int status=0;
        Connection con=null;

      try{
        con=GetCon.getCon();
        PreparedStatement ps1=con.prepareStatement("Select * from NEWACCOUNT");


      try{
        status=ps1.executeUpdate();
        }

      catch(Exception e)
        {e.printStackTrace();
         status=2;
         System.out.println("my staus is 1111111"+status);
         }      

       if(status==0)
        {System.out.println("your table name already exist"+status);}


       else if(status==2) 

        {System.out.println("else if part table does not exist new table has created"+status);
        PreparedStatement ps3=con.prepareStatement("CREATE SEQUENCE javatpointnewaccount MINVALUE 1 MAXVALUE 999999999999 INCREMENT BY 1 START WITH 1 NOCACHE  NOORDER  NOCYCLE");
        ps3.executeUpdate();

        PreparedStatement ps=con.prepareStatement("CREATE TABLE  NEWACCOUNT(ACCOUNTNO NUMBER,USERNAME VARCHAR2(4000),PASSWORD VARCHAR2(4000),REPASSWORD VARCHAR2(4000),AMOUNT NUMBER,ADDERESS VARCHAR2(4000),PHONE NUMBER,PRIMARY KEY (ACCOUNTNO))");
        ps.executeUpdate();




        }


        else{System.out.println("else part "+status);
        }}
        catch(Exception e){e.printStackTrace();}
        }
        public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("project undeployed");

    }
}

Thank you for reading this and appreciate if anyone could help.

Sivvie Lim
  • 784
  • 2
  • 14
  • 43
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Dazak Mar 31 '17 at 12:50
  • @Dazak I know what is a NullPointerException, but I couldn't found it in the code. Is missing javadoc considered as NullPointer? – Sivvie Lim Mar 31 '17 at 12:54
  • How do you try to run your web application? What's the URL? – dsp_user Mar 31 '17 at 12:56
  • @Corene it is in the code. What's at MyListener.java line 13? You should post your code otherwise it's very difficult for anyone here to help you. It has nothing to do with Javadoc. – JJF Mar 31 '17 at 12:58
  • @dsp_user The URL is http://localhost:8080/onlinebanking/ – Sivvie Lim Mar 31 '17 at 13:00
  • @AlexPoole I have found the problem class and I added the code in that class already. Please have a check. Thank you! – Sivvie Lim Mar 31 '17 at 13:03
  • @JJF I have found the problem class and I added the code in that class already. Please have a check. Thank you! – Sivvie Lim Mar 31 '17 at 13:03
  • The problem (NPE) appears to be in the register method (at line 14). – dsp_user Mar 31 '17 at 13:06
  • @dsp_user I have updated the code for the method. Is it because I did not initialize the PreparedStatement or the parameter? – Sivvie Lim Mar 31 '17 at 13:13
  • Why are using executeUpdate when you have a select query. (You should use executeQuery instead) – dsp_user Mar 31 '17 at 13:21
  • @dsp_user Because the status variable is declared as int not ResultSet... – Sivvie Lim Mar 31 '17 at 13:34
  • You really shouldn't do that even if your JDBC driver supports it (the Javadoc is quite clear https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeUpdate() – dsp_user Mar 31 '17 at 13:42
  • con == null @line 13 in MyListener. Read link on what a NP is and how to fix it. Debugger is your friend here – JJF Mar 31 '17 at 13:43

0 Answers0