Solved java.lang.NoClassDefFoundError when calling a method from another class

Discussion in 'Plugin Development' started by genandnic, May 26, 2017.

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

    genandnic

    I'm attempting to call a method (isEnemyTerritory) I created in another class. Anyone know why I might be getting this error instead of succeeding once I place a block?

    Code:
    Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.genandnic.tutorial.event.teams.TeamZone
            at com.genandnic.tutorial.event.block.BlockPlace.onBlockPlace(BlockPlace.java:24) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[bukkit.jar:git-Spigot-b605b31-f4822eb]
            ... 18 more
    The class causing the error:
    Code:
    package com.genandnic.tutorial.event.block;
    
    import org.bukkit.Location;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    import com.genandnic.tutorial.event.teams.TeamZone;
    
    public class BlockPlace implements Listener {
    
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent event) {
            Block block = event.getBlock();
            Location location = block.getLocation();
           
            if(TeamZone.isEnemyTerritory(location)) {
                event.setCancelled(true);
            }
    
        }
           
    }
    
    The class that I'm trying to call methods from:
    Code:
    package com.genandnic.tutorial.event.teams;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.Team;
    
    public class TeamZone {
    
        static Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard();
        static ArrayList<TeamZone> zones = new ArrayList<TeamZone>();
    
        private String name;
        private Team team;
        private static Location location;
        private static int x = location.getBlockX();
        private static int z = location.getBlockZ();
        private static int radius;
        private static World world = Bukkit.getWorld("world");
        private static Location red_base = world.getSpawnLocation().add(50.0, 0.0, 0.0);
        private static Location blue_base = world.getSpawnLocation().add(-50.0, 0.0, 0.0);
    
        public TeamZone(String name, Team team, Location location, int radius) {
            this.name = name;
            this.team = team;
            this.location = location;
            this.radius = radius;
        }
       
        public static boolean isEnemyTerritory(Location location) {
            return true;
        }
    
    }
     
  2. @genandnic
    1. Stop abusing static. None of that needs to be static. Remove the static modifier for all those fields.
    2. Location is null, so you can't set X and Z to anything yet. Only set X and Z in the constructor.
    3. Set world, redbase, and bluebase in the constructor; The world may not exist when the initialization occurs, so it is better to check if it exists first before setting those values.
     
    genandnic likes this.
  3. Offline

    Horsey

    Have you used 'depend: [dependencyname]' in your plugin.yml? Bukkit could be trying to load a plugin that isn't loaded?
     
  4. Offline

    genandnic

    @Zombie_Striker @Horsey

    Thanks for the help! I've got it working. This is what I have now:
    Code:
    package com.genandnic.tutorial.event.teams;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.Team;
    
    public class TeamZone {
    
        Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard();
        static ArrayList<TeamZone> zones = new ArrayList<TeamZone>();
    
        private String name;
        private Team team;
        private Location location;
        private int x;
        private int z;
        private int radius;
        private World world;
        private Location red_base;
        private Location blue_base;
    
        public TeamZone() {
    
        }
    
        public TeamZone(String name, Team team, Location location, int radius) {
            this.name = name;
            this.team = team;
            this.location = location;
            this.x = location.getBlockX();
            this.z = location.getBlockZ();
            this.radius = radius;
        }
       
        public void LoadConfig() {
            this.world = Bukkit.getWorld("world");
            this.red_base = world.getSpawnLocation().add(-50.0, 0.0, 0.0);
            this.blue_base = world.getSpawnLocation().add(50.0, 0.0, 0.0);
            zones.add(new TeamZone("Red Castle", board.getTeam("red"), red_base, 25));
            zones.add(new TeamZone("Blue Castle", board.getTeam("blue"), blue_base, 25));
            Bukkit.getServer().broadcastMessage("Loaded zone configuration.");
        }
    
        public TeamZone getZone(Location location) {
            for (TeamZone zone : zones) {
                int distanceX = Math.abs(zone.x - location.getBlockX());
                int distanceZ = Math.abs(zone.z - location.getBlockZ());
                
                if (distanceX <= zone.radius && distanceZ <= zone.radius) {
                    return zone;
                }
            }
            return null;
        }
    
        public String getName() {
            return name;
        }
    
        public Team getTeam() {
            return team;
        }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page