我正在使用播放 1.2.5。我正在通过 ajax 从视图发送 JSON 对象,但在控制器中出现空指针异常。下面是我的代码。它从 Html 字段中获取值并将它们发送到 Play 控制器。
看法
function submitData(){
'use strict';
var user = document.getElementById('user').value;
var pass = document.getElementById('pass').value;
var message = "";
if(user == "" && pass==""){
//message=null;
message = "Please Enter User and Password";
document.getElementById('submitb').disabled=true;
}else if(user === ""){
//message= null;
message = " Please Enter User name";
document.getElementById('submitb').disabled=true;
}else if(pass == ""){
//message=null;
message = "Please Enter Password";
document.getElementById('submitb').disabled=true;
}
if(message!=""){
document.getElementById('result').innerHTML = message;
document.getElementById('submitb').disabled= false;
message = null;
}else{
sendAjax();
}
} // ending submit function
SendAjax 功能:
function sendAjax(){
'use strict';
var ajax = getXhrObject();
ajax.onreadystatechange = handleAjaxResponse;
//var userData =
var data = {'user': document.getElementById('user').value,'pass': document.getElementById('pass').value}
var jsonData = JSON.stringify(data);
ajax.open('POST', '/validate', true);
ajax.setRequestHeader('contentType', 'application/json');
var ajaxAbortTimer = setTimeout(function() { //abort timer
if (ajax) {
ajax.abort();
ajax = null;
} // second 'if (ajax)' ends here
}, 5000);
ajax.send(jsonData);
}// ending sendAjax function.
获取XhrObject()
function getXhrObject(){
'use strict';
var ajax = null;
try{
if(window.XMLHttpRequest){
ajax = new XMLHttpRequest();
}else if(window.ActiveXObject){
ajax = new ActiveXObject();
}
}catch(e){
console.log(e);
}
return ajax;
} // ending getXhrObject Function
控制器: 这是我的控制器的代码。当我向它发送请求时,会引发空指针异常。
public static void validate(Request request){
String status = null, response = null;
List<String> credInfo = new ArrayList<String>();
if (request.contentType.equals("application/json")){
Iterator<String> it = request.params.allSimple().keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value = request.params.get(key);
Logger.info("value" + value);
JsonElement body = new JsonParser().parse(value);
JsonObject jsObj = body.getAsJsonObject();
JsonElement JsonEmail = jsObj.get("user");
JsonElement JsonPassword = jsObj.get("pass");
String email = JsonEmail.getAsString();
String password = JsonPassword.getAsString();
System.out.println("email: "+email);
System.out.println("password:"+password);
if(!email.equals(user) && !password.equals(pass)){
status = "fail"; response = "Authentication failed";
}
}
}
renderJSON("{\"status\":"+status+" \response\":"+response+" }");
}
异常的堆栈跟踪是:
Internal Server Error (500) for request POST /validate
Execution exception (In /app/controllers/Application.java around line 30)
NullPointerException occured : Try to read contentType on null object play.mvc.Http$Request (controllers.Application.validate, line 30)
play.exceptions.JavaExecutionException: Try to read contentType on null object play.mvc.Http$Request (controllers.Application.validate, line 30)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException: Try to read contentType on null object play.mvc.Http$Request (controllers.Application.validate, line 30)
at play.classloading.enhancers.PropertiesEnhancer$FieldAccessor.invokeReadProperty(PropertiesEnhancer.java:213)
at controllers.Application.validate(Application.java:30)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
... 1 more