Java Rule ‘Null pointers should not be dereferenced’ and my sonar version is SonarLint IntelliJ 4.0.0.2916
I’m getting the following error java null exception specifically “fault in the next code when obj can’t be null in any scenario. Seems the analizer don’t recognice the logic with the funcional object Optional”.
final Obj obj = Optional.ofNullable(oldObj).orElse(new Obj());
if (obj.getSomeProperty() == null) {
Im using null to validate but why is this error “null pointers should not be dereferenced sonar” can anyone help me fix this error….
Aniya
A Null reference means you are trying to get a non existing element,so java throws it as a exception,which is not really a error, so you will exception like null pointers should not be dereferenced sonar or accessed.
I will show you a example program which calls null pointers should not be dereferenced sonar exception.
WholeClass avgmthd= null;
…….
if (!getRatingUtilities().isNR(minRating)) {
return minRating.getRatgCaam(); //Sonar: Null pointers should not be dereferenced
}
as i passed a null value and checking the if condition above,system throwed a null pointer exception ,so find the null accessing areas in your program and try to fix them.
Pubali Halder
If you got this kind of error, let me help you out.
In Java, although we do not always get null pointer errors, sometimes if Null pointer exceptions are thrown when the referenced variable is accessed but is not pointing to any object mentioned in the program code. We can resolve it by try-catch block or if-else conditions.
NullPointerException in java is a type of runtime exception. A special null value can be assigned in java to an object reference.
If you refer to a null pointer, then it should never be accessed or dereferenced. For this NullPointerException is to be thrown. Such type of exception will cause program termination abruptly.
It can also expose debugging information at the worst and that can be useful for an attacker to break the security measures. We should also note that when they are present, they can take advantage of both the annotations including @CheckForNull and @Nonnull. Both of them can understand which values are and are not nullable.
To avoid this error, all the objects should be ensured properly at the time of using them or more appropriately before using them. Also, we should note that when we declare a reference variable, we must verify that the object is not null. To overcome string comparison with literals problem, we should consider invoking it from the literal, instead of invoking the method from the null object. We can also avoid this kind of exception by calling equals literal rather than an object. The ternary operator can also be used to avoid this kind of exception. We should also keep a check on the arguments of a method.
If you found this answer useful, don’t forget to like it. Also, share it with people who are getting this error.
sounds good, it will be great if you help me with example.
PrintWriter out = null; try { out = new PrintWriter( new BufferedWriter( new FileWriter(“out.txt”, true))); out.println(“the text”); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { out.close(); } }
what changes should i go with?
It is some what urgent on my project.
Thank you
you got a null pointer exception for the above code execution ??
not null pointer Bt it is shwoing again obj should be closed in finlly. nd if i remove if condition checking Null , it is accepting for “close obj in finally”. then again sonar will pop up Major issue for null pointer.. Please ref SS
Thanks for your time
neha
In ss the if statements are two times. Either In the finally block instead of all if statements write out.close() or remove null
if possible share ss again
hope it works
hi Rupesh,
just make the source with try , for eg:
Object obj=new Object();———————————(1)
(1)-any Source.
then Access the Source with try and use the benefits inside the try block and Catch (if any Exceptions in Catch block)
ie;
try(Object obj=new Object()){
//do the activities with the obj
}
catch(Exceptions e)
{
//catch -if any exceptions
}
the above code will Automatically Close the obj whenever it done its job!!!!!!!
not getting exception, sonar showing major issue on same code
The Java Rule Null Pointer should not be dereferenced.A refrence to null should never be dereferenced.Doing so well cause a Null pointer exception to be the best such exception handling will cause abrupt program termination.This could expose debugging information.
this looks like a bug to me, am posting here what I posted at stackoverflow(https://stackoverflow.com/questions/71697278/sonar-ternary-operation-throws-warning-a-nullpointerexception-could-be-thrown)
I have java code like this:
Even though, Sonar is showing the following alert:
Clearly this cannot produce NullPointerException; Even worse, if I format this to:
Sonar shows the exact same warning, therefore, I do not see a possible way to avoid this alert.