NullPointerException when using getName () function from SingularAttribute

When I call the getName method of the SingularAttribute class, I get a nullpointer exception. I've been stuck here for a couple of hours and for the life of me can't figure out why this breaks, so I decided to ask you for some thoughts on the matter. I am assuming that my annotations are wrong, or that I am not taking a step in generating the auto-generated source files.

This is an example of the call I am making:

SingularAttribute attribute = criteria.getAttribute();
if( attribute == null )
    System.out.println("Attribute is null");
else
    System.out.println(attribute.getName());

      

Here's an exception, it occurs in the call to attribute.getName ():

Exception in thread "main" java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.metamodel.proxy.AttributeProxyImpl.getName(AttributeProxyImpl.java:73)
at test.core.Test.main(Main.java:47)

      

I have autogenerated files containing metamodel data for my entities. Here is an example of my entity and my metamodel:

@Entity
@Table(name = "cards")
public class Card {
    @Column(unique = true)
    private String name;
}

@Generated(value="EclipseLink-2.6.0.v20140809-rNA", date="2015-01-07T09:16:03")
@StaticMetamodel(Card.class)
public class Card_ {
    public static volatile SingularAttribute<Card, String> name;
}

      

Here is my Criteria class:

public class Criteria<T,U extends Serializable> {
    private final SingularAttribute<? super T, U> attribute;

    private final U parameter;

    private final CriteriaOperator operator;

    Criteria(SingularAttribute<? super T, U> attribute, U parameter, CriteriaOperator operator) {
        this.attribute = attribute;
        this.parameter = parameter;
        this.operator = operator;
    }

    public static enum CriteriaOperator{
        AND,OR,NOT;
    }

    public SingularAttribute<? super T, U> getAttribute() {
        return attribute;
    }

    public U getParameter() {
        return parameter;
    }

    public CriteriaOperator getOperator() {
        return operator;
    }
}

      

Here I am creating a new instance of the criteria class

33 public static void main(String[] args) {
34  
35  Criteria<Card, String> criteria = new Criteria<Card, String>(Card_.name, "test", CriteriaOperator.AND);
36  
37  if (criteria == null) {
38      System.out.println("Criteria is null");
39  } else {
40      System.out.println(criteria.getOperator().name());
41      
42      SingularAttribute<? super Card, String> attribute = criteria.getAttribute();
43      if( attribute == null )
45          System.out.println("Attribute is null");
46      else
47          System.out.println(attribute.getName());
48      
49      System.out.println(criteria.getParameter().toString());
50  }
51  
52 }

      

Edit 1: Removed gui components to make the example more trivial, including the main function that creates the criteria class.

+3


source to share


1 answer


Apparently my DB connection needs to be open for attribute.getName () to be called for success. once i have instantiated EntityManagerFactory and EntityManager the call works. Once these two lines are removed, the call will fail.



+1


source







All Articles