0

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();
    }
  }
}
Mohan Gangan
  • 127
  • 11
  • Try if (!cat.equals("")) to compare the value of the string rather than the object reference. – Stack Underflow Jan 29 '15 at 19:15
  • 1
    Heh. I got caught by the lack of braces around the if statements. My advice is to always use these so other coders, including yourself in the future, don't make the same mistakes. –  Jan 29 '15 at 19:17
  • I put the braces and still the same result – Mohan Gangan Jan 29 '15 at 19:30
  • I said the lack of braces made the code less readable, causing me to misunderstand the code. The problem here is actually how you are testing for equality. –  Jan 30 '15 at 20:21

3 Answers3

2

Use this for the if condition

!"".equals (cat)

Direct equal checks for reference equality, not content equality.

Inverting cat and the empty string takes care of the null condition without any crutches since the empty string is never null.

Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33
0

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)

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
0

It worked as if (!cat.isEmpty())! Even without braces!

jrtc27
  • 8,496
  • 3
  • 36
  • 68
Mohan Gangan
  • 127
  • 11
  • Use Lev's version. You can use string.isEmpty() only if you are 100% sure that string is not null. Otherwise it will throw a NullPointerException if the string is null. You can't be sure with db.getCategories() as you are not the developer and don't know how this method handles no categories - now and in future versions. With `if (!"".equals(cat))` you are safe. – Knut Herrmann Jan 29 '15 at 19:45
  • Of course it does work without braces because you have only one statement between If and else. But, Java coding conventions are telling that you should always use braces as code gets this way more robust. – Knut Herrmann Jan 29 '15 at 19:49
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Sled Jan 29 '15 at 20:23
  • @ArtB: Oh, it does provide an answer. – Deduplicator Jan 29 '15 at 20:34
  • You should report the documentation error to IBM/Lotus. There's probably a link to do that right on the documentation page. – Richard Schwartz Jan 29 '15 at 21:36
  • The solution did not worked for getting and checking template name. – Mohan Gangan Jan 29 '15 at 21:45
  • With db.getCategories() you get "the categories under which a database appears in the Database Library", not the template name. – Knut Herrmann Jan 30 '15 at 08:11