yiqianyan 2017-03-27 05: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 05:32
    关注
        //String customer_id_card = request.getParameter("customer_id_card ");
        //String customer_sex = request.getParameter("customer_sex ");
                这两行"customer_id_card "这里面有空格。你去掉就可以了。它把空格也算在字符串中了。
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部