i can't understand what problem with the statement below. No error in sql statement and connection.

Date dt;

dt = (rs.getDate(1)==null?"":rs.getDate(1.trim());

Dani AI

Generated

Quick summary and what likely went wrong (tying back to the replies above): was right that trim() is a String method and not applicable to date/int types. correctly pointed out the ternary operator will not compile if one branch is a String and the other a Date. also hit the key runtime issue: a NullPointerException comes when the ResultSet reference itself is null (or the variable you call methods on is null), not when a column value is SQL NULL.

Checklist to resolve this reliably:

  • Ensure the ResultSet variable is initialized from executeQuery(...) and is not null. Check the stack trace to see exactly which variable is null.
  • Advance the cursor with rs.next() before calling getters; otherwise you get SQL errors for no current row.
  • Do not try to assign a String literal to a Date variable. Keep types consistent: retrieve a java.sql.Date (or java.sql.Timestamp) and then convert/format it to a String if needed.
  • Use a null check on the returned date value (or rs.wasNull() immediately after the getter) to decide how to handle SQL NULL.

Example pattern (retrieve, check, format):

java.sql.Date sqlDate = rs.getDate("my_date_column");
if (sqlDate == null) {
    // handle missing value
} else {
    String formatted = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date(sqlDate.getTime()));
}

References: ResultSet.getDate behavior is documented in the Java SE API ResultSet.getDate(int) and the java.sql.Date class is here: java.sql.Date.

Final note: fix the initialization/flow that produces a null ResultSet first, then correct the type handling. That will eliminate both the compile-time and the NullPointerException issues.

Recommended Answers

All 5 Replies

What is that trim suppossed to be doing?

a number entered like that is an int, which can't be trimmed, and getDate returns a Date, which can also not be trimmed (if that was what you meant). Neither of those are Strings, which is the Class that actually has that method.

What exactly are you trying to do?

My typing error. My code was below. It still has error with the statement.

Date date;
date = (rcd.getDate(1)==null?"":rcd.getDate(1));

hardly surprising. If "rcd" is null you get an exception.
thus the error in your code (or at least that one) is not on that line but elsewhere, where you fail to fill "rcd".

I think that this shouldn't even compile:

Date date;
date = (rcd.getDate(1)==null?"":rcd.getDate(1));

Because if rcd.getDate(1)==null then the expression wil return an empty String: "". But date is type Date:
Plus, I think that the parenthesis should be like this:

date = (rcd.getDate(1)==null)?"":rcd.getDate(1);

it indeed won't compile. but even if it did it wouldn't work if he has a problem getting a resultset and ignored it (as so many kids do).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.