Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Map<Integer, BigDecimal> causes NPE because it is backed by a Hashtable(Problems mapping possible (and valid) null values)
Map<Integer, BigDecimal> causes NPE because it is backed by a Hashtable [message #1058113] Sat, 11 May 2013 15:13 Go to next message
Edson Richter is currently offline Edson RichterFriend
Messages: 3
Registered: July 2009
Junior Member
I've created the following code using EclipseLink 2.4.1:

   private Map<Integer, BigDecimal> fatorWebService = new HashMap<Integer, BigDecimal>();

  @ElementCollection(fetch = FetchType.EAGER)
  @CollectionTable(name = "tabelafrete_nattransp", joinColumns = @JoinColumn(name = "tabelafrete_id"))
  @MapKeyColumn(name = "naturezatransporte_id")
  @Column(name = "fator_ws", nullable = true)
  public Map<Integer, BigDecimal> getFatorWebService() {
    return this.fatorWebService;
  }

  public void setFatorWebService(Map<Integer, BigDecimal> fatorWs) {
    this.fatorWebService = fatorWs;
  }


Problem is when I run the application I get an NullPointerException. The cause for the problem is that EclipseLink is using a Hashtable (instead a HashMap) to back my collection.

Can anyone tell me how to force EclipseLink to use a HashMap instead a Hashtable?


The exception follows:

[EL Warning]: 2013-05-11 11:55:56.279--ServerSession(278404637)--Thread(Thread[http-nio-8088-exec-54,5,main])--java.lang.NullPointerException
	at java.util.Hashtable.put(Hashtable.java:396)
	at org.eclipse.persistence.internal.queries.MapContainerPolicy.addInto(MapContainerPolicy.java:135)
	at org.eclipse.persistence.internal.queries.MappedKeyMapContainerPolicy.addInto(MappedKeyMapContainerPolicy.java:195)
	at org.eclipse.persistence.queries.DataReadQuery.executeNonCursor(DataReadQuery.java:226)
	at org.eclipse.persistence.queries.DataReadQuery.executeDatabaseQuery(DataReadQuery.java:152)
	at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:852)
	at org.eclipse.persistence.queries.DataReadQuery.execute(DataReadQuery.java:137)
	at org.eclipse.persistence.internal.sessions.AbstractSession.internalExecuteQuery(AbstractSession.java:2946)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1602)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1584)
	at org.eclipse.persistence.internal.indirection.NoIndirectionPolicy.valueFromQuery(NoIndirectionPolicy.java:323)
	at org.eclipse.persistence.mappings.DirectCollectionMapping.valueFromRow(DirectCollectionMapping.java:3154)
	at org.eclipse.persistence.mappings.ForeignReferenceMapping.readFromRowIntoObject(ForeignReferenceMapping.java:1369)
	at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildAttributesIntoObject(ObjectBuilder.java:448)
	at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:803)
	at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildWorkingCopyCloneNormally(ObjectBuilder.java:719)
	at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObjectInUnitOfWork(ObjectBuilder.java:672)
	at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:605)
	at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:564)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:777)
	at org.eclipse.persistence.queries.ReadObjectQuery.registerResultInUnitOfWork(ReadObjectQuery.java:787)
	at org.eclipse.persistence.queries.ReadObjectQuery.executeObjectLevelReadQuery(ReadObjectQuery.java:460)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1150)
	at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:852)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1109)
	at org.eclipse.persistence.queries.ReadObjectQuery.execute(ReadObjectQuery.java:421)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1197)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2875)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1602)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1584)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1549)
	at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:231)
	at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:403)
Re: Map<Integer, BigDecimal> causes NPE because it is backed by a Hashtable [message #1058610 is a reply to message #1058113] Tue, 14 May 2013 14:39 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You might want to consider defines an Entity class for the table instead of using a Map.

EclipseLink defaults to using Hashtable for Maps (when LAZY a special LAZY Hashtable subclass is used).

If you make the mapping EAGER you can force it to use a specific implementation class. (either by declaring the variable type as HashMap, or using a DescriptorCustomizer to set the mapping's container policy (mapping.useMapClass(HashMap.class)).

However, I'm not certain if EclipseLink's MapKeyColumn support will work correctly for null keys.

Please log a bug that Hashtable is used by default, EclipseLink should at least provide an option to use HashMap by default, including supporting LAZY.




James : Wiki : Book : Blog : Twitter
Previous Topic:how to return value in anonymous pl sql using eclipselink
Next Topic:[Moxy] Simple unmarshalling and marshalling changes xml
Goto Forum:
  


Current Time: Thu Nov 27 09:57:18 GMT 2014

Powered by FUDForum. Page generated in 0.14430 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software