Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic NPE cases #9

Open
razum2um opened this issue Apr 29, 2019 · 1 comment
Open

Dynamic NPE cases #9

razum2um opened this issue Apr 29, 2019 · 1 comment

Comments

@razum2um
Copy link

razum2um commented Apr 29, 2019

Based on approach taken in

for (NullLiteralExpr nullLiteralExpr : StaticJavaParser.parse(stream).findAll(NullLiteralExpr.class)) {

I think the famous NPE is happening 10% because of explicit return null; / x = null; in various flavours,
but 90% due something like HashMap.get("non-existing-key") or getters for non-initialised object fields with defaults, e.g. name in https://www.codebyamir.com/blog/stop-returning-null-in-java

Besides, in the second case, it makes sense to skip/allow != null pattern as comparison doesn't do any harm + it's often unavoidable for an imported library classes you don't control

@nikialeksey
Copy link
Owner

Agreed with you about comparision with null (#10).

Absolutely, there is cases with NPE due to non-initialised fields, or non existing keys in map, but I did see this statistics, and it's pretty hard to implement dynamic npe analysis. I think, we can avoid many cases of dynamic NPE by other ways: use only final fields, every check map for existing or not keys and so on. Good idea for new static analysis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants