4

When I call the getName method of the SingularAttribute class I get a nullpointerexception. I've been stuck here for a couple of hours and for the life of me I can't figure out why this is breaking so I decided to ask ya'll for some thoughts on this issue. I assume somewhere my annotations are incorrect, or that I'm missing a step when creating my auto generated-source files.

This is an example of the call I'm making:

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

Here is the exception, it occurs on the attribute.getName() call:

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 auto generated-source files that contain metamodel data of 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 is where I create 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 the gui components to make the example more trivial, included the main function that instantiates the criteria class.

Calum
  • 61
  • 5
  • It seems the error is reported from the application's event thread (it's a GUI application, apparently). Are you accessing the same criteria object from the main thread of the application or any other thread? Can you give us any code that accesses the criteria object? (Add it to the question, please) – RealSkeptic Jan 07 '15 at 16:16
  • Inside Main class what line of code is there at line number 47..?? – Abhishek Mishra Jan 07 '15 at 17:22
  • It's getting thrown at `attribute.getName()`, which makes me think `attribute` is null. But you perform a null-check, so I'm not sure why that statement would get executed if it's null. Have you tried stepping through this with a debugger? And I highly suggest using braces for all your if statements, even if they only contain a single statement – Vince Jan 07 '15 at 19:09

1 Answers1

2

Apparently a connection to my DB needs to be open for the attribute.getName() call to succeed. as soon as I created an instance of the EntityManagerFactory and EntityManager the call works. As soon as those two lines are removed the call fails.

Calum
  • 61
  • 5