0

So I have an issue. My RPC calls will fail due to "NullpointExeption". Meaning that one column contains null in value. This is done on purpose, as I am gonna fill this column with value at some time. How can I allow the Java code to ignore/ or allow this error to happen thru error handling??

@Override
public List <BreakRegistered> getAllRegisteredBreaks() throws IllegalArgumentException {

   List<BreakRegistered> resultsfromquery = null;
   ResultSet resultSet = null;

   try {
       //Execute query kaldes på resultsetter, fordi der kun nedhentes data
       resultSet = getAllEmployeesBreaks.executeQuery();
       resultsfromquery = new ArrayList<BreakRegistered>();

       while (resultSet.next()) {
           resultsfromquery.add(new BreakRegistered(
                   resultSet.getTimestamp("time").toString(),
                   resultSet.getTimestamp("checkedOut").toString(),  //Error occurs HERE!
                   resultSet.getString("navn"),
                   resultSet.getInt("medarbejderID")));
       }
   } catch (SQLException sqlException) {
       throw new IllegalArgumentException(" \"getBreaks\" fejlede");
   } finally {
       try {
           resultSet.close();
       } catch (SQLException sqlException) {
           sqlException.printStackTrace();
           close();
       }
   }

   return resultsfromquery;
}
jarlh
  • 42,561
  • 8
  • 45
  • 63
JokesOnYou
  • 23
  • 6
  • 1
    I'm curious as to how you find yourself at the level of doing SQL but have never had to catch a `NullPointerException`? Especially since you already have a `catch` block that you could copy the logic of? – josh.trow Apr 17 '20 at 12:55
  • Before calling the toString method, check if it's null. If it's null, go to the next one, if not, use toString – user Apr 17 '20 at 12:56
  • Does `resultSet.getTimestamp("checkedOut")` return `null`. Are you fine with the String `"null"`? If the answer for both of those are yes, then use `String.valueOf(resultSet.getTimestamp("checkedOut"))`. – Johannes Kuhn Apr 17 '20 at 13:10
  • @JohannesKuhn, you have honestly saved me. Your comment was simple and quick solution to my stupid problem. Thank you very much. I do have en issue tho. My datatable now prints "Null" as text at the column. How do I instead print " ". Can I add some if logic in the server method or what? – JokesOnYou Apr 17 '20 at 13:23
  • See the answers below. Adapt one of them. (obviously, the answer to my second question was no, but at least it answers the first one) – Johannes Kuhn Apr 17 '20 at 13:24
  • @JohannesKuhn, it didnt really answer. Sorry btw, I should have said that i wasnt fine with the printing of "null" when you asked. Im thinking if i was to say that the SQL table's column cannot be null "NOT NULL" Would that cause further errors, or would it then print ""? – JokesOnYou Apr 17 '20 at 13:42

2 Answers2

0

The implication is that resultSet.getTimestamp("checkedOut") is returning null. You need to check for that before you try to deference the object to call toString().

A typical construct for this might be:

Object time = resultSet.getTimestamp("checkedOut");  //Or use a more specific type
String timeAsString;
if ( time != null)
  timeAsString = time.toString();
else
  timeAsString = null;  // or empty string, or any other placeholder value you want

P.S. It looks like you are executing SQL queries. I would not call those "RPC calls".

Dave Costa
  • 47,262
  • 8
  • 56
  • 72
0

You just need to check whether it is null. If you will use this query result later, It would be nice to put the logic into Object. Moreover, In this case, you just need to change the constructor if there will be some changes to your DB in the future.

public BreakRegistered(ResultSet resultSet) {
    this.time = resultSet.getTimestamp("time") != null ? resultSet.getTimestamp("time").toString() : null;
    this.checkedOut = resultSet.getTimestamp("checkedOut") != null ? resultSet.getTimestamp("checkedOut").toString() : null;
    this.navn = resultSet.getString("navn");
    this.medarbejderID = resultSet.getInt("medarbejderID")
}
emredmrcn
  • 144
  • 1
  • 5