Background
Sometimes functions return null in some cases, and yet the one who uses them did not know about it, so NPE is inevitable.
For example (and just an example to show what I'm talking about):
public class Test { public static void main(final String[] args) { final int t=0; System.out.println("result:"+foo(t).toString());
Problem
Eclipse does not warn about this, and does not even have it in the settings.
It can detect such problems only when no functions are involved (see my editing below).
Not only that, but I can't find an annotation that says the return value can be null. I think the opposite is true.
What i tried
So, I thought that since Eclipse does not have some checks (and this is good), I could use alternative static code analysis tools like FindBugs and CodePro Analytix . Both could not find such errors.
I tried to contact both projects, but have not received an answer yet. for CodePro Iโm not even sure that I did it right ... Actually, I think this project was discontinued (last update in 2010 ...).
Question
Is there any tool that can help avoid such errors?
EDIT: since many people think that this is solvable by simply changing the code, think about working in a huge team where some people might forget about it (everyone could be wrong). Or maybe you are even working on a project that uses the SDK (which may even be closed source), which may return null in some cases, but it is not documented because the creator of the SDK forgot about it.
This can happen, no matter how good you are as a programmer.
What I ask is a way to overcome this by allowing eclipse help. he should be able to warn me, at least in the basic examples that I wrote here.
Eclipse should be able to do such checks, just like it can warn me about this example:
int t=0; --t; String s="s"; if(t<0) s=null; System.out.println(s.toString());
This can be detected when you enable it in the Eclipse settings:
Java-> Compiler-> Errors/Warning-> Potential null pointer access
I like Eclipse because it has a lot of great warnings and errors that can help avoid errors, but what I wrote above is not detected by it and not even with the tools that I talked about.
Many errors are hard to find, and no one is the perfect programmer who is not mistaken. Even if you know a lot, you are not protected from other people's mistakes. People may not even find what is wrong with the base code (see here , here or just here to see how well you can find the errors yourself). That's why there are tools that can help you.