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

PreparedStatement.setObject(1,null) throws NPE in edge cases #1614

Closed
1 of 2 tasks
giorgiga opened this issue Nov 22, 2019 · 3 comments
Closed
1 of 2 tasks

PreparedStatement.setObject(1,null) throws NPE in edge cases #1614

giorgiga opened this issue Nov 22, 2019 · 3 comments

Comments

@giorgiga
Copy link

I'm submitting a ...

  • bug report
  • feature request

Describe the issue
PreparedStatement.setObject(1,null) throws NPE instead of SQLException when used on a query with no parameters (see code below)

Driver Version?
42.2.8

Java Version?
13

OS Version?
Mac (Irrelevant)

PostgreSQL Version?
12

To Reproduce
Steps to reproduce the behaviour:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;

import org.postgresql.Driver;

public class NPE {

	private static final String JDBC_URL = "jdbc:postgresql://<server>:5432/<database>";
	private static final String USERNAME = "postgres";
	private static final String PASSWORD = "postgres";

	@SuppressWarnings("resource")
	public static void main(String[] args) throws SQLException {
		Properties properties = new Properties();
		properties.putAll(Map.of("user", USERNAME, "password", PASSWORD));
		Connection c = new Driver().connect(JDBC_URL, properties);

		PreparedStatement ps = c.prepareStatement("select 1"); // note there are no parameters

		// These throw NPE instead of SQLException
		// ps.setObject(-1, null);
		// ps.setObject( 0, null);
		ps.setObject( 1, null);
		//
		// Exception in thread "main" java.lang.NullPointerException
		// 	at org.postgresql.core.v3.SimpleParameterList.setNull(SimpleParameterList.java:159)
		// 	at org.postgresql.jdbc.PgPreparedStatement.setNull(PgPreparedStatement.java:245)
		// 	at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:896)
		// 	at ...
		// This throws SQLE as expected
		// ps.setObject( 1, "");
		// 
		// BTW: the error message is a bit wonky:
		//        "The column index is out of range: 1, number of columns: 0."
		// but we are talking parameters rather than columns here.

		System.out.println("done.");
	}

}

Expected behaviour
An SQLException should be thrown instead of an NPE

Logs
Shouldn't be needed – let me know if otherwise.

@davecramer
Copy link
Member

Thanks for the report, but why are you setting parameters when there are none? Just trying to understand the use case

@giorgiga
Copy link
Author

There is no use case :)
I just noticed this while writing some tests and thought to report it because it might be that there is some other execution path (a "valid" one) that bypasses checks like this one.

@davecramer
Copy link
Member

closed with #1620

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