Search code examples
javalotus-domino

Domino java example code does not work properly


Here is a code from the Domino 8 designer help to get database categories.

The condition "if(cat != "") always return true though database category is empty or non-empty. What is catch?

import lotus.domino.*;
public class JavaAgent extends AgentBase {
  public void NotesMain() {
    try {
      Session session = getSession();
      AgentContext agentContext = 
          session.getAgentContext();
      // (Your code goes here) 
      Database db = agentContext.getCurrentDatabase();
      String title = db.getTitle();
      String cat = db.getCategories();
      if (cat != "")//This condition does not work
        System.out.println("Database \"" +
        title + "\" has the categories: " + cat);
      else
        System.out.println("Database \"" +
        title + "\" has no categories");
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

Solution

  • I like to use Google's Guava for these kinds of things especially when dealing with String

    In Guava there exist a Class Strings which provide

    public static boolean isNullOrEmpty(@Nullable
                        String string)
    
    Returns true if the given string is null or is the empty string. 
    

    so use Strings.isNullOrEmpty(cat)