Tuesday, September 11, 2007

Java NullPointerException from a NULL field in MySQL database

Problem

A java.lang.NullPointerException error occurs when the Entity Bean trying to retrieve rows from MySQL database.

2007-09-11 09:05:20,025 DEBUG
[org.jboss.ejb.plugins.cmp.jdbc.JDBCLoadEntityCommand.SMS_SymbolTrade]
Executing SQL: SELECT tradeid, symbolid, boughtdate, boughtshare, boughtprice,
solddate, soldshare, soldprice FROM sms_symboltrade WHERE (tradeid=?) OR (tradeid=?)
OR (tradeid=?) OR (tradeid)
2007-09-11 09:05:20,035 ERROR
[org.jboss.ejb.plugins.LogInterceptor] TransactionRolledbackLocalException in
method: public abstract smsj.ejb.interfaces.SMS_SymbolTradeValue
smsj.ejb.interfaces.SMS_SymbolTradeLocal.getSMS_SymbolTradeValue(),
causedBy:java.lang.NullPointerException

at smsj.ejb.bean.SMS_SymbolTradeCMP$Proxy.getSoldshare(<generated>)
at smsj.ejb.bean.SMS_SymbolTradeCMP.getSMS_SymbolTradeValue(SMS_SymbolTradeCMP.java:76)
at sun.reflect.GeneratedMethodAccessor114.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:85)
at $Proxy220.selectTradeBySymbolIdBoughtDate(Unknown Source)
at smsj.server.SMSSocketProtocol.generateXML(SMSSocketProtocol.java:423)


Environment

MySQL Table: SMS_SymbolTrade

+-------------+-------------+------+-----+---------+-------+
| Field |Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| TradeId | varchar(64) | NO | PRI | | |
| SoldDate | date | YES | | NULL | |
| SoldShare | int(7) | YES | | 0| |
| SoldPrice | float(7,2) | YES | | 0.00| |
+-------------+-------------+------+-----+---------+-------+


Data Sample:
+--------------------+----------+-----------+-----------+
| TradeId | SoldDate | SoldShare | SoldPrice |
+--------------------+----------+-----------+-----------+
| 101189456691548214 | NULL | NULL | NULL |
| 101189456691550974 | NULL
| NULL | NULL |
| 101189456691559293 | NULL
|0 | NULL |
| 101189456691563329 | NULL
|0 | NULL |
+--------------------+----------+-----------+-----------+

Entity Bean: SMS_SymbolTradeBean.java
public abstract class SMS_SymbolTradeBean{
private String tradeid; //pk
private String solddate;
private int soldshare;
private Float soldprice;
. . . . . . . .

Solution

The root cause of the NullPointerException is from the NULL value of the SoldShare field in SMS_SymbolTrade table. The data type of the SoldShare field is set to int(7), which means the SoldShare field is a primitive, not an Object. Although you can set a NULL value to the SoldShare field in MySQL database, when the Entity bean trying to read it from database, because it’s a primitive field that can’t be set to NULL in EJB, it throws the famous NullPointerException to the log.

Just simply updated all NULL value to 0 and it fixed myproblem.

Update SMS_SymbolTrade SET SoldShare = 0 WHERE SoldShare IS NULL;

No comments: