servlet中空指针异常问题

yrgyrg0409 发布于 2014/03/13 13:46
阅读 2K+
收藏 0
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
String registername = "";
String registeremail = "";
String registerpassword = "";
List<Object> params = new ArrayList<Object>();

DataOutputStream output=new DataOutputStream(response.getOutputStream()); 
registername = request.getParameter("registername");
registeremail = request.getParameter("registeremail");
registerpassword = request.getParameter("registerpassword");

// System.out.println(registername);
// System.out.println(registeremail);
// System.out.println(registerpassword);
params.add(registername);
params.add(registeremail);
params.add(registerpassword);

try { 
boolean flag = service.register(params);
if(flag){
output.writeUTF("SUCCESS");
}else{
output.writeUTF("FAIL");
}
}catch(Exception e){
e.printStackTrace();
}finally{
output.flush();
output.close();
}

}

这段代码一直在flag = service.register(params); 这行报告空指针异常,求解??
service的定义private RegisterService service= new RegisterDao();
RegisterService是接口,register方法的定义

public boolean register(List<Object> params) {
// TODO Auto-generated method stub
boolean flag = false;
String sql ="insert into userinfo(username,email,password) values(?,?,?)";
try {
jdbcUtil.getConnection();
flag = jdbcUtil.updateByPreparedStatement(sql, params);

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
jdbcUtil.releaseConn();
}
return flag;
}

加载中
0
Brin想写程序
Brin想写程序

推荐你

doPost里面把RegisterService service= new RegisterDao();这句加上看看。

去掉你的private的service声明

因为在servlet里面不要用自己的成员对象,这个是servlet常识。

Brin想写程序
Brin想写程序
回复 @yrgyrg0409 : 能把错误堆栈打印出来看吗?
y
yrgyrg0409
谢谢回答,试了下还是这样的错误
0
Brin想写程序
Brin想写程序

回复 @yrgyrg0409

还有获取parameter值,推荐你用下面这个静态方法。  

public static String getString(HttpServletRequest request, String name) {

        Object retObj = request.getParameter(name);
        if (retObj == null) {
            retObj = request.getAttribute(name);
            if (retObj == null) {
                return "";
            } else {
                return (String) retObj;
            }
        } else {
            return (String) retObj;
        }
    }
0
y
yrgyrg0409

引用来自“Brin想写程序”的答案

回复 @yrgyrg0409

还有获取parameter值,推荐你用下面这个静态方法。  

public static String getString(HttpServletRequest request, String name) {

        Object retObj = request.getParameter(name);
        if (retObj == null) {
            retObj = request.getAttribute(name);
            if (retObj == null) {
                return "";
            } else {
                return (String) retObj;
            }
        } else {
            return (String) retObj;
        }
    }
