提交数据后显示java.lang.NullPointerException错误

时间:2019-05-03 14:59:36

标签: java mysql netbeans-7

我已经使用NetBeans和servlets MySQL创建了一个Java Web项目。单击后,提交按钮显示以下错误。

HTTP状态500-

类型例外报告

消息

说明服务器遇到内部错误(),导致服务器无法满足此请求。

例外

java.lang.NullPointerException note可以在GlassFish Server Open Source Edition 3.1.2日志中找到有关异常及其根本原因的完整堆栈跟踪。

GlassFish Server开源版3.1.2

我尝试了使用StackOverflow和Internet的几种解决方案,但是没有用。我认为可能由于我的代码错误而发生此错误。但是我不明白错误在哪里。

DbConnection

    import com.mysql.jdbc.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class DbConnection {
        Connection con;

       public Connection getCon(){
            try {
               Class.forName("com.mysql.jdbc.Driver");
              >DriverManager.getConnection("jdbc:mysql://localhost:3306/bus_management_system", "root", "");

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
            }

            return con;
        }

    }
**Servlet**
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class route extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int rnumber = Integer.parseInt(request.getParameter("route_number"));
            String rname = request.getParameter("route_name");

            DbConnection db = new DbConnection ();
            Connection con = db.getCon();
            Statement stmt = (Statement) con.createStatement();
            stmt.executeUpdate("INSERT INTO route(Route_Number,Route_Name) VALUES ('"+rnumber+"','"+rname+"') ");
            out.println("Inserted Succesfully");

        }catch (SQLException ex){
            Logger.getLogger(route.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Route.jsp

                        <form  data-parsley-validate class="form-horizontal form-label-left" action ="route" method="post">
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="number">Route Number <span class="required">*</span>
                                </label>
                               <div class="col-md-6 col-sm-6 col-xs-12">
                                <input type="number" id="number" name="route_number" required="required" data-validate-minmax="1,100000" class="form-control col-md-7 col-xs-12">
                                </div>
                            </div>
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Route Name <span class="required">*</span>
                               </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                <input id="name" class="form-control col-md-7 col-xs-12" data-validate-length-range="100" data-validate-words="1" name="route_name" required="required" type="text">
                                </div>
                            </div>

                          <div class="form-group">
                            <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                              <button class="btn btn-info btn-sm" name="save">Save<i class="fa fa-paper-plane-o ml-1"></i></button>
                            </div>
                          </div>

                        </form>

0 个答案:

没有答案