From 643d85c5563e276d617107a7ca8832ea6185f138 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 10 Jan 2024 10:41:22 +0100 Subject: [PATCH] [xdata] Fix NPE in FillableForm Calling write() in FillableForm's constructor causes a NPE because write() makes use of requiredFields which has not been set at this time. Furthermore, write() makes use of missingRequiredFields, which is also populated in that loop. Therefore, we have to delay the invocation of write() until requiredFields got set. Thanks to Dan Caseley for reporting this. Reported-by: Dan Caseley --- .../org/jivesoftware/smackx/xdata/form/FillableForm.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java index c14990a9a1..332c7fc3ff 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java @@ -1,6 +1,6 @@ /** * - * Copyright 2020 Florian Schmaus + * Copyright 2020-2024 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,6 +54,7 @@ public FillableForm(DataForm dataForm) { } Set requiredFields = new HashSet<>(); + List requiredFieldsWithDefaultValue = new ArrayList<>(); for (FormField formField : dataForm.getFields()) { if (formField.isRequired()) { String fieldName = formField.getFieldName(); @@ -61,13 +62,17 @@ public FillableForm(DataForm dataForm) { if (formField.hasValueSet()) { // This is a form field with a default value. - write(formField); + requiredFieldsWithDefaultValue.add(formField); } else { missingRequiredFields.add(fieldName); } } } this.requiredFields = Collections.unmodifiableSet(requiredFields); + + for (FormField field : requiredFieldsWithDefaultValue) { + write(field); + } } protected void writeListMulti(String fieldName, List values) {