Skip to content

Fix NullPointerException in TestPanel Prefs.getRecentMessageXmlFiles on fresh install#143

Open
jmwall wants to merge 1 commit intohapifhir:masterfrom
jmwall:fix/testpanel-npe-recent-message-xml-files
Open

Fix NullPointerException in TestPanel Prefs.getRecentMessageXmlFiles on fresh install#143
jmwall wants to merge 1 commit intohapifhir:masterfrom
jmwall:fix/testpanel-npe-recent-message-xml-files

Conversation

@jmwall
Copy link

@jmwall jmwall commented Feb 28, 2026

Bug

On a fresh install (no existing prefs.xml), closing or removing a message from the TestPanel message list throws a NullPointerException at Prefs.java:388 because myRecentMessageXmlFiles is null when getRecentMessageXmlFiles() iterates over it.

Fix

Added a null check that initializes savedVals to an empty ArrayList<XmlFormat>() if myRecentMessageXmlFiles is null — matching the existing pattern already used for myOpenTableFiles in the same class.

Reproduction

  1. Delete ~/HapiTestPanel/prefs.xml (or launch on a machine that has never run TestPanel)
  2. Launch TestPanel
  3. Open any message
  4. Attempt to close/remove it
  5. The dialog appears but clicking Yes/No/Cancel fails silently with an NPE in the console

Root Cause

Regression introduced in [c679d04](https://github.com/hapifhir/hapi-hl7v2/commit/c679d04c08e0d46c4d1036dd2408d80ac77a2347) ("Allow exporting and importing of profile group files", Sep 2012) which refactored Prefs.java from the Java Preferences API to JAXB-serialized XML fields.

The old code always produced a non-null list because ourPrefs.get() provided a default empty string:

// Old code — always safe, savedVals is never null
ArrayList<String> savedVals = new ArrayList<String>(
    Arrays.asList(ourPrefs.get(GET_RECENT_MESSAGE_FILES, "").split("\\n")));

The new code directly assigns the JAXB field, which is null until prefs.xml exists and has been deserialized:

// New code — null on fresh install when no prefs.xml exists
List<XmlFormat> savedVals = myRecentMessageXmlFiles;
for (XmlFormat string : savedVals) {  // NPE here

Notably, the same commit correctly handled the null case for the analogous myOpenTableFiles field:

if (myOpenTableFiles == null) {
    myOpenTableFiles = new ArrayList<String>();
}

This fix applies the same null-guard pattern that was used for myOpenTableFiles but was missed for myRecentMessageXmlFiles.

…nstall

myRecentMessageXmlFiles is null when no prefs.xml exists yet, causing
an NPE when getRecentMessageXmlFiles() iterates over it.

Added null check matching the existing pattern used for myOpenTableFiles.

Regression from c679d04 (Sep 2012) which migrated Prefs from the Java
Preferences API to JAXB-serialized XML fields without guarding this field against null.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant