This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 223031 - Wrong Warning - Dereferencing possible null pointer
Summary: Wrong Warning - Dereferencing possible null pointer
Status: REOPENED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.3
Hardware: PC Linux
: P3 normal with 3 votes (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-11-30 08:19 UTC by mone.java
Modified: 2016-08-10 18:23 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
IDE log (168.57 KB, text/plain)
2012-11-30 08:19 UTC, mone.java
Details
Another false positiv example (17.58 KB, image/png)
2013-01-14 19:30 UTC, markiewb
Details

Note You need to log in before you can comment on or make changes to this bug.
Description mone.java 2012-11-30 08:19:27 UTC
Product Version = NetBeans IDE 7.3 Beta 2 (Build 201211062253)
Operating System = Linux version 2.6.32-5-686-bigmem running on i386
Java; VM; Vendor = 1.7.0_03
Runtime = Java HotSpot(TM) Client VM 22.1-b02

    private void createBtnActionPerformed(java.awt.event.ActionEvent evt) {
        nota = new Note();
        String text = noteTxt.getText();
        String user = userFld.getText();
        Calendar c = dataCal.getCalendar();
        if(text.isEmpty() || user.isEmpty() || c == null) {
            JOptionPane.showMessageDialog(this, "You must fill all fields", "Error!", JOptionPane.ERROR_MESSAGE);
            nota = null;
            return;
        }
        Date date = c.getTime();
        nota.setNote(text); # Causing Dereferencing possible null pointer
        nota.setDataNota(date);
        nota.setUtenteModifica(user);
        nota.setProprietario(user);
        dispose();
    }

As you can see in the example above, the line nota.setNote(text) cause Warning - Dereferencing possible null pointer but is impossible to have a null pointer exception here.
Comment 1 mone.java 2012-11-30 08:19:35 UTC
Created attachment 128620 [details]
IDE log
Comment 2 Ralph Ruijs 2012-12-03 11:38:51 UTC
Another test case from refactoring.java/src/org/netbeans/modules/refactoring/java/spi/ui/JavaRefactoringActionDelegate.java:

JEditorPane pane = NbDocument.findRecentEditorPane(ck);
result = pane != null;
if (requiresSelection) {
    result = result && (pane.getSelectionStart() != pane.getSelectionEnd());
}
Comment 3 markiewb 2013-01-14 19:30:06 UTC
Created attachment 130205 [details]
Another false positiv example

Another false positiv example - see screenshot
Comment 4 wraithguard01 2013-03-05 16:57:16 UTC
Here's another case:

public void main(){
	double userInput = java.lang.Math.random();
	Object o = null;
	if(userInput > .5){
		o = new Object();
	}
	assertion(o); //This will exit with an Error if o is null
	System.out.println(o.toString()); //Warning still displays here
}

public void assertion(Object o){
	if(o == null){
		throw new Error("o is null");
	}
}

If I manually inline the code in the assertion(), the warning goes away.
Comment 5 ralphlevan 2013-03-22 14:32:34 UTC
Me too.  Problem happens with the variable "query".  We clearly test for a null value and return if that happens.  But later, query.getBytes() raises that warning.


    public SearchRetrieveResponseType doRequest(SearchRetrieveRequestType request) throws ServletException {
        // stuff omitted
        String query = request.getQuery();
        if(query==null || query.length()==0)
            return diagnostic(SRWDiagnostic.MandatoryParameterNotSupplied, "query", response);

        String resultSetID = getResultSetId(query);
        if (resultSetID!=null) { // got a cached result
            // omitted
        }
        else { // Evaluate the query.
            try {
                result = getQueryResult(query, request);
            } catch (InstantiationException e) {
                log.error("Exception "+e.getMessage()+" caught while doing query:");
                try {
                    log.error(Utilities.byteArrayToString(query.getBytes("UTF-8")));
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(SRWDatabase.class.getName()).log(Level.SEVERE, null, ex);
                }
                log.error("request: "+request);
                log.error(e, e);
                return diagnostic(SRWDiagnostic.GeneralSystemError,
                        e.getMessage(), response);
            }
        }
Comment 6 ralphlevan 2013-03-22 14:33:44 UTC
Me too.  Problem happens with the variable "query".  We clearly test for a null value and return if that happens.  But later, query.getBytes() raises that warning.


    public SearchRetrieveResponseType doRequest(SearchRetrieveRequestType request) throws ServletException {
        // stuff omitted
        String query = request.getQuery();
        if(query==null || query.length()==0)
            return diagnostic(SRWDiagnostic.MandatoryParameterNotSupplied, "query", response);

        String resultSetID = getResultSetId(query);
        if (resultSetID!=null) { // got a cached result
            // omitted
        }
        else { // Evaluate the query.
            try {
                result = getQueryResult(query, request);
            } catch (InstantiationException e) {
                log.error("Exception "+e.getMessage()+" caught while doing query:");
                try {
                    log.error(Utilities.byteArrayToString(query.getBytes("UTF-8")));
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(SRWDatabase.class.getName()).log(Level.SEVERE, null, ex);
                }
                log.error("request: "+request);
                log.error(e, e);
                return diagnostic(SRWDiagnostic.GeneralSystemError,
                        e.getMessage(), response);
            }
        }
Comment 7 Svata Dedic 2014-09-11 13:29:28 UTC
This defect seems to be fixed in dev. The null assignment in the if-branch with `return' statement does not affect the possibly-null state in the rest of the method.
Comment 8 ememisya 2016-08-10 18:06:13 UTC
I have NetBeans IDE 8.1 (Build 201510222201) and the following false positive may still be observed:

boolean hasParameters = true;
final List<String> currentAuthorityValues = currentAuthorityParameters.get(parameterKey);
//Null check, here!
hasParameters &= currentAuthorityValues != null;
//False warning for the below line
for (String requiredValue : currentAuthorityValues) {
  ....
}
Comment 9 ememisya 2016-08-10 18:23:19 UTC
(In reply to ememisya from comment #8)
> I have NetBeans IDE 8.1 (Build 201510222201) and the following false
> positive may still be observed:
> 
> boolean hasParameters = true;
> final List<String> currentAuthorityValues =
> currentAuthorityParameters.get(parameterKey);
> //Null check, here!
> hasParameters &= currentAuthorityValues != null;
> //False warning for the below line
> for (String requiredValue : currentAuthorityValues) {
>   ....
> }

Err sorry forgot to include a line, better cases below:

  static class Test {
    List<String> value;
    public List<String> getValue() {
      return value;
    }
    public void setValue(List<String> _value) {
      value = _value;
    }
  }

  public static void main(String[] args) {
    Test test = new Test();
    boolean hasParameters = true;
    final List<String> currentAuthorityValues = test.getValue();
//Null check, here!
    hasParameters &= currentAuthorityValues != null;
//False warning for the below line
    if (hasParameters) {
      for (String requiredValue : currentAuthorityValues) {
        test.getValue();
      }
    }
  }

also

  static class Test {

    List<String> value;

    public List<String> getValue() {
      return value;
    }

    public void setValue(List<String> _value) {
      value = _value;
    }
  }

  public static void main(String[] args) {
    Test test = new Test();
    final List<String> currentAuthorityValues = test.getValue();
    boolean hasParameters = currentAuthorityValues != null;
    if (hasParameters) {
      for (String requiredValue : currentAuthorityValues) {
        test.getValue();
      }
    }
  }