如何避免NullPointerException?

时间:2017-03-21 12:42:58

标签: java mysql nullpointerexception

  

Html代码

     

这里的代码是获取同名的多个输入并添加数据库。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Multiple Input</title>
</head>
<body>
<form action="Multiple" method="post">
        <div class="table-responsive">
        <table class="table table-bordered table-hover" id="sample-table-1">
            <thead>
                <tr>
                    <th>familymembers</th>
                    <th>name</th>
                    <th>relationship</th>
                    <th>age</th>
                    <th>occupation</th>
                    <th>education</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>
                        <input type="text" value="" class="form-control" id="name1" label="" name="name" >
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="relation" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="age" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="occupation" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="education" label="">
                    </td>
                </tr>
                <tr>
                    <td>2</td>
                    <td>
                        <input type="text" value="" class="form-control" id="name2" label="" name="name" >
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="relation" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="age" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="occupation" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="education" label="">
                    </td>
                </tr>
                <tr>
                    <td>3</td>
                    <td>
                        <input type="text" value="" class="form-control" id="name3" label="" name="name" >
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="relation" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="age" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="occupation" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="education" label="">
                    </td>
                </tr>
                <tr>
                    <td>4</td>
                    <td>
                        <input type="text" value="" class="form-control" id="name4" label="" name="name" >
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="relation" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="age" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="occupation" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="education" label="">
                    </td>
                </tr>
                <tr>
                    <td>5</td>
                    <td>
                        <input type="text" value="" class="form-control" id="name5" label="" name="name" >
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="relation" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="age" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="occupation" label="">
                    </td>
                    <td>
                        <input type="text" value="" class="form-control" name="education" label="">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <input type="submit" value="submit">
    </div>
</form>
</body>
</html>
  

Java代码

     

我正在尝试将数据设置到数据库中,它获取同名的多个输入。但是我得到了NullPointerException。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            response.setContentType("text/html");
            PrintWriter out=response.getWriter();
            String[] name=request.getParameterValues("name");
            String[] relation=request.getParameterValues("relationship");
            String[] age=request.getParameterValues("age");
            String[] occupation=request.getParameterValues("occupation");
            String[] education=request.getParameterValues("education");

            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sportsevent", "root", "root");

            PreparedStatement ps=con.prepareStatement("insert into multiple(idMultiple, name, relationship, age, occupation, education) values(10,?,?,?,?,?)");
            for (int i = 0; i < name.length; i++) {
                if(name[i] != null) {
                ps.setLong(1, 10);
                ps.setString(2, name[i]);
                ps.setString(3, relation[i]);
                ps.setString(4, age[i]);
                ps.setString(5, occupation[i]);
                ps.setString(6, education[i]);
                }
            }
            ps.executeUpdate();
            ResultSet s=ps.executeQuery();
            if(s != null)
            System.out.println("inserted"+s);
            s.close();
        }catch (Exception e){System.out.println(e);}
    }

0 个答案:

没有答案