From 8b9a9e0f3e11d8a251c01fe68678dfea6c96e05d 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 b40cb92a82..d0657ba083 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. @@ -55,6 +55,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(); @@ -62,13 +63,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) {