0

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.

Gerard Cruz
  • 641
  • 14
  • 34

3 Answers3

2

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

sasankad
  • 3,543
  • 3
  • 24
  • 31
  • I've tried adding in `HttpCon.setRequestMethod("HEAD");` and `HttpCon.connect();` but I'm still getting the same error... – Gerard Cruz Dec 20 '13 at 04:42
  • I'm following the guide on this stackoverflow question http://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-a-http-url-for-availability. I'm testing the code in Pinging an HTTP url and getting a response code. Still the same error. – Gerard Cruz Dec 20 '13 at 05:14
  • @Jigs, have you checked whether httpCon is not null? – sasankad Dec 20 '13 at 05:19
  • I'm not sure on how to check it though. I've tried a condition `if connection != null { "connection okay" }` which did not return anything at all. I've also tried outputting `connection.getURL().toString()` and it outputted http://www.google.com. So I guess it isn't null? – Gerard Cruz Dec 20 '13 at 05:31
  • @Jigs, is your url starts with www or http:// ? I think it should start with http:// but not sure 100% could you check that adding http:// to your url? – sasankad Dec 20 '13 at 05:39
  • Actually it is `http://www.google.com`. Forgot to comment it so it turned into a link. – Gerard Cruz Dec 20 '13 at 05:41
  • btw. the code was httpCon.getURL().toString which returned `http://www.google.com` – Gerard Cruz Dec 20 '13 at 05:43
  • Since you've updated your code, you really should update it in your question so that people can see the latest iteration of your problem. – Richard Schwartz Dec 20 '13 at 14:41
1

You may still have a code problem, but you could also have a permissions problem.

Your code is running as a Java agent on an IBM Lotus Domino server. That means it is subjected to restrictions on what it can do. There are three levels of control: restrictions imposed by Domino's AMGR task, restrictions assigned to your agent, and restrictions imposed by the JVM. Presuming that you are the signer of the agent, have you made sure that you have permission to run unrestricted agents (Security tab of the Server Document in the Domiono Directory)? And have you checked the settings on your agent (the Security tab of the Agent Properties dialog for your agent)? If you have all of those set properly, then you may have to look at the file jvm/lib/security/java.policy in your Domino server installation.

Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41
  • `...have you made sure that you have permission to run unrestricted agents `. Regarding this one. There's an original java agent that is signed by one of my team members that contains a code similar to the one I posted and I'm sure he has permission to run unrestricted agents. However the same error occurs which I'm currently investigating. `Under security tab of the agent`. The Run As Web User checkbox is ticked, disallow unrestricted actions and in default access, site admin and developer is ticked. (I am a member of the developer group/role) – Gerard Cruz Dec 20 '13 at 17:35
  • Hmmm... you wrote "disallow unrestricted", but I belive the choices are "do not allow restricted operations", "allow restricted operations" or "allow restricted operations with full access administration rights". If you want to do network access, I believe that you need to have "allow restricted opreations" or "allow restricted operations with full access...". – Richard Schwartz Dec 20 '13 at 18:49
  • Sorry I meant `Do not allow restricted operations`. I've changed it to `allow restricted operations with full access` however the error remains the same. – Gerard Cruz Dec 20 '13 at 19:46
  • Hmmm... My next suggeestion would have been the Java policy file on the server, but if another agent with similar code runs on the same server without problem, then it does not seem like that could be the problem. If the error is still pointing to System.out.println, and it is still a NullPointerException, then the only possibility is that getResponseMessage is returning null. According to the doc, getResponseCode won't ever do that, but getResponseMessage will if there is no actual message. – Richard Schwartz Dec 20 '13 at 22:34
  • So my suggestion is to break that line into two calls to println. In the first one, just print the result from getResponseCode. My guess is that it's not a normal 200 code, and whatever value you see there will give a strong hint about what you need to do next. – Richard Schwartz Dec 20 '13 at 22:36
  • Hi. I tested it on a local db in my colleague's lotus notes and it works. Here is the [screenshot](http://i.imgur.com/vAqaknQ.png). I tested it on my local db but it doesn't work... – Gerard Cruz Dec 23 '13 at 14:59
  • So the problem must lie on my machine's environment. – Gerard Cruz Dec 23 '13 at 15:11
  • My colleague has LN release 8.5.3FP5. I have 8.5.3FP4 SHF39. We have the same java version 1.6.0 – Gerard Cruz Dec 23 '13 at 15:15
  • I updated my Lotus Notes and then afterwards, it works finally. I'm not sure though if that's the solution – Gerard Cruz Dec 23 '13 at 18:36
1
  1. Close Notes/Designer
  2. Open Task Manager in Windows
  3. Find and kill all IBM Lotus Notes/Domino processes
  4. Try again

Worked for me

Winner
  • 11
  • 1