Skip to content
Advertisement

How to prevent NullPointerException in the finally clause in a Java Class in Domino agent

Java debugging information only returns null without stating the actual cause of the error in the finally clause of the try/catch and not sure on how to get rid of this error message or the cause of it which is related to the recycle method called.

Below is the error log:

Agent Manager: Agent printing: Create Report final insert error:  null
Agent Manager: Agent  error: java.lang.NullPointerException
Agent Manager: Agent  error:  at JavaAgent.createReport(Unknown Source)
Agent Manager: Agent  error:  at JavaAgent.NotesMain(Unknown Source)
Agent Manager: Agent  error:  at lotus.domino.AgentBase.runNotes(Unknown Source)
Agent Manager: Agent  error:  at lotus.domino.NotesThread.run(Unknown Source)

Below are the functions being called

        private void createReport() {
        Integer qCount = 0;
        Document qDoc = null;
        ViewNavigator vNav = null;

        try {
            qView = currentDatabase.getView("RecsByRNo");
            qDoc = qView.getFirstDocument();
            vNav = qView.createViewNav();

            while (qDoc != null) {

                String qNo = qDoc.getItemValueString("RNo");
                String qType = qDoc.getItemValueString("EType");
                String qStatus = qDoc.getItemValueString("Status");
                String qBy = qDoc.getItemValueString("RBy");

                Document tmpReqDoc = qView.getNextDocument(qDoc);
                qDoc.recycle();
                qDoc = tmpReqDoc;
            }

        } catch (Exception e) {
            logErrors("Create Report error: ", e);

        } finally {
            try {
                qView.recycle();
                qDoc.recycle();
                vNav.recycle();
            } catch (Exception e) {
               logErrors("Create Report final insert error: ", e);
            }
        }

        agentLogger.LogAction("<<< Count: " + qCount + " >>>");
    }

    private void logErrors(String t, Exception e) {
        agentLogger.LogError(t + " " + e.getMessage());
        agentLogger.LogError(t + " " + e.getStackTrace().toString());
        System.out.println(t + " " + e.getMessage());
        e.printStackTrace();
    }

Any recommendation will be appreciated.

Advertisement

Answer

The order of recycle() should be updated in the reverse order of creation. The qDoc and vNav objects has been created from qView object. If recycle on qView is called first then other objects become null as well. And it’s always good practice to check for null before closing object in the finally block.

finally {
            try {
                if(vNav != null) vNav.recycle();
                if(qDoc != null) qDoc.recycle();
                if(qView != null) qView.recycle();
            } catch (Exception e) {
               logErrors("Create Report final insert error: ", e);
            }
        }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement