Solved NPE while trying to teleport player

Discussion in 'Spigot Plugin Development' started by Fluoro, Apr 14, 2017.

  1. I am trying to teleport players in a game. Players are sorted numerically by their rating. The format is playerName*rating (* is not multiply). For some reason, on line 80 p.teleport(tps); (bottom of code), I am getting a NullpointerException.

    Code (Text):
    [18:48:06] [Craft Scheduler Thread - 4/WARN]: Exception in thread "Craft Scheduler Thread - 4"
    [18:48:06] [Craft Scheduler Thread - 4/WARN]: org.apache.commons.lang.UnhandledException: Plugin ArenaBrawl v1.0 generated an exception while executing task 12
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException
        at net.gamer818.Locations.teleportPlayers(Locations.java:80)
        at net.gamer818.ArenaJoin$1.run(ArenaJoin.java:73)
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
        ... 3 more
    Code (Text):
    package net.gamer818;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;

    public class Locations {
        public static ArrayList<Location> tp = new ArrayList();

        public Locations() {
            World w = Bukkit.getWorld("world");
            tp.add(new Location(w, -2498.977D, 17.0D, 779.953D));
            tp.add(new Location(w, -2498.977D, 17.0D, 779.953D));
            tp.add(new Location(w, -2443.894D, 17.0D, 802.975D));
            tp.add(new Location(w, -2443.894D, 17.0D, 802.975D));
            tp.add(new Location(w, -2530.024D, 17.0D, 772.028D));
            tp.add(new Location(w, -2530.024D, 17.0D, 772.028D));
            tp.add(new Location(w, -2506.965D, 17.0D, 716.968D));
            tp.add(new Location(w, -2506.965D, 17.0D, 716.968D));
            tp.add(new Location(w, -2498.807D, 17.0D, 686.174D));
            tp.add(new Location(w, -2498.807D, 17.0D, 686.174D));
            tp.add(new Location(w, -2443.948D, 17.0D, 709.019D));
            tp.add(new Location(w, -2443.948D, 17.0D, 709.019D));
            tp.add(new Location(w, -2413.032D, 17.0D, 716.982D));
            tp.add(new Location(w, -2413.032D, 17.0D, 716.982D));
            tp.add(new Location(w, -2436.006D, 17.0D, 772.045D));
            tp.add(new Location(w, -2436.006D, 17.0D, 772.045D));
        }
       
        public List<String> sortPlayers(List <String> strings) {
            Collections.sort(strings, new Comparator<String>() {
                public int compare(String o1, String o2) {
                    return extractInt(o1) - extractInt(o2);
                }

                int extractInt(String s) {
                    String num = s.replaceAll("\\D", "");
                    // return 0 if no digits found
                    return num.isEmpty() ? 0 : Integer.parseInt(num);
                }
            });
           
            return strings;
           
        }

        public static void teleportPlayers() {
            Locations L = new Locations();
            List<String> ppl = new ArrayList<>();
            // for (int i = 0; i < ArenaBrawl.getInstance().playersList.size(); i++)
            // {
            // Player player = (Player) ArenaBrawl.getInstance().playersList.get(i);
            // Location tps = (Location) tp.get(i);
            // player.teleport(tps);
            // }

            Rating r = new Rating();
            for (int i = 0; i < ArenaBrawl.getInstance().playersList.size(); i++) {
                Player player = (Player) ArenaBrawl.getInstance().playersList.get(i);
                ppl.add(player.getName() + "*" + r.getRating(player.getName()));
            }
           
            ppl = L.sortPlayers(ppl);
           
            for (int i = 0; i < ArenaBrawl.getInstance().playersList.size(); i++) {
                String[] lol = ppl.get(i).split("\\*");
               
                Player p = Bukkit.getServer().getPlayer(ppl.get(i).replace(lol[1], "").replace("\\*", ""));
                Bukkit.getLogger().info("lol1 " + lol[1]);
                Bukkit.getLogger().info("ppl stuff " + ppl.get(i).replace(lol[1], "").replaceAll("\\*", ""));
               
                Location tps = (Location) tp.get(i);
                p.teleport(tps);
            }
           
           

        }
    }
     
    Player p = Bukkit.getServer().getPlayer(ppl.get(i).replace(lol[1], "").replace("\\*", ""));
    The player shouldn't be null because in the .getPlayer() the string isn't null, and the player is on the server.
     
  2. You should place this in the development section ;)
     
  3. Sorry, I'm new to this. Do you know how I can move it?

    EDIT: do I repost this in the right section?
     
  4. I don't think that's possible, but you can report it to let a staff member move it :p
     
    • Useful Useful x 1
  5. If you get an NPE on that line, that means that the "ppl", the "ppl.get(i)" or the "lol[1]" variable is null.
    Since an error is not thrown already on this line "String[] lol = ppl.get(i).split("\\*");", "ppl" and "ppl.get(i)" are not null.
    Are you sure the "lol" array contains at least two elements? (Indexing starts with 0)
     
    • Like Like x 1
  6. Okay, I changed the getPlayer parameter to lol[0] and it worked for some reason. Thanks for all the help thought!
     
  7. An array's, list's first element is at its 0th index. This is very important.
     
    • Like Like x 1
  8. mathhulk

    mathhulk Retired Moderator
    Retired

    Moved to Spigot Plugin Development.
     
    • Useful Useful x 1