java.lang.NullPointerException
at com.mywork.util.jdbc.JdbcUtil.updateByPreparedStatement(JdbcUtil.java:48)
at com.mywork.util.register.dao.RegisterDao.register(RegisterDao.java:22)
at com.mywork.util.register.action.RegisterAction.doPost(RegisterAction.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


0
y
yrgyrg0409

引用来自“yrgyrg0409”的答案

引用来自“Brin想写程序”的答案

回复 @yrgyrg0409

还有获取parameter值,推荐你用下面这个静态方法。  

public static String getString(HttpServletRequest request, String name) {

        Object retObj = request.getParameter(name);
        if (retObj == null) {
            retObj = request.getAttribute(name);
            if (retObj == null) {
                return "";
            } else {
                return (String) retObj;
            }
        } else {
            return (String) retObj;
        }
    }
java.lang.NullPointerException
at com.mywork.util.jdbc.JdbcUtil.updateByPreparedStatement(JdbcUtil.java:48)
at com.mywork.util.register.dao.RegisterDao.register(RegisterDao.java:22)
at com.mywork.util.register.action.RegisterAction.doPost(RegisterAction.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


(RegisterAction.java:69就是刚才那个错误

RegisterDao.java:22是flag = jdbcUtil.updateByPreparedStatement(sql, params);这行报错,这行代码在

public boolean register(List<Object> params) {
// TODO Auto-generated method stub
boolean flag = false;
String sql ="insert into userinfo(username,email,password) values(?,?,?)";
try {
jdbcUtil.getConnection();
flag = jdbcUtil.updateByPreparedStatement(sql, params);

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
jdbcUtil.releaseConn();
}
return flag;
}
中,

最后

public boolean updateByPreparedStatement(String sql,List<Object> params) throws Exception{
boolean flag = false;
int result = -1;
pps = connection.prepareStatement(sql);
int index = 1;
if(params!=null&&!params.isEmpty()){
for (int i = 0; i < params.size(); i++) {
pps.setObject(index++, params.get(i));
}
}
result = pps.executeUpdate();
flag = result>0?true:false;
return flag;

}

中的pps = connection.prepareStatement(sql);就是JdbcUtil.java:48

0
Brin想写程序
Brin想写程序

引用来自“yrgyrg0409”的答案

引用来自“Brin想写程序”的答案

回复 @yrgyrg0409

还有获取parameter值,推荐你用下面这个静态方法。  

public static String getString(HttpServletRequest request, String name) {

        Object retObj = request.getParameter(name);
        if (retObj == null) {
            retObj = request.getAttribute(name);
            if (retObj == null) {
                return "";
            } else {
                return (String) retObj;
            }
        } else {
            return (String) retObj;
        }
    }
java.lang.NullPointerException
at com.mywork.util.jdbc.JdbcUtil.updateByPreparedStatement(JdbcUtil.java:48)
at com.mywork.util.register.dao.RegisterDao.register(RegisterDao.java:22)
at com.mywork.util.register.action.RegisterAction.doPost(RegisterAction.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


registername = request.getParameter("registername");  
registeremail = request.getParameter("registeremail");  

registerpassword = request.getParameter("registerpassword"); 

初步看是上面的三个object可能有一个为空。。

0
y
yrgyrg0409

引用来自“Brin想写程序”的答案

引用来自“yrgyrg0409”的答案

引用来自“Brin想写程序”的答案

回复 @yrgyrg0409

还有获取parameter值,推荐你用下面这个静态方法。  

public static String getString(HttpServletRequest request, String name) {

        Object retObj = request.getParameter(name);
        if (retObj == null) {
            retObj = request.getAttribute(name);
            if (retObj == null) {
                return "";
            } else {
                return (String) retObj;
            }
        } else {
            return (String) retObj;
        }
    }
java.lang.NullPointerException
at com.mywork.util.jdbc.JdbcUtil.updateByPreparedStatement(JdbcUtil.java:48)
at com.mywork.util.register.dao.RegisterDao.register(RegisterDao.java:22)
at com.mywork.util.register.action.RegisterAction.doPost(RegisterAction.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


registername = request.getParameter("registername");  
registeremail = request.getParameter("registeremail");  

registerpassword = request.getParameter("registerpassword"); 

初步看是上面的三个object可能有一个为空。。

我改成你给的那个方法了 还是一样,,
0
Brin想写程序
Brin想写程序

引用来自“yrgyrg0409”的答案

引用来自“Brin想写程序”的答案

引用来自“yrgyrg0409”的答案

引用来自“Brin想写程序”的答案

回复 @yrgyrg0409

还有获取parameter值,推荐你用下面这个静态方法。  

public static String getString(HttpServletRequest request, String name) {

        Object retObj = request.getParameter(name);
        if (retObj == null) {
            retObj = request.getAttribute(name);
            if (retObj == null) {
                return "";
            } else {
                return (String) retObj;
            }
        } else {
            return (String) retObj;
        }
    }
java.lang.NullPointerException
at com.mywork.util.jdbc.JdbcUtil.updateByPreparedStatement(JdbcUtil.java:48)
at com.mywork.util.register.dao.RegisterDao.register(RegisterDao.java:22)
at com.mywork.util.register.action.RegisterAction.doPost(RegisterAction.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


registername = request.getParameter("registername");  
registeremail = request.getParameter("registeremail");  

registerpassword = request.getParameter("registerpassword"); 

初步看是上面的三个object可能有一个为空。。

我改成你给的那个方法了 还是一样,,
额。。。我不知道了。闪人。最后看到catalina了,感觉你用的是tomcat,tomcat的缓存在调试的时候很麻烦。。可能需要清理下,否则就老调试之前的旧代码。glassfish就没这个问题。
0
Brin想写程序
Brin想写程序

引用来自“yrgyrg0409”的答案

引用来自“yrgyrg0409”的答案

引用来自“Brin想写程序”的答案

回复 @yrgyrg0409

还有获取parameter值,推荐你用下面这个静态方法。  

public static String getString(HttpServletRequest request, String name) {

        Object retObj = request.getParameter(name);
        if (retObj == null) {
            retObj = request.getAttribute(name);
            if (retObj == null) {
                return "";
            } else {
                return (String) retObj;
            }
        } else {
            return (String) retObj;
        }
    }
java.lang.NullPointerException
at com.mywork.util.jdbc.JdbcUtil.updateByPreparedStatement(JdbcUtil.java:48)
at com.mywork.util.register.dao.RegisterDao.register(RegisterDao.java:22)
at com.mywork.util.register.action.RegisterAction.doPost(RegisterAction.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


(RegisterAction.java:69就是刚才那个错误

RegisterDao.java:22是flag = jdbcUtil.updateByPreparedStatement(sql, params);这行报错,这行代码在

public boolean register(List<Object> params) {
// TODO Auto-generated method stub
boolean flag = false;
String sql ="insert into userinfo(username,email,password) values(?,?,?)";
try {
jdbcUtil.getConnection();
flag = jdbcUtil.updateByPreparedStatement(sql, params);

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
jdbcUtil.releaseConn();
}
return flag;
}
中,

最后

public boolean updateByPreparedStatement(String sql,List<Object> params) throws Exception{
boolean flag = false;
int result = -1;
pps = connection.prepareStatement(sql);
int index = 1;
if(params!=null&&!params.isEmpty()){
for (int i = 0; i < params.size(); i++) {
pps.setObject(index++, params.get(i));
}
}
result = pps.executeUpdate();
flag = result>0?true:false;
return flag;

}

中的pps = connection.prepareStatement(sql);就是JdbcUtil.java:48

数据库打开失败?
返回顶部
顶部