NPE when using different flow style

Issue #319 invalid
Former user created an issue

Hi, using 1.16 I got NPE when dumping yaml:

java.lang.NullPointerException at org.yaml.snakeyaml.emitter.Emitter.expectNode(Emitter.java:400) at org.yaml.snakeyaml.emitter.Emitter.access$12(Emitter.java:381) at org.yaml.snakeyaml.emitter.Emitter$ExpectBlockSequenceItem.expect(Emitter.java:595) at org.yaml.snakeyaml.emitter.Emitter$ExpectFirstBlockSequenceItem.expect(Emitter.java:576) at org.yaml.snakeyaml.emitter.Emitter.emit(Emitter.java:216) at org.yaml.snakeyaml.serializer.Serializer.serializeNode(Serializer.java:185) at org.yaml.snakeyaml.serializer.Serializer.serializeNode(Serializer.java:209) at org.yaml.snakeyaml.serializer.Serializer.serializeNode(Serializer.java:195) at org.yaml.snakeyaml.serializer.Serializer.serialize(Serializer.java:113) at org.yaml.snakeyaml.Yaml.dumpAll(Yaml.java:243) at org.yaml.snakeyaml.Yaml.dumpAll(Yaml.java:206)

Proposed fix - see attached file. Thanks

Comments (8)

  1. Andrey Somov

    Hi.

    1. I think it is better to register and give us your name to make the communication simpler.
    2. What is the context ? Can you please show when the current implementation fails ?
    3. With your change a lot of tests fail. We do not accept a patch if it breaks tests without explanation.
  2. Max Lee

    I'm encountering the same issue.

    My context is rather unorthodox: I use it in a plugin as part of the enhanced Minecraft server API "Bukkit". This error occurs when I call the the code on a special ingame item to save it to a yaml config. (https://i.moep.tv/dPIq32Uh)

    If anybody wants to test that out, it's rather simple to reproduce with the following code and a special skull item given ingame.

    I suspect that it occurs as part of some special item data as it works fine when replacing the Id part of the item string in the command with Name:"-" (Which would still work in the game itself)

    It might actually be an error in the way the item itself is saved (some value being null) but snakeyml should be able to catch that case then imo.

    Item:

    /give @p skull 1 3 {display:{Name:"Snowman"},SkullOwner:{Id:"579e88d6-de86-4db0-840e-ff2a48512586",Properties:{textures:[{Value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNTgxNzFlZTUyY2RlOGE4Zjk4MTdkMGEyYzc4MmM3NjE1YmZkNTMwMWRjOTIwNzZlNzhmZTkyYWFkMWE5M2JmIn19fQ=="}]}}}
    

    Bukkit JavaPlugin code:

    Player player = plugin.getServer().getPlayer("your_username_here");
    ItemStack item = player.getItemInHand();
    plugin.getConfig().set("item", item);
    plugin.saveConfig(); // <-- This uses snakeyml and will throw the npe
    

    Hold the item in your hand and execute the code (via a command for example) and the error occurs.

  3. Andrey Somov

    1) the provided YAML (item, skull) is no valid YAML. You can try http://www.yamllint.com/ to prove it. YAML 1.1 requires a space after ':' (the space is optional in YAML 1.2 to be compatible with JSON)

    2) It is completely unclear how to reproduce the problem. This message is very confusing: "Hold the item in your hand and execute the code". What is Player ? What is ItemStack ? What is plugin ? How SnakeYAML is used inside "plugin.saveConfig()" ? Please create a small project with all the dependencies and a failing test.

  4. Max Lee

    As I said, my case is rather unorthodox as it happens when using a plugin on an object provided by a closed program via a third party plugin API.

    To 1): The format for the items used in this command of the game is not YAML but a slightly modified version of JSON. (sometimes referred to as Mojangson) But that doesn't really matter as it translates this into an internal data structure (NBT).

    To 2): My example was intended for someone already knowing the API, I'll post a project which shows the failing without requiring the game itself when I find the time.

    For where the SnakeYAML is used: the saveConfig() method ends up calling SnakeYAML's in this saveToString() method.

  5. Max Lee

    Oh, sorry, I kinda forgot about that issue as I ended up using a workaround and had other things in mind. I checked the values it tries to save and it really seems to be a problem with a null value set by the Bukkit API so the issue really has nothing at all to do with snakeyaml but improper implementation of the flow style.

    #!
    
    [15:34:55 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'testplugin:test' in plugin Testplugin v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:664) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1188) [paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1000) [paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_66]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_66]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:774) [paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:378) [paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:713) [paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:615) [paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66]
    Caused by: java.lang.NullPointerException: null value in entry: skull-owner=null
            at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:33) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at com.google.common.collect.ImmutableMap.entryOf(ImmutableMap.java:135) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at com.google.common.collect.ImmutableMap$Builder.put(ImmutableMap.java:206) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftMetaSkull.serialize(CraftMetaSkull.java:181) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftMetaItem.serialize(CraftMetaItem.java:850) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftMetaItem.toString(CraftMetaItem.java:961) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at java.lang.String.valueOf(String.java:2994) ~[?:1.8.0_66]
            at java.lang.StringBuilder.append(StringBuilder.java:131) ~[?:1.8.0_66]
            at org.bukkit.inventory.ItemStack.toString(ItemStack.java:304) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at java.lang.String.valueOf(String.java:2994) ~[?:1.8.0_66]
            at java.lang.StringBuilder.append(StringBuilder.java:131) ~[?:1.8.0_66]
            at java.util.AbstractMap.toString(AbstractMap.java:536) ~[?:1.8.0_66]
            at java.lang.String.valueOf(String.java:2994) ~[?:1.8.0_66]
            at java.io.PrintStream.println(PrintStream.java:821) ~[?:1.8.0_66]
            at org.bukkit.configuration.file.YamlConfiguration.saveToString(YamlConfiguration.java:40) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at org.bukkit.configuration.file.FileConfiguration.save(FileConfiguration.java:103) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at org.bukkit.plugin.java.JavaPlugin.saveConfig(JavaPlugin.java:230) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            at de.themoep.Testplugin.Testplugin.onCommand(Testplugin.java:188) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[paperspigot-1.8.8-serialization-test.jar:git-PaperSpigot-a187a0f-75593ab]
            ... 15 more
    
  6. Log in to comment