Search code examples
guava

toStringHelper() is undefined for the type Objects


I am using Guava. i get a "the method toStringHelper() is undefined for the type Objects error" in eclipse. i was working with no errors when suddenly these errors started coming.

@Override
public String toString()
{
    return Objects
        .toStringHelper(this)
        .add("id", this.id)
        .add("custid", this.custid)
        .toString();
}

Solution

  • I would check that you don't have Google Collections (the predecessor to Guava) on your classpath anywhere, e.g. google-collect-1.0.jar. It has a version of the Objects class that doesn't have the toStringHelper methods.

    Another weird possibility is that you have a generic type Objects defined somewhere in the same context, e.g.:

    import com.google.common.base.MoreObjects;
    
    /**
     * In this class, "Objects" is a generic type parameter like "T",
     * just more confusing.
     */
    public class Foo<Objects> {
    
      public String toString() {
        return MoreObjects.toStringHelper(this)
            ...
      }
    }
    

    Seems unlikely, but.... Guava docs