我是JSP和Java的初学者。当我尝试打开用Java开发的Web应用程序时,当我尝试在系统中注册帐户时会出现此错误。系统界面显示正确。但是,每当我尝试键入某些数据时,它都会弹出错误。
Netbeans已连接到Oracle数据库,数据库已启动。我不知道是什么造成这种情况,我检查了GlassFish服务器日志,仍然无法找出问题所在。以下是GlassFish服务器日志:
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)
问题始于CreateServlet.java。以下是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();
}
}
RegisterUser类代码:
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;
}
}
这是MyListener.java代码,这里也有第13行的java.lang.NullPointerException。
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");
}
}
感谢您阅读本文,感谢有人提供帮助。