Could someone explain why my code is getting an error NullPointerException? (new to java, excuse the daftness of the question)

Morning all,

I wrote an agent last week that is supposed to go through my database directory catalog, find the e-mail dbs and calculate how much space would be saved by running automated archiving from a specific date. I ended up finding out that the reason it didn’t work was because it was caught in an infinite loop. I fixed that, and now have a new issue…It gets so far through the script and then throws and error and quits. Here are the error messages:

11/05/2006 08:11:10 PM AMgr: Agent (‘HD Calculator’ in ‘mail\bfundak.nsf’) printing: Database accessed: hmatzkow.nsf

11/05/2006 08:11:11 PM AMgr: Agent (‘HD Calculator’ in ‘mail\bfundak.nsf’) error message: java.lang.NullPointerException

11/05/2006 08:11:11 PM AMgr: Agent (‘HD Calculator’ in ‘mail\bfundak.nsf’) error message: at HDCalcAgent.NotesMain(HDCalcAgent.java:36)

11/05/2006 08:11:11 PM AMgr: Agent (‘HD Calculator’ in ‘mail\bfundak.nsf’) error message: at lotus.domino.AgentBase.runNotes(Unknown Source)

11/05/2006 08:11:11 PM AMgr: Agent (‘HD Calculator’ in ‘mail\bfundak.nsf’) error message: at lotus.domino.NotesThread.run(NotesThread.java:218)

The first line is a println in my code so I know where it is blowing up.

Does anyone know what might cause a nullPointerException? I have included my code below.

any insight as to how to deal with it or the potential causes would be appreciated. I thought the try…catch statement i added where I attempt to open the DB would make a difference, but I don’t know why this error is occuring.

thanks.

brandt

the code:

import lotus.domino.*;

import java.util.*;

public class HDCalcAgent extends AgentBase {

public void NotesMain(){		

	

	try {

		Session s = getSession();

		AgentContext ac = s.getAgentContext();

		System.out.println("HDCalc Java agent has started");

		long keepCount = 0;

		long removeCount = 0;

		DateTime dt = s.createDateTime("Today");

		dt.adjustDay(-549);

		DbDirectory dbDir = s.getDbDirectory(null);

		Database db = dbDir.getFirstDatabase(dbDir.DATABASE);

		Database nextdb = null;

		while (db != null) {

			String filePath = db.getFilePath();

			int findString = filePath.lastIndexOf(Consts.MAIL);

			System.out.println("About to test for DB access");

			System.out.println("Database accessed: " + db.getFileName());

			if (findString != -1) {

				try {

					db.open();

				} catch (Exception e){

					System.out.println("You do not have access to " + db.getFileName());

				}

				if (db.isOpen()){

					DocumentCollection dc = db.getAllDocuments();

					Document doc = dc.getFirstDocument();

					Document nextdoc = null;

					while (doc != null){

						DateTime cdt = doc.getCreated();

						int timeDiff = cdt.timeDifference(dt);

						if (timeDiff < 0){

							removeCount = removeCount + doc.getSize();

						} else {

							keepCount = keepCount + doc.getSize();

						}

						nextdoc = dc.getNextDocument();

						doc.recycle();

						doc = nextdoc;

					}

				}

			}

			nextdb = dbDir.getNextDatabase();

			db.recycle();

			db = nextdb;

		}

		Database basedb = ac.getCurrentDatabase();<--it never gets to this point.

		System.out.println("Calling DB accessed");

		long keepCalc = Math.round(keepCount/Consts.MB);

		long remCalc = Math.round(removeCount/Consts.MB);

		String keepSpace = "" + keepCalc;

		String remSpace = "" + remCalc;

		Document memo = basedb.createDocument();

		System.out.println("Memo has been created.");

		memo.replaceItemValue(Fields.FORM,FValues.MEMO);

		memo.replaceItemValue(Fields.PRINCIPAL,s.getServerName());

		memo.replaceItemValue(Fields.SENDTO,SendTo.RECIPIENTS);

		memo.replaceItemValue(Fields.SUBJECT,FValues.SUBJECT);

		System.out.println("Fields have been created.");

		RichTextItem rtItem = memo.createRichTextItem(Fields.BODY);

		rtItem.appendText(Body.TEXT1);

		rtItem.appendText(dt.getDateOnly());

		rtItem.appendText(Body.TEXT2);

		rtItem.addNewLine(2);

		rtItem.appendText(Body.TEXT3);

		rtItem.appendText(keepSpace);

		System.out.println("HD Space maintained: " + keepSpace + " MB");

		rtItem.addNewLine(2);

		rtItem.appendText(Body.TEXT4);

		rtItem.appendText(remSpace);

		System.out.println("HD Space reclaimed: " + remSpace + " MB");

		System.out.println("Memo about to be sent.");

		memo.send(false);

		System.out.println("HDCalc agent finished.");

	} catch (Exception e) {

		e.printStackTrace();

	}

}

}

final class Consts {

public static final String MAIL = "mail\\";

public static final int MB = 1048576;

}

final class Fields {

public static final String FORM = "Form";

public static final String PRINCIPAL = "Principal";

public static final String SENDTO = "SendTo";

public static final String SUBJECT = "Subject";

public static final String BODY = "Body";

}

final class FValues {

public static final String MEMO = "Memo";

public static final String SUBJECT = "Notes Archiving Hard Drive Calculations";

}

final class Body {

public static final String TEXT1 = "The following calculations have been made to determine the amount of space needed if automated archiving was institued to archive all documents received prior to ";

public static final String TEXT2 = ". All values are in megabytes.";

public static final String TEXT3 = "Amount of space still necessary for non-archived documents: ";

public static final String TEXT4 = "Amount of space recovered by potential archive process: ";

}

final class SendTo {

private static final String[] PRIVATE_RECIPIENTS = {"user1","user2"};

private static final List RECIPIENT_LIST = Collections.unmodifiableList(Arrays.asList(PRIVATE_RECIPIENTS));

public static final Vector RECIPIENTS = new Vector(RECIPIENT_LIST);

}

Subject: Could someone explain why my code is getting an error NullPointerException? (new to java, excuse the daftness of the question)

Which is line 36 of the attached code?

Subject: Could someone explain why my code is getting an error NullPointerException? (new to java, excuse the daftness of the question)

according to the Notes IDE, it’s this line:

int timeDiff = cdt.timeDifference(dt);

here’s a question…the DB in question is currently hidden from everyone, except the admins. This includes the server. Could that make a difference? cdt is set by getting the doc.GetCreated() method…if the db is hidden, perhaps the agent can’t see the doc object?

brandt

Subject: RE: Could someone explain why my code is getting an error NullPointerException? (new to java, excuse the daftness of the question)

Could the error be correct, and dt be null?