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

Java - Improper warning - Potential null pointer access #3124

Closed
ravindragullapalli opened this issue May 31, 2023 · 9 comments
Closed

Java - Improper warning - Potential null pointer access #3124

ravindragullapalli opened this issue May 31, 2023 · 9 comments
Assignees
Labels

Comments

@ravindragullapalli
Copy link

ravindragullapalli commented May 31, 2023

In Java, Visual Studio Code is not recognizing "Objects.isNull" check statement while giving warning of Potential null pointer access (Java 536871364).

Environment
  • Operating System: Kubuntu 22.04 (Linux x64 5.15.0-32-generic)
  • JDK version: 1.8
  • Visual Studio Code version: 1.78.2
  • Java extension version: Language Support for java v1.18.0
Steps To Reproduce

For example, if the code is like

if (Objects.isNull(obj)){
System.out.println("obj is null");
return null;
}
return obj.toString();// Code is giving warning at this statment.

However, if I do this the warning disappears

if (obj == null){
System.out.println("obj is null");
return null;
}
return obj.toString();// No warning at this statement.
Current Result

"Potential null pointer access: The variable obj may be null at this locationJava(536871364)" warning is displayed

Expected Result

No warning should be there as null check is already done using Objects.isNull

Additional Informations
@snjeza
Copy link
Contributor

snjeza commented May 31, 2023

@ravindragullapalli I can't reproduce the issue.
Could you attach a project example reproducing the error?

@ravindragullapalli
Copy link
Author

ravindragullapalli commented May 31, 2023

@snjeza Please find the attached file. Hope this helps

image

JavaNullTest.zip

@ravindragullapalli
Copy link
Author

ravindragullapalli commented May 31, 2023

Added another method nullTest3 where I am not getting that Potential Null Pointer Access. Please find attached zip file.

image

JavaNullTest.zip

@snjeza snjeza self-assigned this May 31, 2023
@liuhaoXD
Copy link

Null check will also get misleading result in Assert.notNull scenary, here is an another example code snippet:

import javax.annotation.Nullable;

@Data
public class Request<T> {
    @Nullable
    private T data;
}

public class PageMonitorConfigController {
    @GetMapping("/xxx")
    public Object doSth (@RequestBody  Request<Sth> req) {
        Assert.notNull(pageableRequest.getData(), "data is null");
        // got 'Potential null pointer access' here ⬇️
        java.lang.System.out.println(pageableRequest.getData().getContent()); 
        return "done";
    }
}

AFAK, Intellj idea provides an annotation @Contract("null -> fail") for developer to tell static code analyzer annotated method will throw exception when given parameter is null

@iggycoder
Copy link

iggycoder commented Sep 8, 2023

also with AssertJ:

@SuppressWarnings("unchecked")
ResponseEntity<Response> castedResult = (ResponseEntity<Response>)result.getResult();
assertThat(castedResult).isNotNull();
assertThat(castedResult.getStatusCode()).isEqualTo(HttpStatus.OK);

castedResult in second assertThat has warning Potential null pointer access: The variable castedResult may be null at this location

@mcavigelli
Copy link

This issue is marked with "upstream".

It seems @snjeza has fixed this issue in the eclipse.jdt.core project in the middle of December 2023, see mentioned issue.

What needs to be done to have this fix in the vscode-java extension?
It would be very useful to have it in our daily work.

@fbricon
Copy link
Collaborator

fbricon commented Feb 26, 2024

AFAICT it's already fixed in vscode-java, but the issue was not closed. I can't reproduce the issue with @ravindragullapalli's test project.
@mcavigelli are you still experiencing the problem? In which case you should definitely add a new sample project.

Else I'll close this issue.

@mcavigelli
Copy link

I can't reproduce it with the exact same code either. So, yes it's fixed and the issue can be closed.

@ravindragullapalli
Copy link
Author

I can confirm that I am not getting this warning now. Thank you for fixing it.

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

No branches or pull requests

6 participants