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

关于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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。