Solved Problem With Config, NullPointerException.

Discussion in 'Spigot Plugin Development' started by Dorian349, Jul 5, 2018.

  1. Hey SpigotMc Community,

    I want to catch name from the config but i have a Java.NullPointerException.

    Main Class:
    Code (Text):
    package fr.dorian349.thebridge;

    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    import fr.dorian349.thebridge.api.TheBridgeState;
    import fr.dorian349.thebridge.commands.TheBridgeCommand;
    import fr.dorian349.thebridge.sql.SqlTheBridge;
    import fr.dorian349.thebridge.sql.SqlTheBridgeBooster;
    import fr.dorian349.thebridge.utils.TheBridgeTitles;
    import fr.dorian349.thebridge.utils.ScoreBoardSign;

    public class TheBridge extends JavaPlugin{
       
        private List<TheBridgeTeam> teams = new ArrayList<>();
        public List<Player> playerNumber = new ArrayList<>();
        private TheBridgeState current;
        public TheBridgeTitles title = new TheBridgeTitles();
        public static SqlTheBridge sqlthebridge;
        public static SqlTheBridgeBooster sqlthebridgebooster;

        public Map<Player, ScoreBoardSign> boards = new HashMap<>();
       
        @Override
        public void onEnable() {
           
            saveDefaultConfig();
            current = TheBridgeState.WAITING;
           
            getCommand("thebridge").setExecutor(new TheBridgeCommand(this));
            getServer().getPluginManager().registerEvents(new TheBridgeListener(this), this);
            if(getConfig().getBoolean("MySQL.Enable") == true){
                sqlthebridge = new SqlTheBridge("jdbc:mysql://", getConfig().getString("MySQL.Host"), getConfig().getInt("MySQL.Port"), getConfig().getString("MySQL.DataBase"), getConfig().getString("MySQL.User"), getConfig().getString("MySQL.Password"));
            }
            if(getConfig().getBoolean("Booster.Enable") == true){
                sqlthebridgebooster = new SqlTheBridgeBooster("jdbc:mysql://", getConfig().getString("MySQL.Host"), getConfig().getInt("MySQL.Port"), getConfig().getString("MySQL.DataBase"), getConfig().getString("MySQL.User"), getConfig().getString("MySQL.Password"));
            }
           
            ConfigurationSection sectionteams = getConfig().getConfigurationSection("teams");
            for(String team : sectionteams.getKeys(false)){
               
                String name = sectionteams.getString(team + ".name");
                String tag = sectionteams.getString(team + ".color");
                String spawn = sectionteams.getString(team + ".spawn");
               
                String[] spawns = spawn.split(",");
                double x = Double.valueOf(spawns[0]);
                double y = Double.valueOf(spawns[1]);
                double z = Double.valueOf(spawns[2]);
                float yaw = Float.valueOf(spawns[3]);
                float pitch = Float.valueOf(spawns[4]);
                World world = Bukkit.getWorld("world");
               
                byte data = (byte) sectionteams.getInt(team + ".data");
                teams.add(new TheBridgeTeam(name, tag, data, new Location(world,x,y,z,yaw,pitch)));
               
            }
        }
    My Config:
    Code (Text):
    Team-Message:
      Team: "&f&lTeam §6▪ "
      Member-Of-The-Team: "&7Member of the Team:"
      Free-Place: "&6» &8[Free place]"
      Click-To-Join: "&6»» &aClick to join"
    TheBridgeTeam Class:
    Code (Text):
    package fr.dorian349.thebridge;
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    public class TheBridgeTeam {
     
        private static TheBridge main;
        @SuppressWarnings("static-access")
        public TheBridgeTeam(TheBridge theBridge) {
            this.main = theBridge;
        }
        private Location spawn;
        private String name;
        private String tag;
        private byte data;
        private List<Player> players = new ArrayList<>();
        String teamname = main.getConfig().getString("Team-Message.Team");
        String memberofteam = main.getConfig().getString("Team-Message.Member-Of-The-Team");
        String freeplace = main.getConfig().getString("Team-Message.Free-Place");
        String clicktojoin = main.getConfig().getString("Team-Message.Click-To-Join");
     
     
        public TheBridgeTeam(String name, String tag, byte data, Location spawn) {
            this.name = name;
            this.tag = tag;
            this.data = data;
            this.spawn = spawn;
        }
        public ItemStack getItem(){
         
            StringBuilder msg = new StringBuilder();
            msg.insert(0, getPlayers().toString());
            msg.deleteCharAt(msg.length() - 1);
            msg.deleteCharAt(msg.length() - 1);
            msg.delete(0, 18);
         
            ItemStack item = new ItemStack(Material.WOOL, players.size(), data);
            ItemMeta m = item.getItemMeta();
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(teamname.replaceAll("&", "§"));
            lore.add("§f ");
            if(players.size() == 1) {
                lore.add("§6» §b" + msg);
            }
            if(players.size() < 1) {
                lore.add(memberofteam.replaceAll("&", "§"));
            }
            lore.add("§f ");
            lore.add(freeplace.replaceAll("&", "§"));
            m.setLore(lore);
            m.setDisplayName(tag + name);
            item.setItemMeta(m);
            return item;
         
        }

    Error Console:
    Code (Text):
    java.lang.NullPointerException
            at fr.dorian349.thebridge.TheBridgeTeam.<init>(TheBridgeTeam.java:26) ~[?:?]
            at fr.dorian349.thebridge.TheBridge.onEnable(TheBridge.java:67) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_171]
    Thanks for you help :)
     
  2. Your initialisation of the "TheBridgeTeam" constructor is causing the NullPointerException.
     
    • Like Like x 2
  3. And how i can initialise it, because i use a method that work for other class. :(
     
  4. What is your row at
    ? If you show us, we might be able to help.
     
  5. You can just look at the code he provided. But I'll help you out.

    TheBridge.java:67
    Code (Java):
    teams.add(new TheBridgeTeam(name, tag, data, new Location(world,x,y,z,yaw,pitch)));
    TheBridgeTeam.java:26
    Code (Java):
    public TheBridgeTeam(String name, String tag, byte data, Location spawn) {
            this.name = name;
            this.tag = tag;
            this.data = data;
            this.spawn = spawn;
        }
     
    • Like Like x 1
  6. Ow I see now, did you reload your server? This is not a very good idea and breaks a lot of code. Just restart the server when you make updates.
     
    • Like Like x 1
  7. Its probably better if I teach you how to debug. Do you use intelliJ?
     
  8. No he didn't. TheBridgeTeam is not his main class. Consider actually reading the code he posted.
     
  9. Sure, I was looking though his error and saw this.

     
  10. I never use it :p
    Ok, i try without restart when i update the code and i have always the same error :c
     
  11. I try this :

    Code (Text):
        public TheBridgeTeam(String name, String tag, byte data, Location spawn, TheBridge theBridge) {
            this.name = name;
            this.tag = tag;
            this.data = data;
            this.spawn = spawn;
            this.main = theBridge;
        }
    Code (Text):
    teams.add(new TheBridgeTeam(name, tag, data, new Location(world,x,y,z,yaw,pitch), this));
    But i have always the same error :c
     
    • Like Like x 1
  12. I'm not exactly sure, but I would think that your problem is cause some/all of the values you put into that constructor are null. You should look into enabling remote debugging on your test server. That way you can add a break point and figure it out.

    https://www.eclipse.org/jetty/documentation/9.4.x/enable-remote-debugging.html (dunno if that is the right tutorial as ive never used eclipse)
     
  13. MiniDigger

    Supporter

    your design of the TheBridgeTeam class is fundamentally flawed.

    you have two constructors, one that takes a plugin instance and one that takes actual values. I would guess that the first one is not necessary.
    you also load the messages for every team instance, that seems unnecessary. I would store the messages in static fields, initialized by a static init method which takes your plugin instance.
    that would remove the NPE, as thats caused by your fields trying to access the plugin instance before it has been assigned (in fact, it never gets assigned), and it would lead to an overall cleaner design.
     
  14. Thanks but second construtor is for team loader and it works

    Code (Text):
    teams:
      red:
        name: "Red Team"
        color: "§c"
        data: 14
        spawn: 28.5,75,0.5,90,-4
      green:
        name: "Green Team"
        color: "§a"
        data: 5
        spawn: -30.5,75,-1.5,-90,-2
      orange:
        name: "Orange Team"
        color: "§6"
        data: 1
        spawn: 28.5,79,-24.5,47.5,2
      cyan:
        name: "Cyan Team"
        color: "§b"
        data: 9
        spawn: -22.5,78,24.5,-135,2
    This a the second part of the config.

    ok, i will try that

    How do you write that? (I only start java 1 month ago :p)
     
    #14 Dorian349, Jul 5, 2018
    Last edited: Jul 5, 2018
    • Like Like x 1
  15. MiniDigger is way better at java/dealing with spigot plugins than me. I suggest doing what he says. But setting up remote debugging is still something you should do, as its incredibly helpful!
     
    • Like Like x 1
  16. I resolve my problem!!!!!!!!
    I follow your instruction @MiniDigger
    Code (Text):
        @Override
        public void onEnable() {
         
            saveDefaultConfig();
            current = TheBridgeState.WAITING;
         
            getCommand("thebridge").setExecutor(new TheBridgeCommand(this));
            getServer().getPluginManager().registerEvents(new TheBridgeListener(this), this);
            if(getConfig().getBoolean("MySQL.Enable") == true){
                sqlthebridge = new SqlTheBridge("jdbc:mysql://", getConfig().getString("MySQL.Host"), getConfig().getInt("MySQL.Port"), getConfig().getString("MySQL.DataBase"), getConfig().getString("MySQL.User"), getConfig().getString("MySQL.Password"));
            }
            if(getConfig().getBoolean("Booster.Enable") == true){
                sqlthebridgebooster = new SqlTheBridgeBooster("jdbc:mysql://", getConfig().getString("MySQL.Host"), getConfig().getInt("MySQL.Port"), getConfig().getString("MySQL.DataBase"), getConfig().getString("MySQL.User"), getConfig().getString("MySQL.Password"));
            }
         

         
            ConfigurationSection sectionteams = getConfig().getConfigurationSection("teams");
            for(String team : sectionteams.getKeys(false)){
             
                String name = sectionteams.getString(team + ".name");
                String tag = sectionteams.getString(team + ".color");
                String spawn = sectionteams.getString(team + ".spawn");
                String teamname = this.getConfig().getString("Team-Message.Team");
                String memberofteam = this.getConfig().getString("Team-Message.Member-Of-The-Team");
                String freeplace = this.getConfig().getString("Team-Message.Free-Place");
                String clicktojoin = this.getConfig().getString("Team-Message.Click-To-Join");
             
                String[] spawns = spawn.split(",");
                double x = Double.valueOf(spawns[0]);
                double y = Double.valueOf(spawns[1]);
                double z = Double.valueOf(spawns[2]);
                float yaw = Float.valueOf(spawns[3]);
                float pitch = Float.valueOf(spawns[4]);
                World world = Bukkit.getWorld("world");
             
                byte data = (byte) sectionteams.getInt(team + ".data");
                teams.add(new TheBridgeTeam(name, tag, data, new Location(world,x,y,z,yaw,pitch), teamname, memberofteam, freeplace, clicktojoin));
             
            }
        }
    Code (Text):
        public TheBridgeTeam(String name, String tag, byte data, Location spawn, String teamname, String memberofteam, String freeplace, String clicktojoin) {
            this.name = name;
            this.tag = tag;
            this.data = data;
            this.spawn = spawn;
            this.teamname = teamname;
            this.memberofteam = memberofteam;
            this.freeplace = freeplace;
            this.clicktojoin = clicktojoin;
        }
    Ty so much @MiniDigger, @gabber200 and @AgainstTheNight
     
    #16 Dorian349, Jul 5, 2018
    Last edited: Jul 5, 2018
    • Like Like x 2
  17. MiniDigger

    Supporter

    Almost, I would still move your config calls out of the loop.
     
    • Agree Agree x 2