Skip to content

Commit de0d4fa

Browse files
committed
avoid null pointer error if a characteristic goes missing
like if a characteristic has been removed, but the iOS device hasn't updated its db yet, and tries to set it
1 parent b71a40b commit de0d4fa

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/main/java/io/github/hapjava/server/impl/json/CharacteristicsController.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public HttpResponse get(HttpRequest request) throws Exception {
5151
characteristics.add(characteristic.add("aid", aid).add("iid", iid).build());
5252
} else {
5353
logger.warn(
54-
"Accessory " + aid + " does not have characteristic " + iid + "Request: " + uri);
54+
"Accessory " + aid + " does not have characteristic " + iid + ". Request: " + uri);
5555
}
5656
} else {
5757
logger.warn(
@@ -77,7 +77,16 @@ public HttpResponse put(HttpRequest request, HomekitClientConnection connection)
7777
JsonObject jsonCharacteristic = (JsonObject) value;
7878
int aid = jsonCharacteristic.getInt("aid");
7979
int iid = jsonCharacteristic.getInt("iid");
80-
Characteristic characteristic = registry.getCharacteristics(aid).get(iid);
80+
Map<Integer, Characteristic> accessory = registry.getCharacteristics(aid);
81+
if (accessory.isEmpty()) {
82+
logger.warn("Accessory {} has no characteristics or does not exist.", aid);
83+
return new HapJsonNoContentResponse();
84+
}
85+
Characteristic characteristic = accessory.get(iid);
86+
if (characteristic == null) {
87+
logger.warn("Accessory {} does not have characteristic {}.", aid, iid);
88+
return new HapJsonNoContentResponse();
89+
}
8190

8291
if (jsonCharacteristic.containsKey("value")) {
8392
characteristic.setValue(jsonCharacteristic.get("value"));

0 commit comments

Comments
 (0)