发送 json 数据时的空指针

Posted

技术标签:

【中文标题】发送 json 数据时的空指针【英文标题】:nullpointer in sending json data 【发布时间】:2018-11-08 00:32:52 【问题描述】:

我在将 json 数据从前端引导程序发送到 express servlet 时遇到问题。这是我尝试过的。我使用 ajax 将数据从我的表单发送到带有 json 的 servlet 类。请帮我解决这个问题。这是我的学生注册表格:

<html>
<head>
<link rel="stylesheet" type="text/css" href="resources/css/bootstrap.min.css">
<script type="text/javascript" src="resources/js/jquery.min.js"></script>
<script type="text/javascript" src="resources/js/bootstrap.min.js">   </script>
<script type="text/javascript">
    function registerStudent() 
        var firstName = $("#firstName").val();
        var lastname = $("#lastname").val();
        var phone = $("#phone").val();
        var email = $("#email").val();
        var studentCode = $("#studentCode").val();
        $.ajax(
            type : "POST",
            url : "/RegisterStudent",
            data :data: "firstName=" + firstName + "lastname=" + lastname + " phone=" +  phone + "email=" + email+"studentCode"+studentCode,
            success : function(data) 
                var ht = data.msg;
                $("#resp").html(ht);
            ,
            error : function(data) 
                alert("Some error occured.");
            
        );
    
</script>
</head>
<body>

<div class="container">

<div class="card " style="width:750px;margin:0px auto">


    <div class="card-header">Student Register</div>

    <div class="card-body">


        <form data-toggle="validator" role="form" method="post" >


            <div class="form-group">

                <label class="control-label" for="firstName">Name</label>

                <input class="form-control" data-error="Please enter name field." id="firstName" placeholder="Name"  type="text" required />

                <div class="help-block with-errors"></div>

            </div>


            <div class="form-group">

                <label for="lastname" class="control-label">lastname</label>

                <input type="text" class="form-control"   id="lastname" placeholder="lastname" required>

                <div class="help-block with-errors"></div>

            </div>


            <div class="form-group">

                <label for="phone" class="control-label">phone</label>

                <div class="form-group">

                    <input type="text" data-minlength="5" class="form-control" id="phone" data-error="fill your phone" placeholder="phone" required>

                    <div class="help-block with-errors"></div>

                </div>

            </div>


            <div class="form-group">

                <label class="control-label" for="email">email</label>

                <textarea class="form-control" data-error="Please enter email field." id="email" placeholder="email" required=""></textarea>

                <div class="help-block with-errors"></div>

            </div>

            <div class="form-group">

                <label for="studentCode" class="control-label">studentCode</label>

                <div class="form-group">

                    <input type="text"  class="form-control" id="studentCode" data-error="fill your studentCode" placeholder="studentCode" required>

                    <div class="help-block with-errors"></div>

                </div>

            </div>


        </form>
    </div>
    <button onclick="registerStudent();" type="button" class="btn btn-success btn-block">Register</button>
    <div class="text-center" id="resp" style="margin-top: 14px;"></div>
</div>
</div> 

这是我将数据发送到数据库的 servlet 类。

@WebServlet(urlPatterns = "/RegisterStudent")
public class RegisterStudentController extends HttpServlet 
StudentServiceInter service = new StudentServiceImpl();
ObjectMapper mapper = new ObjectMapper();
String json = "";

protected void doGet(HttpServletRequest req, HttpServletResponse resp)    throws ServletException, IOException 
    Student student = new Student();
    req.getSession().setAttribute("student", student);
    resp.sendRedirect("/addStudent.jsp");


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
    String jsonData = req.getParameter("data");
    resp.setContentType("application/json");
    PrintWriter out = resp.getWriter();
    out.println(jsonData);
    out.close();


【问题讨论】:

您是否收到此警报(“发生了一些错误。”);?如果没有,你能检查一下你是否要去那个网址吗? @BILALMALIK 是的,我收到了这个警报 【参考方案1】:

试试这个...

 $.ajax(
                            type: 'GET',
                            url: '<?php echo base_url(); ?>"/RegisterStudent"', // your syntax
                            data: 
                                'firstName ': firstName ,
                                'lastName'  :lastname,
                                'phone': phone,
                                'email' : email,
                                'studentCode' : studentCode,

                            ,
                           success : function(data) 
                    var ht = data.msg;
                    $("#resp").html(ht);
                ,
                error : function(data) 
                    alert("Some error occured.");
                
                        );

【讨论】:

【参考方案2】:

一个问题是,客户端,您没有创建 json,而服务器端您尝试解析 json。 Json 格式没有equal (=) 符号。

一个有效的对象是:

data = 
    data: 
        firstName: 'Name',
        lastName: 'Surname',
        phone: '123-4456',
        email: 'a@b.c',
        studentCode: 12345
    
;

在发送之前你应该做JSON.stringify(data)

【讨论】:

以上是关于发送 json 数据时的空指针的主要内容,如果未能解决你的问题,请参考以下文章

从 Android 的 LocationManager 请求 getLastKnownLocation 时的空指针

在 Spring Boot 2 中测试组件时的空指针

setText时的空指针异常 - TextView [复制]

获取表单信息,进行对比时的空指针错误

将 HTML 画布图像传递给控制器​​方法时的空指针

在 java xml 应用程序中初始化控制器时的空指针