I am passing a JSON object through Ajax call in JavaScript function. I have linked the generated action to a struts action class. I have the control in action class, but my setters are not being called, because of which I am facing a NullPointerException
.
Here is my Ajax call in JavaScript
var arr = {entity:entity,subEntity:subEntity,choice:choice,drinkingWaterChosen:drinkingWaterChosen,roadChosen:roadChosen,groupChoice:groupChoice};
console.log(JSON.stringify(arr));
url = "<s:url action='FetchQuery'/>";
$.ajax({
type : 'POST',
url : url,
dataType : 'json',
contentType: 'application/json; charset=utf-8',
data:JSON.stringify(arr),
async : false,
success : function(data) {
console.log(data);
alert("I am back "+data);
}
});
My Struts config file is as shown below
<struts>
<package name="default" extends="json-default">
<result-types>
<result-type name="json" class="org.apache.struts2.json.JSONResult" />
</result-types>
<interceptors>
<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />
</interceptors>
<action name="FetchQuery" class="serp.FetchQuery">
<result type="json"/>
</action>
</package>
My Action class is as shown below
private String entity;//getter and setters for all variables
private String subEntity;
private String choice;
private String drinkingWaterChosen;
private String roadChosen;
private String groupChoice;
public String execute(){
System.out.println("Hello world>>>>>>>>>>>>>>>>>");
String msg = constructQuery();
return msg;
}
private String constructQuery() {
if (entity.equals("Hello")) {} //Null pointer exception is raised at this line
}
I think this error is because the entity is not being set before hand. So my question is how to handle the JSON object that is passed to the action class. I have read many questions on this site and tried all the ways, but still I couldn't figure out the problem. Any help would be great.