Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null results

 
Jay X Brown
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
strange things are happening in my program when i get a 'null' value for a particular column/row in my resultset.
on the one hand the value seems to be a string with the 4 letters 'n', 'u', 'l' and 'l'. on the other hand, if i try to 'trim' it, it gives me a fatal exception.
what's going on?
 
Michal Harezlak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay,
They are not really of a String class they are just null. When you print them out you will see null, but there is no way to do any method calls on them.

 
Jay X Brown
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so how do i query them? basically, i don't want to print anything if the row is null. right now, it's throwing an exception when i try to compare it as a string.
thanks
jay
 
Michal Harezlak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's happening because there is nothing (no object) to compare it with: null. You might want to check for null if (whatever!=null).
 
Jay X Brown
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the last one did it. thanks michael!
i never realized it is so simple to check for null values.
i LOVE this board.
jay
 
Michal Harezlak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad that could help.
 
sachin sachdeva
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whenever u are writing ur query write nvl method in that and replace null with any other value like space also.
Otherwise it will give you null pointer execption
Example
select nvl(empno,'') from emp
 
Michal Harezlak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately that will not always work, for numeric values it will raise an SQLException. What's more I'm not sure if that is a part of 92 standard, is it? I have seen that approach in the PL/SQL.
 
Jay X Brown
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure i understand sachin's suggestion. no reference to nvl on the java site.
??
jb
 
Michal Harezlak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin is refering to the SQL syntax.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic