• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException in Jtable  RSS feed

 
Ubaldo Robledo
Greenhorn
Posts: 22
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hola, i have a problem, im trying to get some values from a jtable (tabla) and insert them in a mysql database,
so i scan the table for some values to know which of the rows i must insert ("s" or "n").

I'm able to insert few rows, but when the last row with "s" or "n" is inserted it launch me a NullPointerException and I dont know why.

can you help me on this one, thank you.

(sorry for my bad english)


 
Devaka Cooray
Marshal
Posts: 5650
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to figure out the cause just by looking at your code. As you catch the NullPointerException and just show your own message, you are hiding the exception's stack trace - don't do that. If you use a logger, log the caught exception. If not, do an ex.printStrackTrace() to be able to quickly figure out which line throws the exception.

Also, never compare Strings as in check=="1" - always use equals(-) method. you should also consider the readability of your code. Your code has two variables declared as 'check' and 'chck', which makes it hard to read.
 
Ubaldo Robledo
Greenhorn
Posts: 22
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:It's hard to figure out the cause just by looking at your code. As you catch the NullPointerException and just show your own message, you are hiding the exception's stack trace - don't do that. If you use a logger, log the caught exception. If not, do an ex.printStrackTrace() to be able to quickly figure out which line throws the exception.

Also, never compare Strings as in check=="1" - always use equals(-) method. You should also consider the readability of your code. Your code has two variables declared as 'check' and 'chck', which makes it hard to read.




Yes actually, i modify some values, thats why i have check and chck, 'cause im trying to get the NullpointerException out of my code.
the exception is on the comparation

if (chck.equals("s")||chck.equals("n"))

when the value that i get from the table is null (doesnt have "s" or "n" or Anything else) i get the message, actually i removed the

}catch(NullPointerException ex){

to know where the error ocurred, i understand that when is null doesnt enter in my if, but i dont understand why sends me a NullPointerException

i have used
check.equals(null);
check.equals("");
check.isEmpty();

and in all of this methods sends me the exception,

i hope this help to you to help me hehe, thank you!
 
Ubaldo Robledo
Greenhorn
Posts: 22
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey!

i solved this, i just need to compare if the check value vas null, is use
check==null; instead of check.equals(null);

i think that is why check==null; its not a operation and only check on memory if the space is the same, or some thing like that.
and check.equals(null); checks the value not the space. well i think so.

thanks Devaka Cooray for answer and Paul Clapham for editing my post.

if something is wrong could you make me know?

thanks again!
 
Devaka Cooray
Marshal
Posts: 5650
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If 'check' is null, attempting to invoke any method 'on' that variable throws NullPointerException. Pre-checking for null is a solution. In shorthand way, you can change the direction and check it as in "s".equals(check) - this will never throw NullPointerException, because you are calling the method on a known String value, which is not null.
 
Ubaldo Robledo
Greenhorn
Posts: 22
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:If 'check' is null, attempting to invoke any method 'on' that variable throws NullPointerException. Pre-checking for null is a solution. In shorthand way, you can change the direction and check it as in "s".equals(check) - this will never throw NullPointerException, because you are calling the method on a known String value, which is not null.



thank you,

you were so kind. greetings
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!