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

New nullness error in 3.32 on if/else condition #5721

Closed
jpschewe opened this issue Mar 14, 2023 · 3 comments
Closed

New nullness error in 3.32 on if/else condition #5721

jpschewe opened this issue Mar 14, 2023 · 3 comments

Comments

@jpschewe
Copy link

This code was fine with 3.31 and I don't understand how prep can be null after the if/else statements as it's assigned in both branches.

import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.SQLException;

import org.checkerframework.checker.nullness.qual.Nullable;

public class IfConditionNullness {

  private @Nullable PreparedStatement prep = null;
  
  public static final int NON_PERFORMANCE_RUN_NUMBER = -1;
  
  private void createResultSet(final Connection connection,
                               final int tournament,
                               final String categoryName)
    throws SQLException {
    if (NON_PERFORMANCE_RUN_NUMBER == 4) {
      prep = connection.prepareStatement("SELECT * FROM "
                                         + categoryName
                                         + " WHERE TeamNumber = ? AND Tournament = ?");
    } else {
      prep = connection.prepareStatement("SELECT * FROM "
                                         + categoryName
                                         + " WHERE TeamNumber = ? AND Tournament = ? AND RunNumber = ?");
      prep.setInt(3, 5);
    }
    prep.setInt(1, 2);
  }
}

The output is

IfConditionNullness.java:29: error: [dereference.of.nullable] dereference of possibly-null reference prep
    prep.setInt(1, 2);
    ^
jpschewe added a commit to jpschewe/fll-sw that referenced this issue Mar 14, 2023
@smillst
Copy link
Member

smillst commented Mar 14, 2023

This code was fine with 3.31 and I don't understand how prep can be null after the if/else statements as it's assigned in both branches.

prep.setInt(3, 5) could set prep to null. I could write an example like I did here: #5717 (comment).

@mernst
Copy link
Member

mernst commented Mar 14, 2023

In this particular case, you could annotate prep as @MonotonicNonNull rather than @Nullable (assuming it is never set to null except at its declaration). See https://checkerframework.org/manual/#type-refinement-purity .

@jpschewe
Copy link
Author

It is set to null in other places, so I can't use @MonotonicNonNull.

Solved with answer at #5717 (comment)

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

3 participants