Solved java.util.NoSuchElementException: Hashtable Enumerator

Discussion in 'Spigot Plugin Development' started by benyji, Jun 30, 2020 at 8:21 PM.

  1. Hey all,

    I'm working with a java Properties file, I simply want to check onEnable() if all the Properties are valid just incase someone doesn't know how to use Properties files and changes 'X' Instead of 'Z':
    Example:
    X=Z
    The error is as follows:
    Code (Text):
    [15:15:47 ERROR]: Error occurred while enabling CustomOres v1.0 (Is it up to date?)
    java.util.NoSuchElementException: Hashtable Enumerator
            at java.util.Hashtable$Enumerator.nextElement(Unknown Source) ~[?:1.8.0_251]
            at java.util.Hashtable$Enumerator.next(Unknown Source) ~[?:1.8.0_251]
            at com.benyji.core.storage.FileManager.verifyIntegrityOfSettings(FileManager.java:58) ~[?:?]
            at com.benyji.core.Main.onEnable(Main.java:59) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
    I have a method as follows:
    Code (Java):
        public void verifyIntegrityOfSettings() {
            try {
            System.out.println("1");
            final Iterator<String> iterator = settings.stringPropertyNames().iterator();
            ArrayList<String> default_properties = new ArrayList<String>();
            default_properties.addAll(default_settings.stringPropertyNames());
            while (iterator.hasNext()) {
                System.out.println("2");
                final String next = iterator.next();
                final String nextResources = default_properties.toString();
                if (!(next.equals(nextResources))) {
                    System.out.println("3");
                    main.consoleMSG(ChatColor.DARK_RED.toString() + ChatColor.BOLD + "ERROR: " + ChatColor.RED
                            + "Invalid Property in " + ChatColor.YELLOW
                            + "/plugins/CustomOres/configuration/settings.properties" + ChatColor.RED + ", Property: '"
                            + ChatColor.YELLOW + iterator.next() + ChatColor.RED + "', Please change '" + ChatColor.YELLOW
                            + iterator.next() + ChatColor.RED + "' back to its original name." + ChatColor.GRAY
                            + " (use '/reload' for changes to take effect)");
                }
            }
        } catch (NullPointerException e) {
            System.out.println("verify integ is null");
        }
        }
    Line 57 in my Main class is:
    Code (Java):
            fileManager.verifyIntegrityOfSettings();
     
    fileManager is defined as:
    Code (Java):
        private static FileManager fileManager;
     
    yes yes I know I used static, ignore that I can get a better way to access the FileManager class but that is besides the point right now,
    I think we've established that FileManager or fileManager is not null, as I got a null pointer exception on line 57 but once I added the Try and Catch to my method, it was null.

    Your help is greatly appreciated, if more information is needed simply ask and I will show!
     
  2. SteelPhoenix

    Moderator

    You call Iterator#next() multiple times before checking if there even are more elements. Also, why are you catching a NullPointerException?
     
  3. Wait wait I'm confused, I thought "while (iterator.hasNext()) {" is checking if there are more elements? If so that shouldn't be the problem since that is only being ran while there are more elements. As for the NullPointerException tbh I was kind of lazy and just wanted to quickly find if FileManager is null or if the method is, and it worked so I don't question it lol. But yeah still confused as to why its giving me that error if everything falls under "while (iterator.hasNext()) {"
     
  4. SteelPhoenix

    Moderator

    Code (Java):
    Iterable<?> iterable = Arrays.asList(1);
    Iterator<?> it = iterable.iterator(); // Has one element
    while (it.hasNext()) { // Returns true on first iteration
      Object o = it.next(); // Returns 1
      Object o2 = it.next(); // Throws NoSuchElementException - it.hasNext() returns false here
    }
    You grab multiple elements but only check once if a next element is available
     
    • Agree Agree x 1
  5. Oh haha didn't realize that I did that, I'm a visual learner so I appreciate the drawn out explanation, helped me out!