How to avoid NulPointerException when performing string comparison of String in Java
python
String s = null;
if("hello".equals(s)) {
System.out.println("Hello!");
}
NullPonterException is squeezed as shown in "** When judging a character string by equals **" There is also an opinion that it is not good.
Personally, I've written code so far, and I've never had to handle it as an exception when s = null, so I think this is a good way to write it. ~~ (Although exception handling is sparse because I am a student and have no practical experience)
We received various comments and reminded us of the importance of null checking. When I compare strings with String in the future, I will do something like s.equals ("hello") and do a null check.
Finally, we would like to thank everyone for their valuable comments.