title: java.lang.IllegalStateException: This is not a JSON Array
body: <p>I am having two problems:</p>
  
  <p>1) java.lang.IllegalStateException: This is not a JSON Array</p>
  
  <p>this I am getting when trying to convert List to JSON. Also plz tell me how to handle null array to json. </p>
  
  <p>2) When I run the query on database it is fetching me 5 results.  When I debug the OperatorDetailsDao.java I get rs.next() to run only once. And another problem is that I get null result in operatorList though it is array of operators. </p>
  
  <p>Please help me to sort out the problem, stuck for two days. </p>
  
  <p><strong>Controller Class</strong> </p>
  
  <pre><code>public class SearchController extends HttpServlet {
    private static final long serialVersionUID = 1L;
  
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
  
              response.setContentType("application/json");
              try {
                  String term = request.getParameter("terms");
                  System.out.println("Data from ajax call " + term);
  
                  OperatorDetialsDao details = new OperatorDetialsDao();
                  List&lt;OperatorDetailsBean&gt; operatorList = details.getOperators(term);
                              System.out.println("Operator List " + operatorList); 
                  JsonElement element = new Gson().toJsonTree(operatorList);
                              System.out.println(element);
                              JsonArray jsonArray = element.getAsJsonArray();
                              response.getWriter().print(jsonArray);
                              response.getWriter().flush();
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
  }
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
               doPost(request, response);
      }
  
  }
  </code></pre>
  
  <p>OperatorDetailsDao -> getOperators()</p>
  
  <pre><code>public List&lt;OperatorDetailsBean&gt; getOperators(String name) {
            PreparedStatement ps = null;
            List&lt;OperatorDetailsBean&gt; operatorList = new ArrayList();
            operatorList = null;
            name = name.toUpperCase();
            String data;
            try {
                ps = connection.prepareStatement("SELECT * FROM operator_details_m  WHERE upper(name) LIKE ?");
                ps.setString(1, name + '%');
                ResultSet rs = ps.executeQuery();
  
                while (rs.next()) {
                    uname = rs.getString("NAME");
                    token = rs.getString("TOKEN_NO");
                    OperatorDetailsBean op = new OperatorDetailsBean();
                    op.setName(uname);
                    op.setTokenNo(token);
                    operatorList.add(op);
                }
  
            } catch (Exception e) {
                //System.out.println(e.getMessage());
            }
            return operatorList;
  }
  </code></pre>
  
  <p>OperatorDetailsBean is a bean class with getter and setters.</p>