API Design: Defensive and Explanatory Error Message

Leave a comment

August 20, 2012 by huionn

java.lang.IllegalStateException: Error in org.openide.nodes.EntrySupportDefault with entry Children.Keys.KE[com.caseforge.task.model.TaskModel@d1cb41f7,0] from among [Children.Keys.KE[com.caseforge.task.model.TaskModel@d1cb41f7,0], Children.Array.AE[]] in {Children.Array.AE[]=Children.Info[Children.Array.AE[],length=0], Children.Keys.KE[com.caseforge.task.model.TaskModel@d1cb41f7,0]= Children.Info[Children.Keys.KE[com.caseforge.task.model.TaskModel@d1cb41f7,0],length=1]} probably caused by faulty key implementation. The key hashCode() and equals() methods must behave as for an IMMUTABLE object and the hashCode() must return the same value for equals() keys.
    at org.openide.nodes.EntrySupportDefault.checkInfo(EntrySupportDefault.java:294)
    at org.openide.nodes.EntrySupportDefault.updateOrder(EntrySupportDefault.java:339)
    at org.openide.nodes.EntrySupportDefault.setEntries(EntrySupportDefault.java:285)

A good API should do defensive checking and provide error message that explains the problem. Better, it should provide the possible root cause of it. As shown in the exception stack trace above, the NetBeans developers really care about its users.

Btw, my mistake:

    public int hashCode() {
        return task.hashCode();

    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        if (!(obj instanceof TaskModel)) {
            return false;
        final TaskModel other = (TaskModel) obj;
        return task == other.task;

The hashCode is not constant as it changes when task is modified. The correct way should be return System.identityHashCode(task);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: