Skip to content

Commit 17ef727

Browse files
committed
Fixed NullPointer when mapping nullable date values.
1 parent b405798 commit 17ef727

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Include the Maven artifact:
2020
<dependency>
2121
<groupId>com.github.collinalpert</groupId>
2222
<artifactId>java2db</artifactId>
23-
<version>4.1</version>
23+
<version>4.1.1</version>
2424
</dependency>
2525
```
2626
Or include the [JAR](https://github.com/CollinAlpert/Java2DB/releases/latest) in your project.
@@ -101,7 +101,7 @@ public class PersonService extends BaseService<Person> {
101101
Every service *must* extend `BaseService`.
102102

103103
That's it! Now we can get data from the database using the services using simple methods like `getById` and so on.\
104-
As you can see from the example, custom methods can be defined in the respective service using the `getSingle` or `getMultiple` methods provided by the `BaseService`.\
104+
As you can see from the example, custom methods can be defined in the respective service using the `getSingle` or `getMultiple` methods provided by the `BaseService`.
105105

106106
The last thing you need to do is give Java2DB access to your database. Set the static variables `HOST`, `DATABASE`, `USERNAME`, `PASSWORD` and optionally `PORT` of the `DBConnection` class to achieve possibility of connection.
107107

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.collinalpert</groupId>
88
<artifactId>java2db</artifactId>
9-
<version>4.1</version>
9+
<version>4.1.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Java2DB</name>

src/main/java/com/github/collinalpert/java2db/mappers/BaseMapper.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,7 @@ private <E extends BaseEntity> void setFields(ResultSet set, E entity, String id
181181
var columnName = Utilities.getColumnName(field);
182182
var columnLabel = (identifier == null ? Utilities.getTableName(entity.getClass()) : identifier) + "_" + columnName;
183183

184-
Object value;
185-
if (field.getType() == LocalDateTime.class) {
186-
value = set.getTimestamp(columnLabel, Calendar.getInstance(Locale.getDefault())).toLocalDateTime();
187-
} else if (field.getType() == LocalDate.class) {
188-
value = set.getDate(columnLabel, Calendar.getInstance(Locale.getDefault())).toLocalDate();
189-
} else if (field.getType() == LocalTime.class) {
190-
value = set.getTime(columnLabel, Calendar.getInstance(Locale.getDefault())).toLocalTime();
191-
} else {
192-
value = set.getObject(columnLabel);
193-
}
184+
Object value = getValue(set, columnLabel, field.getType());
194185

195186
if (value == null) {
196187
continue;
@@ -200,6 +191,21 @@ private <E extends BaseEntity> void setFields(ResultSet set, E entity, String id
200191
}
201192
}
202193

194+
private Object getValue(ResultSet set, String columnLabel, Class<?> type) throws SQLException {
195+
if (type == LocalDateTime.class) {
196+
var value = set.getTimestamp(columnLabel, Calendar.getInstance(Locale.getDefault()));
197+
return value == null ? null : value.toLocalDateTime();
198+
} else if (type == LocalDate.class) {
199+
var value = set.getDate(columnLabel, Calendar.getInstance(Locale.getDefault()));
200+
return value == null ? null : value.toLocalDate();
201+
} else if (type == LocalTime.class) {
202+
var value = set.getTime(columnLabel, Calendar.getInstance(Locale.getDefault()));
203+
return value == null ? null : value.toLocalTime();
204+
} else {
205+
return set.getObject(columnLabel);
206+
}
207+
}
208+
203209
private String getForeignKeyName(Field field) {
204210
var foreignKeyColumnName = field.getAnnotation(ForeignKeyEntity.class).value();
205211

src/main/java/com/github/collinalpert/java2db/utilities/Utilities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public static <E extends Throwable> void tryAction(ThrowableRunnable<E> runnable
203203
}
204204

205205
/**
206-
* Tries to perform a certain supplier and retrieve its value while considering a checked exception that could occur.
206+
* Tries to execute a supplier and retrieve its value while considering a checked exception that could occur.
207207
*
208208
* @param supplier The {@code Supplier} to try to execute.
209209
* @param <T> The type of value to return.

0 commit comments

Comments
 (0)