title: java.lang.ClassCastException: org.json.JSONObject$Null cannot be cast to java.lang.String
body: <p>Exception is <code>java.lang.ClassCastException</code>: <code>org.json.JSONObject$Null</code> cannot be cast to <code>java.lang.String</code></p>
  
  <p><code>Printthis&gt;&gt;&gt;&gt;&gt;&gt;userName=user1&amp;passKey=12345678
  POST Response Code :: 200</code></p>
  
  <p><code>jsonObj&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;{"response":{"roleTitle":"Parent","roleId":146,"passKey":"12345678","id":1,"userName":"user1"},"errorCode":null,"error":false,"message":null}</code></p>
  
  <p><code>Jan 04, 2018 4:33:11 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with path [/balihans] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: org.json.JSONObject$Null cannot be cast to java.lang.String] with root cause
  java.lang.ClassCastException: org.json.JSONObject$Null cannot be cast to java.lang.String
      at com.swasth.general.controller.SwasthController.postLogin(SwasthController.java:2274)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Unknown Source)</code></p>
  
  <p><strong>Here is my postLogin code</strong></p>
  
  <pre><code>@RequestMapping(value = "/login", method = RequestMethod.POST)
  public ModelAndView postLogin(HttpServletRequest request,HttpServletRequest response) throws IOException, JSONException {
  
      ModelAndView addmodel = new ModelAndView("login");
      String uri = "http://localhost:8080/login/api/";
      URL url = new URL(uri);
      HttpURLConnection con = (HttpURLConnection)url.openConnection();
      con.setRequestMethod("POST");
  
      String userName = request.getParameter("userName");
      String passKey = request.getParameter("passKey");
      con.setDoOutput(true);
      OutputStream os = con.getOutputStream();
  
      os.write(("userName=" + userName + "&amp;passKey=" + passKey).getBytes());
      System.out.println("Printthis&gt;&gt;&gt;&gt;&gt;&gt;" + "userName=" + userName + "&amp;passKey=" + passKey);
      os.flush();
      os.close();
  
      int responseCode = con.getResponseCode();
      System.out.println("POST Response Code :: " + responseCode);        
  
      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
      String inputLine;
      StringBuffer responses = new StringBuffer();
  
      while ((inputLine = in.readLine()) != null) {
          responses.append(inputLine);
      }
      //System.out.println("responses&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;"+responses);
  
      String str = responses.toString();
      JSONObject jsonObj = new JSONObject(str);
  
      System.out.println("jsonObj&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;"+jsonObj);
  
      String res = (String)jsonObj.get("errorCode");
  
      // build a JSON object
  
      List&lt;PatentLoginInfo&gt; PatentLoginInfoArray = new ArrayList&lt;PatentLoginInfo&gt;();
      ObjectMapper mapper = new ObjectMapper();       
  
      RestTemplate restTemplate = new RestTemplate();
  
      String res11 = (String)jsonObj.get("response");
      String login = restTemplate.getForObject(uri, String.class);
      JSONObject obj = new JSONObject(login);
      JSONObject objects = obj.getJSONObject(res11);
  
      System.out.println("objects&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;"+objects);
  
      PatentLoginInfo patentLoginInfo = mapper.readValue(objects.toString(), PatentLoginInfo.class);
       PatentLoginInfoArray.add(patentLoginInfo);
  
      addmodel.addObject("theLogin", PatentLoginInfoArray);
  
      if (res.equals("BVE000403")) { // success
  
          addmodel = new ModelAndView("index");
          addmodel.addObject("login Unsucessfull","Password is invalid");
  
          return addmodel;
      } 
      else{
  
              addmodel = new ModelAndView("AnswerQuestion");
              addmodel.addObject("login sucessfull");
              return addmodel;
  
      }
  }
  </code></pre>