I found many question about this subject - but don't found one which is the same to me.
I work in Java 8 Project. I get Object[] instance arraive from sql-query, something like:
"SELECT user.name, user.age, user.client_type, user.shopping_card FROM user_account user"
(I use sqlyog)
Sometime user.client_type field or user.shopping_card return null. When I want to convert this Object[] instance to Java class I get exception:
String userName= (String) obj[1];
int age= (Integer) obj[2];
int type= obj[3]== null? null: (Integer) obj[3];
int card= obj[4]== null? null: (Integer) obj[4] ;
return new UserData(userName, age, type, card);
When type is null / card is null I get an exception when debug the type/card casting row:
java.lang.NullPointerException
"SELECT user.name, user.age,
CASE WHEN user.client_type IS NOT NULL THEN user.client_type ELSE NULL END as
client_type, CASE WHEN user.shopping_card IS NOT NULL THEN
user.shopping_card ELSE NULL END as shopping_card FROM user_account user"
What should I do? I must say that when all data is not null - all works well!