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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题