当前位置: 动力学知识库 > 问答 > 编程问答 >

java - What's wrong with my request.getParameter(abc)?

问题描述:

I have a form which has a doPost:

<form id="frmData" name="frmData" action="ProcessReg" method="post">

<td>Full Name</td>

<td><input type="text" id="stFullName" name="stFullName" value=""></td>

I am getting a HTTP 500 error and Tomcat log indicate the line that hit request.getParameter part as the problem.

java.lang.NullPointerException

ProcessReg.processRequest(ProcessReg.java:86)

ProcessReg.doPost(ProcessReg.java:32)

javax.servlet.http.HttpServlet.service(HttpServlet.java:647)

javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

And my servlet:

public class ProcessReg extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

PreparedStatement ps = null;

Connection con = null;

ResultSet rs = null;

Statement stmt = null;

String strName = "";

strName = request.getParameter("stFullName");

try {

DBConnect a = new DBConnect();

stmt = (Statement) DBConnect.getConnection();

rs = stmt.executeQuery("SELECT * FROM members");

I hope someone can tells me what went wrong cos before it was working perfectly and I really am lost where is the error.

Full stacktrace:

SEVERE: Servlet.service() for servlet [ProcessReg] in context with path [/Web1] threw exception

java.lang.NullPointerException

at ProcessReg.processRequest(ProcessReg.java:86)

at ProcessReg.doPost(ProcessReg.java:32)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)

at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)

at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)

at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)

at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

at java.lang.Thread.run(Thread.java:619)

And my DBConnect.java :

 public class DBConnect {

private static Connection con;

public static void createConnection(String dbUrl,String dbusername,String dbPassword){

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con = DriverManager.getConnection("jdbc:odbc:practODBC, 231, abc");

} catch (Exception ex) {

}

}

public static Connection getConnection(){

return con;

}

public static void closeConnection(){

if(con!=null){

try {

con.close();

} catch (SQLException ex) {

}

}

网友答案:

The problem is on line 86 of ProcessReg you are trying to call a method on an object, but the reference you have to that object == null, hence the NullPointerException.

Find that reference, and decide whether it being null at that point in the control flow is an error or not. If it's an error, fix that. If it's not an error, wait until you've set it to point to an object before using it to call methods on that object.

UPDATE

Now you've posted your code for DBConnection, it's clear there are many issues with your code. I'll try to point out the main ones, but as an aside I'd seriously recommend brushing up on the Java fundamentals here before going on to write relatively complex pieces of functionality like Servlets etc.

Issues in ProcessReg

you are calling getConnection() and returning con, but you haven't initialised con yet so you're just returning a null reference.

you are then trying to cast the Connection reference returned from getConnection() to the Statement type. This will throw a ClassCastException. Instead you'll want to call createStatement() on the returned Connection object reference.

all the methods on DbConnect are static (see below - you should refactor this) so you're instantiating a pointless DBConnect object a, then obviously never using it.

Issues in DBConnect

you have a static field con, and static methods to modify/access it. Don't do this. Make con an instance variable and tuck the initialisation inside the DBConnect constructor - something like this:

public class DBConnect {

    private Connection con;

    public DBConnect() {
        // setup con
    }

    public Connection getConnection() {
        return con; // con is never null because it's been initialised in constructor
    }

    ...

Then, in ProcessReg, instantiate a DBConnect object, and grab the Connection object it wraps by calling getConnection(), similarly to this:

PreparedStatement ps = null;
Connection con = null;
ResultSet rs = null;
Statement stmt = null;

String strName = "";

strName = request.getParameter("stFullName");

try {
     DBConnect dbConnect = new DBConnect();
     con = dbConnect.getConnection();
     stmt = con.createStatement();                   
     rs = stmt.executeQuery("SELECT * FROM members");
网友答案:

Problem is public static Connection getConnection() method returns a null connection as you are never calling createConnection() whhich actually loads the driver and creates the connection object. You need to fix your getConnection() to call createConnection() before returning con object in DBConnect class.

网友答案:

Make check for null:

if (stmt != null) {
    rs = stmt.executeQuery("SELECT * FROM members");
}

But your code looks very strange.

  1. You call the static method of the class DBConnect:

    DBConnect.getConnection();
    

    Then why do you create the object of this class?

    DBConnect a = new DBConnect();
    
  2. Why did you set return value of getConnection() method to variable of Statement type?

    stmt = (Statement) DBConnect.getConnection();
    
  3. For what purpose do you cast it to Statement? It would be need only if your method returns the Object refernce type. Casting is not necessary if the return type is subinterface or any implementation of Statement. And casting of Connection to Statement cannot be executed.

  4. Method DBConnect.createConnection() doesn't use taken parameters. They're hardcoded.

  5. The static field con will remain null if the method has not been called or has happened exception.

分享给朋友:
您可能感兴趣的文章:
随机阅读: