Solved A strange nullpointerexception

Discussion in 'Spigot Plugin Development' started by Stellrow, Aug 10, 2018.

  1. So im trying to simply give someone a item when they write a command
    Code (Text):
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player) {
                Player player = (Player) sender;
                UUID uuid = player.getUniqueId();
                if(plugin.rpgtool.containsKey(uuid)) {
                    String mdp = plugin.getConfig().getString(".MesajDetinePickaxe");
                    String mdp2 = mdp.replaceAll("&", "§");
                    String mdp3 = mdp2.replaceAll("§§", "§");
                    player.sendMessage(mdp3);
                }else {
                    String mapp = plugin.getConfig().getString(".MesajAiPrimitPickaxe");
                    String mapp2 = mapp.replaceAll("&", "§");
                    String mapp3 = mapp2.replaceAll("§§", "§");
                    player.sendMessage(mapp3);
                    plugin.rpgtool.put(uuid, 1);
                    ItemStack rpgtool = new ItemStack(Material.WOOD_PICKAXE);
                    ItemMeta rpgtoolm = rpgtool.getItemMeta();
                    String np = plugin.getConfig().getString(".NumePickaxe");
                    String np2 = np.replaceAll("&", "§");
                    String np3 = np2.replaceAll("§§", "§");
                    String nume = player.getName();
                    String np4 = np3.replace("%nume", nume);
                    rpgtoolm.setDisplayName(np4);
                   
                    List<String> lore = new ArrayList<String>();
                   
                    String lp1 = plugin.getConfig().getString(".LorePickaxe1");
                    String lp12 = lp1.replaceAll("&", "§");
                    String lp13 = lp12.replaceAll("§§", "§");
                   
                    String lp2 = plugin.getConfig().getString(".LorePickaxe2");
                    String lp21 = lp2.replaceAll("&", "§");
                    String lp22 = lp21.replaceAll("§§", "§");  
                           
                    String lp3 = plugin.getConfig().getString(".LorePickaxe3");
                    String lp31 = lp3.replaceAll("&", "§");
                    String lp32 = lp31.replaceAll("§§", "§");
                   
                    String lp4 = plugin.getConfig().getString(".LorePickaxe4");
                    String lp41 = lp4.replaceAll("&", "§");
                    String lp42 = lp41.replaceAll("§§", "§");
                   
                    lore.add(lp13);
                    lore.add(lp22);
                    lore.add(lp32);
                    lore.add(lp42);
                    rpgtoolm.setLore(lore);
                    rpgtool.setItemMeta(rpgtoolm);
                   
                    player.getInventory().addItem(rpgtool);
    This is the code,i know its a bit messy but meh
    And this is the error
    Code (Text):
    [13:52:47] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'rpgtool' in plugin RPGTools v0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.dispatchCommand(CraftServer.java:648) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at net.minecraft.server.v1_12_R1.PlayerConnection.handleCommand(PlayerConnection.java:1397) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1232) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at net.minecraft.server.v1_12_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_161]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_161]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
        at RPGTools.CommandClass.onCommand(CommandClass.java:31) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.12.2.jar:git-Spigot-642f6d2-6103339]
        ... 15 more
     
  2. What is line 31 in your code?
     
  3. Code (Text):
    String mapp2 = mapp.replaceAll("&", "§");
    A replace to use the & colorcodes
     
  4. Why?
    Code (Java):

      String mapp2 = mapp.replaceAll("&", "§");
      String mapp3 = mapp2.replaceAll("§§", "§");
     
    Code (Java):

       /**
        * Returns a message translated.
        *
        * @param msg
        *            The message to be translated
        *
        * @return A translated message
        */

       public static String tr(String msg) {
           return ChatColor.translateAlternateColorCodes('&', msg);
       }
     

    Code (Java):

    plugin.getConfig().getString(".MesajAiPrimitPickaxe");
     
    Either the plugin instance if null or there is no .MesajAiPrimitPickaxe path in your config.
     
  5. Yep that "i" was the problem
     
  6. If we are still at this topic before i start to create it do you know a good way to modify the lore every xAction..? without filling everything with my disgusting coding habits(spam every simple thing)
     
  7. Create a new thread for this case and do not forget to provide more details on what you wish to achieve. I'm not sure what you mean by saying modify a lore every "xAction".
     
  8. Its a simple breakevent if it breaks a block i will change the part of the lore containing the xp count, getting the value from a config
    But the part changing the lore is worrying me
     
  9. Manipulating lores:
    Code (Java):

    ItemMeta meta = ItemStack#getItemMeta;
    List<String> lore = meta#getLore;

    lore.add(...);
    lore.remove(...);

    meta#setLore(lore);

    ItemStack#setItemMeta(meta);
     
     
  10. I opened a new thread as you said