Solved list.remove(number);

Discussion in 'Plugin Development' started by yPedx, May 19, 2017.

  1. Offline

    yPedx

    This will always give me an internal error; (when I remove "number")
    Code:
    int number = Integer.valueOf(args[2]);
    List<String> notes = this.getConfig().getStringList("Note-List."+target.getName()+".notes");
                                this.getConfig().set("Note-List."+target.getName()+".notes", notes);
    
    notes.remove(number);
    Whole code;
    Code:
    if (args.length >= 1 && args[0].equalsIgnoreCase("delete")) {
                            if (!sender.hasPermission("glamreports.delnote")){
                                sender.sendMessage(notePrefix+notesNoPermission);
                        }
                            if (sender.hasPermission("glamreports.delnote")) {
                                if (args.length == 0 || args.length == 1) {
                                    sender.sendMessage(notePrefix+removeNoteUsage);
                                }
                                if(args.length >= 2){
                                    Player target = Bukkit.getPlayer(args[1]);
                                if(args.length == 2 && target != null){
                                    sender.sendMessage(notePrefix+notesSpecifyID);
                                }
                                if(args.length == 3){
                                int number = Integer.valueOf(args[2]);
                                if (target == null && args.length == 2 || target == null && args.length == 3){
                                    sender.sendMessage(notePrefix+notesPlayerNotFound);
                                }
                                List<String> notes = this.getConfig().getStringList("Note-List."+target.getName()+".notes");
                                this.getConfig().set("Note-List."+target.getName()+".notes", notes);
                                if (target != null && args.length == 3 && number <= maxNotes){
                                if(notes.size() == 0 || number > notes.size() || number == 0 || notes == null){
                                    sender.sendMessage(notePrefix+removedFailure);
                                }
                                if(number <= notes.size() || number != 0){
                                notes.remove(number-1);
                                saveConfig();
                                sender.sendMessage(notePrefix+removedNote.replaceAll("%ID%", String.valueOf(number)).replaceAll("%player%", target.getName()));
                        }}}}}}
    Any way to catch the internal error and output a message?
     
  2. @yPedx
    What is the error in the config? That is more helpful than the internal error message.

    Also, you should use else statements and indent your code correctly. It is very hard to see how your code is encapsulated, and a few of the if statements could be simplified to just an else statement.
     
  3. Offline

    yPedx

    @Zombie_Striker
    Code:
    [19:23:10 INFO]: Accountor issued server command: /note delete Accountor 1
    [19:23:10 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'note' in plugin GlamReports v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [Spigot.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
            at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_131]
            at java.util.ArrayList.remove(Unknown Source) ~[?:1.8.0_131]
            at me.developer.kriss.GlamReports.onCommand(GlamReports.java:245) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Spigot.jar:git-Spigot-21fe707-e1ebe52]
            ... 15 more
    If the note list doesn't have any notes, it gives an internal error.. It works fine if the list contains notes.
    Line 245 is notes.remove(number);

    And, true. I should start using else statements more often.
     
  4. @yPedx
    Make sure the notes.size() is greater than 0 before trying to remove the number.
     
  5. Offline

    yPedx

    @Zombie_Striker
    Already did that :/
    Code:
    if(notes.size() == 0 || number > notes.size() || number == 0 || notes == null){
                                    sender.sendMessage(notePrefix+removedFailure);
                                }
     
  6. @yPedx
    But you're not returning, nor do you use any else statements. All that does is print the message, and then continue as though there are notes. You either need to return or add an else statement so that the next bit of code does not run.
     
  7. Offline

    yPedx

    @Zombie_Striker
    Ah, returning worked.
    However, if the number is higher than the maximum size of the list (10) no message is displayed.

    EDIT: Fixed.
     

Share This Page