2
votes

I am just learning Java and have a question on calls from an XPage to a JavaBean.

I am working on a JavaBean I got from Declan Sciolla-Lynch's blog that works perfectly in most situations. The link points to the example code.

Essentially the JavaBean connects into the names.nsf and pulls various fields for lookup. IE User's phone or email address. Similar to an existing LotusScript class we had implemented in the past. We have a secondary Directory with additional information that I took Declan's example and modified it. It now points to a different server, nsf and has additional fields. It works under normal conditions.

However I have run into to two issues that cause the class to fail and to show an unhelpful "Can't Instantiate Class" error. The issues were with the ACL and database replication formula and will be resolved but I'd like to try to minimize environmentally caused issues. The problem is I can't trap this in EL or SSJS because it is happening at a JavaBean/class level. There is a try{} catch {} around the Java code but the "Can't Instantiate Class" error still occurs. I am attempting to load the default values of three fields on an XPage with SSJS calls to the JavaBean.

Is there a way to capture the error in the JavaBean and pass it to the XPages so that I can test for a loaded class before calling the managed bean?

1

1 Answers

2
votes

"Can't Instantiate Class" error occurs when your Java class constructor throws an error.

Add a try/catch block to your Java bean constructor and deliver the error message to your XPage in catch block:

first add an error messages control "messages1" to your XPage

<xp:messages id="messages1"></xp:messages>

and then write your error message in Java bean to this control

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

public class MyClass implements Serializable {

    public MyClass() {
        try {
            ...
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage( "messages1", 
                new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), ""));
        }