私はplay1.2.5を使用しています。ビューからajaxを介してJSONオブジェクトを送信していますが、コントローラーでNullPointerExceptionが発生します。以下は私のコードです。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.
getXhrObject()
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
コントローラー: これが私のコントローラーのコードです。リクエストを送信すると、nullポインタ例外がスローされます。
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