Java exception | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Java exception

I Wrote a GUI panel which has got a if sentence: if(A_LIST.isEmpty()) {do something}, but it keeps showing exception in thread "AWT-EventQueue-0" NullPointerException, and I also tried .equals(null) and contains(null), they all shows the exception, anyone can tell me why?? Thanks!

21st May 2020, 4:51 AM
3 Answers
+ 2
NullPointerException can be caused if no references exist to an object and thus it was garbage collected by the JVM. For example it can happen if you don't assign your GUI component to a variable. This is just speculation though, it is really hard to guess without actually seeing your code :)
21st May 2020, 6:20 AM
Tibor Santa
Tibor Santa - avatar
Can't remember java graphucs all that well, but in the error stack, check which line it says the error came from, and an NPE means that a null object was passed, no matter what you've done. You should try different cases and check which ones pass a null object and which don't, but note that the error is by your side.
21st May 2020, 5:20 AM
coddy - avatar
Coder's Crux so does that mean no matter what condition l've got, I can't pass in a null element what so ever?
21st May 2020, 5:45 AM