Static trailing string field is NULL at run time with only a specific value

I am creating a Java EE web project using Java 7 and GlassFish 4.1.1. My IDE is NetBeans.

When refactoring some code from the repository class, I replaced the "USERS" line with a static final field in my model class. Immediately after that (clean and build), I got an IllegalArgumentException error from my service and it turned out that the field reads as null.

Model class:

public class User {

    public static final String TABLE_NAME = "USERS";
    public static final String PRIMARY_KEY = "ID";

    private String id;    
    private String name;

    public User(String id, String name) {
        this.id = id;
        this.name = name;
    }

}

User repository until:

public List<User> get() throws RepositoryException
{
    try {
        return service.retrieve("USERS");
    } catch (SQLException ex) {
        throw new RepositoryException("Retrieval failed.", ex);
    }
}

User repository after:

public List<User> get() throws RepositoryException
{
    try {
        return service.retrieve(User.TABLE_NAME);
    } catch (SQLException ex) {
        throw new RepositoryException("Retrieval failed.", ex);
    }
}

Perhaps the strangest thing about this (but perhaps quite revealing?) Is that if I change the value of the field, the problem will disappear.

// This works!
public static final String TABLE_NAME = "USERSS";

Of course, the value of TABLE_NAME is now incorrect.

Here is what I tried:

  • ( )
  • GlassFish,
  • NetBeans
  • Google

!:)

+4

Source: https://habr.com/ru/post/1690015/


All Articles