1

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
chaitu
  • 1,036
  • 5
  • 20
  • 39

2 Answers2

2

The error could be your array, double quotes are missing. Change it to this form:

var arr = {
              'entity' : entity, 
           'subEntity' : subEntity, 
              'choice' : choice,
 'drinkingWaterChosen' : drinkingWaterChosen, 
          'roadChosen' : roadChosen, 
         'groupChoice' : groupChoice
}; 
user3386877
  • 515
  • 1
  • 5
  • 12
2

To handle JSON object you need to add json interceptor to the action config.

<action name="FetchQuery" class="serp.FetchQuery">
    <result type="json"/>
  <interceptor-ref name="json">
  <interceptor-ref name="defaultStack">
</action>

The json interceptor is able to parse the request, and populate the action object with data. This is excerpt from the documentation page:

If the interceptor is used, the action will be populated from the JSON content in the request, these are the rules of the interceptor:

  • The "content-type" must be "application/json"
  • The JSON content must be well formed, see json.org for grammar.
  • Action must have a public "setter" method for fields that must be populated.
  • Supported types for population are: Primitives (int,long...String), Date, List, Map, Primitive Arrays, Other class (more on this later), and Array of Other class.
  • Any object in JSON, that is to be populated inside a list, or a map, will be of type Map (mapping from properties to values), any whole number will be of type Long, any decimal number will be of type Double, and any array of type List.
Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176