+ 2

Why Java throws nullpointerexception?

When i insert values with sql API and handle exceptions and do it again to make sure the "exceptionAlreadyExists" works Eclipse throws me a NullPointerException :( sorry for my english help me please

30th May 2020, 8:30 PM
Gabriel Ferreyra
Gabriel Ferreyra - avatar
7 Respostas
+ 6
Well, in the example code you're only catching the PrAlreadyExistException not the NullpointerException, you could try to catch Exception instead.
30th May 2020, 9:40 PM
Tashi N
Tashi N - avatar
+ 5
Sounds like an interesting question but it is impossible to answer without an example code. Would you mind publishing a minimum working example that shows the error on the code playground and link it here?
30th May 2020, 8:43 PM
Tashi N
Tashi N - avatar
+ 5
Hey, no worries. What persistence api do you use? I guess we need to dive deeper into your insert method to understand what's going on. And I'm not sure what the SQL api is. Looks like the insert returns null because the record could not be created (it does already exist on the second attempt), not sure why the npe wasn't caught though..
30th May 2020, 9:12 PM
Tashi N
Tashi N - avatar
+ 2
Maybe it's a sql problem, sorry i'm a begginer don't judge me This is only the Main class. i already builded the connection with sql and created the classes. also import them. https://code.sololearn.com/c8SfFj22G98E/?ref=app
30th May 2020, 8:59 PM
Gabriel Ferreyra
Gabriel Ferreyra - avatar
+ 2
Full stacktrace would be more helpful... And yes, catch everything what could be thrown...
31st May 2020, 1:05 AM
Sandra Meyer
Sandra Meyer - avatar
+ 2
I solved it! The problem was that i put the boolean if(to prove that already exist) of my created exception in the try/catch and it has to be outside.😅 Before the block. This was my first problem that i shared with the community. Thank you so much🥰
31st May 2020, 1:43 AM
Gabriel Ferreyra
Gabriel Ferreyra - avatar
+ 1
Exactly it's something like that. I use the MySQL JDBC. I really don't know why my created exception can't catch it. It's too much code to write here but thanks anyway for your help.
30th May 2020, 9:32 PM
Gabriel Ferreyra
Gabriel Ferreyra - avatar