Java NullPointerException in Java Agent

Question

I'm developing a java agent. I have an NullPointerException error which I do believe should not happen.

Here is the debug console message:

java.lang.NullPointerException
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:719)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:646)
    at COM.ibm.JEmpower.applet.http.HttpURLConnection.getInputStream(HttpURLConnection.java:411)
    at COM.ibm.JEmpower.applet.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:703)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:399)
    at JavaAgent.NotesMain(JavaAgent.java:16)
    at lotus.domino.AgentBase.runNotes(Unknown Source)
    at lotus.domino.NotesThread.run(Unknown Source)

This the code in the java agent

import lotus.domino.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
public class JavaAgent extends AgentBase {

    public void NotesMain() {
      String strAux = "[A Working URL]";
      try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();

          // (Your code goes here)

          HttpURLConnection httpCon = (HttpURLConnection) new URL(strAux).openConnection();

          httpCon.setRequestMethod("HEAD");
          httpCon.setConnectTimeout(20000);  
          httpCon.setReadTimeout(20000);

          httpCon.connect();

          System.out.println(HttpCon.getURL().toString());

          int responsecode = httpCon.getResponseCode();
          System.out.println("Response code is "   responsecode   " - "   httpCon.getResponseMessage());
      } catch(Exception e) {
          e.printStackTrace();
      }
    }

}

Basically the error points to System.out.println("Response code is " httpCon.getResponseCode() " - " httpCon.getResponseMessage());.

The thing is that the URL is a working link since I've tried it by accessing in a browser.

What could be the possible reasons for this error? It doesn't work as well on a local notes db that I created for testing. However, in a normal java program not developed in notes, it works.

Solution

It's pointless getting the response code before you've sent anything. You're supposed to send the request first, then get the response code, then get the response if required