Solved NullPointerException

Discussion in 'Plugin Development' started by StellarFX, Jun 1, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    StellarFX

    Hello,

    I'm trying to get a random coordinate between two given one. So I'm trying to get coordinates from a config.yml
    but it gives me a NullPointerException; here's my error

    Show Spoiler
    Code:
    [CatchST] Task #54 for CatchST v0.0.1 generated an exception
    java.lang.NullPointerException: null
            at fr.stellarfx.catchst.Main.startGame(Main.java:291) ~[?:?]
            at fr.stellarfx.catchst.Main$2.run(Main.java:166) ~[?:?]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:76) ~[spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:361) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]


    and here's my code at line ~291

    Show Spoiler

    Code:
    public void startGame() {
           
            String[] gz1 = this.getConfig().getString("game.coordinates.zone.1").split(",");
            String[] gz2 = this.getConfig().getString("game.coordinates.zone.2").split(",");
           
            gameZone1.setX(Integer.parseInt(gz1[0])); //This line is 291
            gameZone2.setX(Integer.parseInt(gz2[0]));
            gameZone1.setY(Integer.parseInt(gz1[1]));
            gameZone2.setY(Integer.parseInt(gz2[1]));
            gameZone1.setZ(Integer.parseInt(gz1[2]));
            gameZone2.setZ(Integer.parseInt(gz2[2]));


    aaand finally my config.yml
    Show Spoiler

    Code:
    game:
      coordinates:
        queue: -269,7,-91
        game: -270,7,-91
        zone:
          '1': -268,6,-91
          '2': -271,4,-93
      number: 3


    Please help!

    Regards,
    StellarFX
     
  2. Offline

    KarimAKL

  3. Offline

    StellarFX

    @KarimAKL I think it is

    EDIT : No it's not I tried to set it to something but it does the same error
     
  4. Offline

    KarimAKL

    @StellarFX If that's the case then is 'gameZone2' null as well? If initializing those two doesn't work, then please show your current code.
     
  5. Offline

    StellarFX

    @KarimAKL I think 'gameZone2' is also null.
    Here's my code :

    Show Spoiler

    Code:
    package fr.stellarfx.catchst;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;
    
    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.Economy;
    
    public class Main extends JavaPlugin implements Listener {
       
        private static Economy econ = null;
        String prefix = "§7[§6BR§7] §7";
        int NOP = 0;
        ArrayList<String> queue = new ArrayList<String>();
        int points;
        Location queuespawn;
        String[] queuespawn1;
        String[] gamespawn1;
        Location gamespawn;
        Location gameZone;
        Location gameZone1;
        Location gameZone2;
        World worldp;
    
        public void onEnable() {
            gamespawn1 = this.getConfig().getString("game.coordinates.game").split(",");
            queuespawn1 = this.getConfig().getString("game.coordinates.queue").split(",");
            Bukkit.getLogger().info("PLUGIN STARTED");
            loadConfiguration();
           
            if(getServer().getPluginManager().getPlugin("Vault") == null) {
                getLogger().severe("VAULT IS NOT INSTALLED");
                getLogger().severe("DISABLING THE PLUGIN");
                Bukkit.getPluginManager().disablePlugin(this);
            }
           
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
            setupEconomy();
                   
        }
       
        public void loadConfiguration() {
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }
       
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if ((sender instanceof Player)) {
                Player p = (Player)sender;
                if(cmd.getName().equalsIgnoreCase("games")) {
                    Inventory games = Bukkit.getServer().createInventory(p, 9, "Games");
                    ItemStack gp1 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 8);
                    ItemMeta meta1 = gp1.getItemMeta();
                    meta1.setDisplayName(ChatColor.BOLD + "");
                    gp1.setItemMeta(meta1);
                   
                    ItemStack gp2 = new ItemStack(Material.DIAMOND_HOE);
                    ItemMeta meta2 = gp2.getItemMeta();
                    gp2.setDurability((short) 5);
                    meta2.spigot().setUnbreakable(true);
                    meta2.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE);
                    meta2.setDisplayName(ChatColor.GOLD + "Bug Roundup");
                    ArrayList<String> lore2 = new ArrayList<String>();
                    lore2.add("§8§o(" + NOP + "/10)");
                    meta2.setLore(lore2);
                    gp2.setItemMeta(meta2);
                    games.setItem(0, gp1);
                    games.setItem(1, gp1);
                    games.setItem(2, gp1);
                    games.setItem(3, gp1);
                    games.setItem(4, gp2);
                    games.setItem(5, gp1);
                    games.setItem(6, gp1);
                    games.setItem(7, gp1);
                    games.setItem(8, gp1);
                    p.openInventory(games);
                    return true;       
                }
               
                if(cmd.getName().equalsIgnoreCase("ast")) {
                    for(int i3 = 0; i3 < Integer.parseInt(this.getConfig().getString("game.number")); i3++) {
                        Location plook = p.getTargetBlock(null, 100).getLocation().add(0, 2.5, 0);
                        ArmorStand as = (ArmorStand) plook.getWorld().spawnEntity(plook, EntityType.ARMOR_STAND);
                       
                        ItemStack bee = new ItemStack(Material.DIAMOND_HOE);
                        ItemMeta meta2 = bee.getItemMeta();
                        bee.setDurability((short) 5);
                        meta2.spigot().setUnbreakable(true);
                        meta2.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE);
                        meta2.setDisplayName(ChatColor.GOLD + "");
                        ArrayList<String> lore2 = new ArrayList<String>();
                        lore2.add("§8§o(" + NOP + "/10)");
                        meta2.setLore(lore2);
                        bee.setItemMeta(meta2);
                       
                        as.setSmall(true);
                        as.setGravity(true);
                        as.setCanPickupItems(false);
                        as.setVisible(false);
                        as.setBasePlate(false);
                        as.setInvulnerable(false);
                        as.setHelmet(bee);
                       
                        this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                            public void run() {
                                double randommex = Math.random();
                                double randommey = Math.random();
                                double randommez = Math.random();
                                Vector direction = new Vector(new Random().nextInt((int) (Math.random() + 1 + randommex)) - randommex, new Random().nextInt((int) (Math.random() + 1 + randommey)) - randommey, new Random().nextInt((int) (Math.random() + 1 + randommez)) - randommez);
                                as.setVelocity(direction);
                            }
                        }, 20, 20);
                    }
                    return true;
                }
               
               
                if(cmd.getName().equalsIgnoreCase("brup")) {
                    if(args.length == 1) {
                        if(args[0].equalsIgnoreCase("join")) {
                                if(!queue.contains(p.getName())) {
                                    queue.add(p.getName());
                                    for(int i=0; i<queue.size(); i++) {
                                        final int n = i;
                                        p.teleport(new Location(p.getWorld(), Integer.parseInt(queuespawn1[0]), Integer.parseInt(queuespawn1[1]), Integer.parseInt(queuespawn1[2])));
                                        NOP = NOP + 1;
                                        if(queue.size() >= 1) {
                                            Bukkit.getServer().getPlayer(queue.get(i)).sendTitle(ChatColor.GOLD + "The game will start", ChatColor.YELLOW + "in 20 seconds", 20, 40, 20);
                                            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                                public void run() {
                                                    Bukkit.getServer().getPlayer(queue.get(n)).sendTitle(ChatColor.GOLD + "Starting...", "", 20, 40, 20);
                                                    Bukkit.getServer().getPlayer(queue.get(n)).teleport(new Location(p.getWorld(), Integer.parseInt(gamespawn1[0]), Integer.parseInt(gamespawn1[1]), Integer.parseInt(gamespawn1[2])));
                                                    startGame();
                                                    gameZone1 = p.getLocation();
                                                    gameZone2 = p.getLocation();
                                                    p.sendMessage(gameZone1.toString());
                                                }
                                                },400L);
                                            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                                public void run() {
                                                    Bukkit.getServer().getPlayer(queue.get(n)).sendTitle(ChatColor.GOLD + "The game will start", ChatColor.YELLOW + "in 1 seconds", 20, 40, 20);
                                                }
                                                },380L);
                                            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                                public void run() {
                                                    Bukkit.getServer().getPlayer(queue.get(n)).sendTitle(ChatColor.GOLD + "The game will start", ChatColor.YELLOW + "in 2 seconds", 20, 40, 20);
                                                }
                                                },360L);
                                            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                                public void run() {
                                                    Bukkit.getServer().getPlayer(queue.get(n)).sendTitle(ChatColor.GOLD + "The game will start", ChatColor.YELLOW + "in 3 seconds", 20, 40, 20);
                                                }
                                                },340L);
                                            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                                public void run() {
                                                    Bukkit.getServer().getPlayer(queue.get(n)).sendTitle(ChatColor.GOLD + "The game will start", ChatColor.YELLOW + "in 10 seconds", 20, 40, 20);
                                                }
                                                },200L);
                                       
                                        }
                                    }
                                } else {
                                    p.sendMessage(prefix + "You are already in the queue !");
                               
                            }   
                           
                            return true;
                           
                        }
                       
                        if(args[0].equalsIgnoreCase("leave")) {
                            if(queue.contains(p.getName())) {
                                NOP = NOP - 1;
                                p.sendMessage(prefix + "You left the queue.");
                                queue.remove(p.getName());
                                p.performCommand("spawn");           
                                return true;
                            } else {
                                p.sendMessage(prefix + "Ours");
                            }
                           
                        }
                       
                        if(args[0].equalsIgnoreCase("reload")) {
                            if(p.hasPermission("brup.admin")) {
                                this.reloadConfig();
                                this.saveConfig();
                                gamespawn1 = this.getConfig().getString("game.coordinates.game").split(",");
                                queuespawn1 = this.getConfig().getString("game.coordinates.queue").split(",");
                                p.sendMessage(prefix + "Config file reloaded !");
                            }
                           
                        }
                    } else if(args.length == 2) {
                        if(args[0].equals("set")) {
                            if(args[1].equals("queue")) {
                                if(p.hasPermission("brup.admin")) {
                                    this.getConfig().set("game.coordinates.queue", Math.round(p.getLocation().getX()) + "," + Math.round(p.getLocation().getY()) + "," + Math.round(p.getLocation().getZ()));
                                    p.sendMessage(prefix + "You edited the queue spawn !");
                                    this.saveConfig();
                                    return true;
                                }
                            }
                            if(args[1].equals("game")) {
                                if(p.hasPermission("brup.admin")) {
                                    this.getConfig().set("game.coordinates.game", Math.round(p.getLocation().getX()) + "," + Math.round(p.getLocation().getY()) + "," + Math.round(p.getLocation().getZ()));
                                    p.sendMessage(prefix + "You edited the game spawn !");
                                    this.saveConfig();
                                    return true;
                                }
                            }
                        }
                    } else if(args.length == 3) {
                        if(args[0].equals("set")) {
                            if(args[1].equals("zone")) {
                                if(args[2].equals("1")) {
                                    worldp = p.getWorld();
                                    gameZone1 = p.getLocation();
                                    this.getConfig().set("game.coordinates.zone.1", Math.round(p.getLocation().getX()) + "," + Math.round(p.getLocation().getY()) + "," + Math.round(p.getLocation().getZ()));
                                    p.sendMessage(prefix + "You edited the zone 1 spawn !");
                                    this.saveConfig();
                                    return true;
                                }
                                if(args[2].equals("2")) {
                                    gameZone2 = p.getLocation();
                                    this.getConfig().set("game.coordinates.zone.2", Math.round(p.getLocation().getX()) + "," + Math.round(p.getLocation().getY()) + "," + Math.round(p.getLocation().getZ()));
                                    p.sendMessage(prefix + "You edited the zone 2 spawn !");
                                    this.saveConfig();
                                    return true;
                                }
                            }
                           
                        }
                       
                    } else {
                        p.sendMessage("§8============");
                        p.sendMessage("§6/brup join §7: Joins the queue.");
                        p.sendMessage("§6/brup set queue §7: Sets the queue spawn.");
                        p.sendMessage("§6/brup set game §7: Sets the game spawn.");
                        p.sendMessage("§8============");
                        return true;
                    }
                }
            }
            return false;
           
           
        }
       
    
        public static double getRandomIntegerBetweenRange(double min, double max){
            double x = (int)(Math.random()*((max-min)+1))+min;
            return x;
        }
       
        public void startGame() {
           
            String[] gz1 = this.getConfig().getString("game.coordinates.zone.1").split(",");
            String[] gz2 = this.getConfig().getString("game.coordinates.zone.2").split(",");
           
           
            gameZone1.setX(Integer.parseInt(gz1[0]));
            gameZone2.setX(Integer.parseInt(gz2[0]));
            gameZone1.setY(Integer.parseInt(gz1[1]));
            gameZone2.setY(Integer.parseInt(gz2[1]));
            gameZone1.setZ(Integer.parseInt(gz1[2]));
            gameZone2.setZ(Integer.parseInt(gz2[2]));
           
            for(int i = 0; i < Integer.parseInt(this.getConfig().getString("game.number")); i++) {
    
                double x = getRandomIntegerBetweenRange(gameZone1.getX(), gameZone2.getX());
                double y = getRandomIntegerBetweenRange(gameZone1.getY(), gameZone2.getY());
                double z = getRandomIntegerBetweenRange(gameZone1.getZ(), gameZone2.getZ());
                gameZone = new Location(worldp, x, y, z);
           
                ArmorStand as = (ArmorStand) gameZone.getWorld().spawnEntity(gameZone, EntityType.ARMOR_STAND);
               
                ItemStack bee = new ItemStack(Material.DIAMOND_HOE);
                ItemMeta meta2 = bee.getItemMeta();
                bee.setDurability((short) 5);
                meta2.spigot().setUnbreakable(true);
                meta2.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_UNBREAKABLE);
                meta2.setDisplayName(ChatColor.GOLD + "");
                ArrayList<String> lore2 = new ArrayList<String>();
                lore2.add("§8§o(" + NOP + "/10)");
                meta2.setLore(lore2);
                bee.setItemMeta(meta2);
                this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                    public void run() {
                        double randommex = Math.random();
                        double randommey = Math.random();
                        double randommez = Math.random();
                        Vector direction = new Vector(new Random().nextInt((int) (Math.random() + 1 + randommex)) - randommex, new Random().nextInt((int) (Math.random() + 1 + randommey)) - randommey, new Random().nextInt((int) (Math.random() + 1 + randommez)) - randommez);
                        as.setVelocity(direction);
                    }
                }, 20, 20);
            }
        }
       
        /*@EventHandler
        public void onArmorstandClick(PlayerInteractEntityEvent e) {
            Player p = e.getPlayer();
            if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
                if(p.getInventory().getItemInMainHand().equals(Material.DIAMOND_HOE)) {
                    e.getClickedBlock().breakNaturally();
                }
            }
        }*/
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getInventory().getTitle().equalsIgnoreCase("Games")) {
                if(e.getSlot() == 4 && (e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§6Bug Roundup"))) {
                    e.getWhoClicked().closeInventory();
                    Player p = (Player) e.getWhoClicked();
                    p.performCommand("brup join");
                }
                e.setCancelled(true);
            }
       
        }
    }
    
     
  6. Offline

    KarimAKL

    @StellarFX Set 'gameZone1' and 'gameZone2' to the player's location BEFORE you use the 'startGame' method.
     
  7. Offline

    StellarFX

    @KarimAKL Now I have a NullPointerException line 309

    Code:
    ArmorStand as = (ArmorStand) gameZone.getWorld().spawnEntity(gameZone, EntityType.ARMOR_STAND);

    EDIT : I did the same thing for 'worldp', now it's working. Thanks for your help !
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page