yiqianyan
2017-03-27 13:07
采纳率: 90.9%
浏览 2.8k
已采纳

关于cookie使用中出现的空指针异常

customer_add.jsp:

 <form name="formName" method="post" action="customer_add_post.jsp">
        添加客户信息: <br> <br>

        <table width="100%" border="1" align="center" cellpadding="3"
            cellspacing="1" bordercolor="#00FFFF"
            style="border-collapse: collapse">
            <tr>
                <td>姓名:</td>
                <td><input name='customer_name' type='text'
                    style='border: solid 1px #000000; color: #666666' /></td>
            </tr>
            <tr>
                <td>身份证:</td>
                <td><input name='customer_id_card' type='text'
                     size='50'
                    style='border: solid 1px #000000; color: #666666' /></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td><select name='customer_sex'>
                        <option value="男">男</option>
                        <option value="女">女</option>
                </select></td>
            </tr>
            <tr>
                <td>地址:</td>
                <td><input name='customer_address' type='text' size='50'
                    style='border: solid 1px #000000; color: #666666' /></td>
            </tr>
            <tr>
                <td>电话:</td>
                <td><input name='customer_tel' type='text'
                    style='border: solid 1px #000000; color: #666666' /></td>
            </tr>
            <tr>
                <td>照片:</td>
                <td><input name='customer_photo' type='text' size='50'
                    style='border: solid 1px #000000; color: #666666' /> <input
                    type='button' value='上传' onClick="up('customer_photo')"
                    style='border: solid 1px #000000; color: #666666' /></td>
            </tr>
            <tr>
                <td>备注:</td>
                <td><textarea name='remarks' cols='50' rows='5'
                        style='border: solid 1px #000000; color: #666666'></textarea></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input name='customer_password' type='text'
                    style='border: solid 1px #000000; color: #666666' /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="submit" value="提交"
                    onclick="return check();"
                    style='border: solid 1px #000000; color: #666666' /> <input
                    type="reset" name="reset" value="重置"
                    style='border: solid 1px #000000; color: #666666' /></td>
            </tr>
        </table>
    </form>

customer_add_post.jsp:

 <%@ page language="java" pageEncoding="utf-8"
    import="java.sql.*,java.net.*,javabean.Customer"%>
<html>
<head>
<title></title>
</head>

<body>
    <%
        request.setCharacterEncoding("utf-8");

        //使用这三行出现错误:Stacktrace:] with root causejava.lang.NullPointerException at java.net.URLEncoder.encode(Unknown Source)
        //String customer_name = request.getParameter("customer_name");
        //String customer_id_card = request.getParameter("customer_id_card ");
        //String customer_sex = request.getParameter("customer_sex ");

        //不使用上面三行,而是使用下面这三行(明明和上面一模一样),无错误,并且数据表中能够顺利更新一行。
        String customer_name = request.getParameter("customer_name");
        String customer_id_card = request.getParameter("customer_id_card");
        String customer_sex = request.getParameter("customer_sex");

        Cookie customer_name_cookie = new Cookie("customer_name", URLEncoder.encode(customer_name, "utf-8"));
        Cookie customer_id_card_cookie = new Cookie("customer_id_card",
                URLEncoder.encode(customer_id_card, "utf-8"));
        Cookie customer_sex_cookie = new Cookie("customer_sex", URLEncoder.encode(customer_sex, "utf-8"));

        response.addCookie(customer_name_cookie);
        response.addCookie(customer_id_card_cookie);
        response.addCookie(customer_sex_cookie);

        Customer customer = new Customer();
        customer.customerInfoAdd(customer_name, customer_id_card, customer_sex);
    %>
</body>
</html>

Customer.java:

 package javabean;

import java.sql.*;

import db.DBUtil;

public class Customer {
    private String customer_name;
    private String customer_id_card;
    private String customer_sex;
    private String customer_address;
    private String customer_tel;
    private String customer_photo;
    private String remarks;
    private String customer_password;

    private Connection conn;
    private Statement stmt;

    public void customerInfoAdd( String customer_name,String customer_id_card,String customer_sex) {
        conn = DBUtil.getConnection();
        try {
            stmt = conn.createStatement();

            String sql = "insert into customer(customer_name,customer_id_card,customer_sex) values('"+customer_name+"','"+customer_id_card+"','"+customer_sex+"')";
            stmt.executeUpdate(sql);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

错误出现在customer_add_post.jsp注释处

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 飞翔IT 2017-03-27 13:32
    已采纳
        //String customer_id_card = request.getParameter("customer_id_card ");
        //String customer_sex = request.getParameter("customer_sex ");
                这两行"customer_id_card "这里面有空格。你去掉就可以了。它把空格也算在字符串中了。
    
    点赞 打赏 评论
  • qq_36013370 2017-03-27 13:20

    没看懂,代码太多了,眼花缭乱的。

    点赞 打赏 评论

相关推荐 更多相似问题