Skip to content

[Bug] NullPointerException in SummonMerchantEnchantment.onPlayerChat on every chat message #204

Description

@sudapeople

[Bug] NullPointerException in SummonMerchantEnchantment.onPlayerChat on every chat message

EliteMobs Version: 10.0.1
Server: Paper 1.21.8
Java: 21

Description

Every time a player sends a chat message, the following error is thrown:

[ERROR] Could not pass event AsyncPlayerChatEvent to EliteMobs v10.0.1
java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because
"com.magmaguy.elitemobs.items.customenchantments.SummonMerchantEnchantment.merchantMessage" is null
    at SummonMerchantEnchantment$SummonMerchantEvents.onPlayerChat(SummonMerchantEnchantment.java:88)

Root Cause

In SummonMerchantEnchantment.java (Line 24), the merchantMessage field is initialized as a static final field:

private static final String merchantMessage = EnchantmentsConfig.getEnchantment("summon_merchant.yml")
    .getFileConfiguration().getString("message");

However, the message key is only written to the config file by SummonMerchantConfig.processAdditionalFields(), which runs after the SummonMerchantEnchantment class is loaded by the JVM.

This means getString("message") returns null because the key doesn't exist in the YAML file yet at the time of class loading.

The onPlayerChat method at line 88 then calls merchantMessage.isEmpty() on this null value, causing the NPE on every single chat message.

Suggested Fix

Either:

Option 1: Use a null-safe default value

private static final String merchantMessage = EnchantmentsConfig.getEnchantment("summon_merchant.yml")
    .getFileConfiguration().getString("message", "");

Option 2: Add a null check in onPlayerChat

@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event) {
    if (merchantMessage == null || merchantMessage.isEmpty()) return;
    // ...
}

Option 3: Read from SummonMerchantConfig.message instead

Use SummonMerchantConfig.message (the static field that IS properly initialized via processAdditionalFields) instead of directly reading from FileConfiguration.

Steps to Reproduce

  1. Fresh install EliteMobs 10.0.1
  2. Start the server
  3. Join and type any chat message
  4. Observe the NPE in console

Additional Notes

  • Setting isEnabled: false in enchantments/summon_merchant.yml does NOT prevent the error, because the event listener is still registered regardless of the enchantment's enabled state.
  • The message key is never written to the YAML file, confirming that processAdditionalFields() is not being called before the static initializer runs.
  • The error does not crash the server or prevent chat from working — the chat message is still delivered — but it spams the console with error stack traces on every single chat message.

Relevant Source Files

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions