0

I am trying to do an httpPost into a webservice. I want to send a list as an array. Here is what I 've done so far.

public Response searchMessages(@QueryParam("tags") List<String> tags, @QueryParam("senders") List<String> senders, @QueryParam("api_keys") List<String> apiKeys) throws Exception

{Iterator<String> tagsIt = tags.iterator();
        Iterator<String> sendersIt = senders.iterator();
        Iterator<String> apiKeysIt = apiKeys.iterator();
CloseableHttpClient httpClient  = null;
        HttpPost httpPost =null;
        List<NameValuePair> nvps=null;
        CloseableHttpResponse response=null;
        InputStream  in=null;
        String myResponse = "";
        try
        {
            httpClient  = HttpClients.createDefault();
            httpPost = new HttpPost(url);

            while(tagsIt.hasNext())
            {
                String keyIt = tagsIt.next();
                nvps.add(new BasicNameValuePair("tags",hashMap.get(keyIt)));
            }
            while(sendersIt.hasNext())
            {
                String keySend = sendersIt.next();
                nvps.add(new BasicNameValuePair("senders",hashMap.get(keySend)));
            }
            while(apiKeysIt.hasNext())
            {
                String keySend = sendersIt.next();
                nvps.add(new BasicNameValuePair("apiKeys",hashMap.get(keySend)));
            }   
                //System.out.println(key+" "+hashMap.get(key));



            httpPost.setEntity(new UrlEncodedFormEntity(nvps,Consts.UTF_8));
            response = httpClient.execute(httpPost);

            BufferedReader buffer=null;
            try{


                //System.out.println(response.toString());
                in= response.getEntity().getContent();
                buffer = new BufferedReader(new InputStreamReader(in));
                  String s = "";
                  while ((s = buffer.readLine()) != null) {
                    myResponse += s+"\n";
                  }
                  status= response.getStatusLine().getStatusCode();
                  resp = Response.status(status).entity(myResponse).build();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        finally
        {
            in.close();
            response.close();
            httpClient.close();

        }

My issue is that I get a null pointer exception in in.close() line. I do not know where my mistake is.Please help.

user3485417
  • 583
  • 2
  • 7
  • 21
  • Have you tried using a debugger to see where the value of ```in``` is set and that it is set before the ```in.close()```? – Brian Topping May 04 '15 at 15:05
  • Did you try adding a null check before closing? It is always better to do null check in such conditions. If you are using java-8, I suggest you to use Try block variables for stream inistialisations. Refer http://stackoverflow.com/questions/2225221/closing-database-connections-in-java/16331963#16331963 – Yadu Krishnan May 04 '15 at 15:18
  • I've done debugging and figured out that I was sending wrong type of entity to the web service...... :-) – user3485417 May 06 '15 at 07:03

0 Answers0