I'm making a POST call from within Java, and I'm not sure why it's not going through. The code is as follows:
String screencastStartURL = "......";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("stateID", s_id));
nameValuePairs.add(new BasicNameValuePair("startTime", screencastStartTime));
nameValuePairs.add(new BasicNameValuePair("identifier", screencastId));
System.out.println("about to make postCall");
String postResponse = Commons.postCall(screencastStartURL, nameValuePairs);
out.println(postResponse); // out is an earlier instantiated PrintWriter
The postCall method is as follows:
public static String postCall(String url, List<NameValuePair> nameValuePairs) {
System.out.println("Within postCall method");
String ans = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
System.out.println("Within postCall method 2");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
ans = (EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
return ans;
}
When I deploy this code on my Tomcat Server, it prints about to make postCall in the terminal, which means it successfully executes till before the POST call is made. But then, it doesn't print Within postCall method which means something goes wrong in the call itself?
The error message that I see is a NullPointerException which causes a ServletException
Edit
However when I make the POST call using the POSTMAN Rest Client Chrome App, it goes through successfully.
Second Edit
The stack trace that I see in my browser is
exception
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.ExceptionInInitializerError
com.bl.apps.MemoLocalRecorder.App.doGet(App.java:121)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
java.io.Reader.<init>(Reader.java:78)
java.io.InputStreamReader.<init>(InputStreamReader.java:72)
com.bl.util.Commons.setURL(Commons.java:221)
com.bl.util.Commons.<clinit>(Commons.java:150)
com.bl.apps.MemoLocalRecorder.App.doGet(App.java:121)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
As per the stack trace you posted. the error is basically because of any exception in static variable initialization in static block
below is some info and some links to give more light on this
http://examples.javacodegeeks.com/java-basics/exceptions/java-lang-exceptionininitializererror-how-to-handle-exception-initializer-error/
http://craftingjava.blogspot.in/2012/06/javalangexceptionininitializererror.html