yiqianyan 2017-03-27 13:07 采纳率: 100%
浏览 2947
已采纳

关于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 "这里面有空格。你去掉就可以了。它把空格也算在字符串中了。
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)