I have a problem with Hibernate 4.3.0Final that I have been unable to solve. It is puzzling because I am running similar code in several other places which is working fine. The below code is throwing a java.lang.NullPointerException. It is running in a web service in using a NetBeans setup using Glassfish and JavaDB.
HERE IS THE SOURCE THAT IS THROWING THE EXCEPTION, IT OCCURS ON OR AFTER [ item.setRoomId(-1); ]:
characterId, characterFirstName, roomId, itemName, variables are being passed in from above; I have verified via debugging that they all have values with valid datatypes; also a valid record is in the Items table.
Code:
Query itemsQuery =
em.createNamedQuery("Items.findByRoomIdByName");
itemsQuery.setParameter("roomId", roomId);
itemsQuery.setParameter("name", itemName);
Items item = null;
if (itemsQuery.getResultList().isEmpty()) {
throw new UnableToIdentifyException("Item does not exist! You cannot get an item that does not exist. Did you already pick it up?");
}
else {
item = (Items) itemsQuery.getSingleResult();
Boolean isStuck = item.getIsUnmovable();
if (isStuck) {
//Item stuck
notifyItemGetViewers(characterId, characterFirstName, roomId, itemName, isStuck);
}
else {
//Pick up item
try {
item.setRoomId(-1);
item.setCharacterId(characterId);
item.setStoreId(-1);
}
catch (Exception e) {
logger.severe("Wrapped Exection Caught: Exception: " + e.toString() + " Error Message: " + e.getMessage());
}
em.persist(item);
em.flush();
notifyItemGetViewers(characterId, characterFirstName, roomId, itemName, isStuck);
}
}
ITEMS.JAVA (ENTITY CLASS):
Code:
package org.tav.mit;
import java.beans.*;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "mit_items")
@NamedQueries({
@NamedQuery(name="Items.findByItemId",
query="SELECT i FROM Items i WHERE i.itemId = :itemId"),
@NamedQuery(name="Items.findByRoomIdByName",
query="SELECT i FROM Items i WHERE i.roomId = :roomId AND i.name = :name"),
@NamedQuery(name="Items.findByRoomId",
query="SELECT i FROM Items i WHERE i.roomId = :roomId"),
@NamedQuery(name="Items.findByCharacterId",
query="SELECT i FROM Items i WHERE i.characterId = :characterId"),
@NamedQuery(name="Items.findByStoreId",
query="SELECT i FROM Items i WHERE i.storeId = :storeId"),
@NamedQuery(name="Items.findByName",
query="SELECT i FROM Items i WHERE i.name = :name")
})
public class Items implements Serializable {
public static final String PROP_ITEMID = "itemIdProperty";
public static final String PROP_ROOMID = "roomIdProperty";
public static final String PROP_CHARACTERID = "characterIdProperty";
public static final String PROP_STOREID = "storeIdProperty";
public static final String PROP_NAME = "nameProperty";
public static final String PROP_DESCRIPTION = "descriptionProperty";
public static final String PROP_TYPE = "typeProperty";
public static final String PROP_WORTH = "worthProperty";
public static final String PROP_BODYLOCATION = "bodyLocationProperty";
public static final String PROP_ISUNMOVABLE = "isUnmovableProperty";
public static final String PROP_ACBONUS = "acBonusProperty";
public static final String PROP_USETIMEDELAY = "useTimeDelayProperty";
public static final String PROP_DAMAGEDICE = "damageDiceProperty";
public static final String PROP_DAMAGEDICESIDES = "damageDiceSidesProperty";
public static final String PROP_DAMAGETYPE = "damageTypeProperty";
public static final String PROP_LIGHTPROVIDED = "lightProvidedProperty";
@Id
@GeneratedValue
private Integer itemId;
@Column()
private Integer roomId = -1;
@Column()
private Integer characterId = -1;
@Column()
private Integer storeId = -1;
@Column(length = 128, nullable = false)
private String name;
@Column(length = 2048, nullable = true)
private String description;
@Column()
private Integer type = -1;
@Column()
private Double worth = 0.0;
@Column()
private Integer bodyLocation = -1;
@Column(nullable = false)
private Boolean isUnmovable = false;
@Column()
private Integer acBonus = -1;
@Column()
private Integer useTimeDelay = -1;
@Column()
private Integer damageDice = -1;
@Column()
private Integer damageDiceSides = -1;
@Column()
private Integer damageType = -1;
@Column(nullable = false)
private Boolean lightProvided = false;
@Column(length = 2048)
private PropertyChangeSupport propertySupport;
public Items()
{
propertySupport = new PropertyChangeSupport(this);
}
public int getItemId() {
return itemId;
}
public void setItemId(Integer itemId) {
Integer oldValue = this.itemId;
this.itemId = itemId;
propertySupport.firePropertyChange(PROP_ITEMID, oldValue, itemId);
}
public int getRoomId() {
return roomId;
}
public void setRoomId(Integer roomId) {
Integer oldValue = this.roomId;
this.roomId = roomId;
propertySupport.firePropertyChange(PROP_ROOMID, oldValue, roomId);
}
public int getCharacterId() {
return characterId;
}
public void setCharacterId(Integer characterId) {
Integer oldValue = this.characterId;
this.characterId = characterId;
propertySupport.firePropertyChange(PROP_CHARACTERID, oldValue, characterId);
}
public int getStoreId() {
return storeId;
}
public void setStoreId(Integer storeId) {
Integer oldValue = this.storeId;
this.storeId = storeId;
propertySupport.firePropertyChange(PROP_STOREID, oldValue, storeId);
}
public String getName() {
return name;
}
public void setName(String name) {
String oldValue = this.name;
this.name = name;
propertySupport.firePropertyChange(PROP_NAME, oldValue, name);
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
String oldValue = this.description;
this.description = description;
propertySupport.firePropertyChange(PROP_DESCRIPTION, oldValue, description);
}
public int getType() {
return type;
}
public void setType(Integer type) {
Integer oldValue = this.type;
this.type = type;
propertySupport.firePropertyChange(PROP_TYPE, oldValue, type);
}
public double getWorth() {
return worth;
}
public void setWorth(double worth) {
double oldValue = this.worth;
this.worth = worth;
propertySupport.firePropertyChange(PROP_WORTH, oldValue, worth);
}
public int getBodyLocation() {
return bodyLocation;
}
public void setBodyLocation(Integer bodyLocation) {
Integer oldValue = this.bodyLocation;
this.bodyLocation = bodyLocation;
propertySupport.firePropertyChange(PROP_BODYLOCATION, oldValue, bodyLocation);
}
public boolean getIsUnmovable() {
return isUnmovable;
}
public void setIsUnmovable(boolean isUnmovable) {
boolean oldValue = this.isUnmovable;
this.isUnmovable = isUnmovable;
propertySupport.firePropertyChange(PROP_ISUNMOVABLE, oldValue, isUnmovable);
}
public int getAcBonus() {
return acBonus;
}
public void setAcBonus(Integer acBonus) {
Integer oldValue = this.acBonus;
this.acBonus = acBonus;
propertySupport.firePropertyChange(PROP_ACBONUS, oldValue, acBonus);
}
public int getUseTimeDelay() {
return useTimeDelay;
}
public void setUseTimeDelay(Integer useTimeDelay) {
Integer oldValue = this.useTimeDelay;
this.useTimeDelay = useTimeDelay;
propertySupport.firePropertyChange(PROP_USETIMEDELAY, oldValue, useTimeDelay);
}
public int getDamageDice() {
return damageDice;
}
public void setDamageDice(Integer damageDice) {
Integer oldValue = this.damageDice;
this.damageDice = damageDice;
propertySupport.firePropertyChange(PROP_DAMAGEDICE, oldValue, damageDice);
}
public int getDamageDiceSides() {
return damageDiceSides;
}
public void setDamageDiceSides(Integer damageDiceSides) {
Integer oldValue = this.damageDiceSides;
this.damageDiceSides = damageDiceSides;
propertySupport.firePropertyChange(PROP_DAMAGEDICESIDES, oldValue, damageDiceSides);
}
public int getDamageType() {
return damageType;
}
public void setDamageType(Integer damageType) {
Integer oldValue = this.damageType;
this.damageType = damageType;
propertySupport.firePropertyChange(PROP_DAMAGETYPE, oldValue, damageType);
}
public boolean getLightProvided() {
return lightProvided;
}
public void setLightProvided(boolean lightProvided) {
boolean oldValue = this.lightProvided;
this.lightProvided = lightProvided;
propertySupport.firePropertyChange(PROP_LIGHTPROVIDED, oldValue, lightProvided);
}
@Override
public int hashCode() {
int hash = 0;
hash += (itemId != -1 ? itemId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Items)) {
return false;
}
Items other = (Items) object;
if ((this.itemId == -1 && other.itemId != -1) || (this.itemId != -1 &&
!((this.itemId) == (other.itemId)))) {
return false;
}
return true;
}
}
One interesting thing is that after the exception throws the database shows that the roomId has in fact been set to -1. However the other fields have not been updated. If I reorder the setFields(-1) methods the exception still trigger on the first method. Also, originally the try catch block was not there. I had to add it in order to see the root cause exception because this method has the @Transactional annotation which wraps the exception in a general RollbackException that does not show the root cause exception.