Search code examples
javabukkit

Spigot Plugin 1.8.8 NullPointerEception


Here is the stack trace:

[18:54:41] [Server thread/INFO]: LonghornHD_TV_YT issued server command: /switch
[18:54:41] [Server thread/ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing      command 'switch' in plugin DerMariosServer-Lobby v1.5.5
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-550ebac-7019900]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-550ebac-7019900]
at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-550ebac-7019900]
at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot.jar:git-Spigot-550ebac-7019900]
at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot.jar:git-Spigot-550ebac-7019900]
at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-550ebac-7019900]
at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-550ebac-7019900]
at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-550ebac-7019900]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_101]
at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_101]
at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot.jar:git-Spigot-550ebac-7019900]
at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-550ebac-7019900]
at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot.jar:git-Spigot-550ebac-7019900]
at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot.jar:git-Spigot-550ebac-7019900]
at java.lang.Thread.run(Thread.java:745) [?:1.7.0_101]
Caused by: java.lang.NullPointerException
at Main.main.switch_mode_if(main.java:253) ~[?:?]
at Commands.commands.onCommand(commands.java:96) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-550ebac-7019900]
... 14 more

Here is the code of the Commands class:

if(cmd.getName().equalsIgnoreCase("switch")){
if(sender instanceof Player){
    final Player p = (Player) sender;
    if(p.hasPermission("DerMarios.Lobby.Mode.Switch")){
        if(plugin.switch_mode_if(p)){
            p.sendMessage(prefix + "§6Du gehst jetzt in den Normal Mode.");
            Bukkit.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
                
                @Override
                public void run() {
                    plugin.switch_mode(p);
                }
            }, 40);
            return true;
        }else{
            p.sendMessage(prefix + "§6Du gehst jetzt in den Bau Mode.");
            Bukkit.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
                
                @Override
                public void run() {
                    plugin.switch_mode(p);
                }
            }, 40);
            return true;
        }
    }else{
        p.sendMessage(prefix + "§6Du hast keine Permissions!");
        return true;
    }
}else{
    ConsoleCommandSender co = plugin.getServer().getConsoleSender();
    co.sendMessage(prefix + "§4Der Befehl kann nur ingame ausgeführt werden!");
    return true;
}

Here is the Code of the Main class:

package Main;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

//import Extra.oldoldNick;
import Extra.oldNick;
import Extra.TPS;
import Commands.commands;
import Items.items;
import Listener.listener_main;
import net.minecraft.server.v1_8_R1.ChatSerializer;
import net.minecraft.server.v1_8_R1.IChatBaseComponent;
import net.minecraft.server.v1_8_R1.PacketPlayOutChat;
import net.minecraft.server.v1_8_R1.PlayerConnection;

public class main extends JavaPlugin implements Listener{

private String prefix = "§8[§6System§8] ";
private static main inst;
private ConsoleCommandSender co = getServer().getConsoleSender();
public HashMap<String, String> nick = new HashMap<String, String>();
public ArrayList<String> nonicks = new ArrayList<String>();
public ArrayList<String> nicks = new ArrayList<String>();
public static ArrayList<String> doublejump = new ArrayList<String>();
private ArrayList<Player> switch_mode = new ArrayList<Player>();
//  private static float defaultSpeed = 0.2f;

public void onLoad(){
    co.sendMessage(prefix + "Das LobbyPlugin wurde geladen");
}

@SuppressWarnings("unchecked")
public void onEnable(){
    File mk = new File("plugins//DerMarios//Lobby");
    final File data = new File("plugins//DerMarios//Lobby//data.yml");
    File data2 = new File("plugins//DerMarios//nick_names.yml");
    commands cm = new commands(this);
    getCommand("gmc").setExecutor(cm);
    getCommand("check").setExecutor(cm);
    getCommand("nick").setExecutor(cm);
    getCommand("farben").setExecutor(cm);
    getCommand("zeichen").setExecutor(cm);
    getCommand("ping").setExecutor(cm);
    getCommand("switch").setExecutor(cm);
    if(!(mk.mkdir())){
        mk.mkdirs();
    }
    if(!(data.exists())){
        try {
            data.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if(!(data2.exists())){
        try {
            data2.createNewFile();
        } catch(IOException e){
            e.printStackTrace();
        }
    }
    new oldNick(this);
    FileConfiguration cg = YamlConfiguration.loadConfiguration(data);
    FileConfiguration cg2 = YamlConfiguration.loadConfiguration(data2);
    nonicks.add("GommeHD");
    nonicks.add("ungespielt");
    nonicks.add("LonghornHD_TV_YT");
    nonicks.add("PasMinecrafter");
    nonicks.add("DerMarios");
    nonicks.add("LonghornHD");
    if(cg2.contains("Nicks")){
        nicks = (ArrayList<String>) cg2.getList("Nicks");
    }else{
        nicks.add("Bruiser");
        nicks.add("Punisher");
        nicks.add("Heavy Hands");
        nicks.add("Knock Out");
        nicks.add("Studd");
        nicks.add("Punk");
        nicks.add("Wild Thing");
        nicks.add("Superhuman");
        nicks.add("Disgusting");
        nicks.add("Filthy");
        nicks.add("Dangerous");
        nicks.add("Desiel");
        nicks.add("Strong");
        nicks.add("Amazing");
        nicks.add("Excellent");
        nicks.add("Big Bang");
        nicks.add("Ankle Breaker");
        nicks.add("The Freak");
        nicks.add("Riot Boy");
        nicks.add("Killer");
        nicks.add("Super Human");
        nicks.add("The Amazon");
        nicks.add("Beautiful");
        nicks.add("El Diablo");
        nicks.add("Snow Man");
        nicks.add("Hopeless");
        nicks.add("Renegade");
        nicks.add("Blue Print");
        nicks.add("The Ruler");
        nicks.add("Friend or Foe");
        nicks.add("The Prisident");
        nicks.add("The Politican");
        nicks.add("Cupid");
        nicks.add("The Jokester");
        nicks.add("The Chokester");
        nicks.add("Bone Snapper");
        nicks.add("The Dentist");
        nicks.add("TheBoneRealing");
        nicks.add("Emotionless");
        nicks.add("The Bloody");
        nicks.add("Famous");
        nicks.add("Rich");
        nicks.add("Street Chemist");
        nicks.add("The Ghost");
        nicks.add("The Battler");
        nicks.add("TheCigarSmoker");
        nicks.add("Sugar");
        nicks.add("The Dancer");
        nicks.add("Moon Walker");
        nicks.add("Grown Up");
        nicks.add("Shorty");
        nicks.add("Fast and Lound");
        nicks.add("The Strom");
        nicks.add("End of the Line");
        cg2.set("Nicks", nicks);
        try {
            cg2.save(data2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if(cg.contains("No-Nicks")){
        nonicks = (ArrayList<String>) cg.getList("No-Nicks");
    }
    cg.set("No-Nicks", nonicks);
    try {
        cg.save(data);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new TPS(), 0L, 1L);
    co.sendMessage(prefix + "§eDas LobbyPlugin wurde erfolgreich geladen!");
    new listener_main(this);
    new items(this);
    new oldNick(this);
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
        
        @Override
        public void run() {
            for(Player OnlinePlayer : Bukkit.getOnlinePlayers()){
                FileConfiguration cfg = YamlConfiguration.loadConfiguration(data);
                if(cfg.contains("Freunde." + OnlinePlayer.getUniqueId() + ".Anfrage.TimeEnd")){
                    long current = System.currentTimeMillis();
                    long End = cfg.getLong("Freunde." + OnlinePlayer.getUniqueId() + ".Anfrage.TimeEnd");
                    long Test = End-current;
                    if(Test <= 0){
                        String Traget = cfg.getString("Freunde." + OnlinePlayer.getUniqueId() + ".Anfrage.Target");
                        if(Bukkit.getPlayer(Traget) != null){
                            Player T = Bukkit.getPlayer(Traget);
                            cfg.set("Freunde." + OnlinePlayer.getUniqueId() + ".Anfrage.You", null);
                            cfg.set("Freunde." + OnlinePlayer.getUniqueId() + ".Anfrage.Target", null);
                            cfg.set("Freunde." + OnlinePlayer.getUniqueId() + ".Anfrage.TimeEnd", null);
                            OnlinePlayer.sendMessage(prefix + "§6Die Zeit ist um! Die Anfrage wurde abgebrochen!");
                            T.sendMessage(prefix + "§6Die Zeit ist um! Du hattes Zeit um die Anfrage anzunehmen oder abzulehenen!");
                            try {
                                cfg.save(data);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            return;
                        }else{
                            cfg.set("Freunde." + OnlinePlayer.getUniqueId() + ".Anfrage.You", null);
                            cfg.set("Freunde." + OnlinePlayer.getUniqueId() + ".Anfrage.Target", null);
                            cfg.set("Freunde." + OnlinePlayer.getUniqueId() + ".Anfrage.TimeEnd", null);
                            OnlinePlayer.sendMessage(prefix + "§6Die Anfrage wurde abgebrochen!");
                            try {
                                cfg.save(data);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }, 1, 1);
}

public void onDisable(){
    
    
    co.sendMessage(prefix + "Das LobbyPlugin wurde erfolgreich beendet!");
    
    
}
public void sendActionBar(Player p, String m){
    if(m == null) m = "";
    m = ChatColor.translateAlternateColorCodes('&', m);
    m = m.replaceAll("%PLAYER%", p.getDisplayName());
    
    PlayerConnection con = ((CraftPlayer)p).getHandle().playerConnection;
    
    IChatBaseComponent chat = ChatSerializer.a("{\"text\": \"" + m + "\"}");
    PacketPlayOutChat packet = new PacketPlayOutChat(chat,(byte) 2);
    con.sendPacket(packet);
    
}

public static void setUltraSpeed(Player p){
    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 3));
}

public static void setSchnellerSpeed(Player p){
    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 2));
}

public static void setSchnellSpeed(Player p){
    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 1));
}

public static void setNormalSpeed(Player p){
    p.removePotionEffect(PotionEffectType.SPEED);
}

public static void switch_mode(Player p){
    if(switch_mode_if(p)){
        inst.switch_mode(p, false);
    }else{
        inst.switch_mode(p, true);
    }
}

public static boolean switch_mode_if(Player p){
    if(inst.switch_mode_code_if(p)){
        return true;
    }else{
        return false;
    }
}


public boolean switch_mode_code_if(Player p){
    if(switch_mode.contains(p)){
        GameMode c = GameMode.CREATIVE;
        p.getInventory().clear();
        p.setGameMode(c);
        return true;
    }else{
        return false;
    }
}

public void switch_mode(Player p, boolean switchmode){
    if(switchmode){
        switch_mode.add(p);
    }else{
        switch_mode.remove(p);
    }
}

public String getPrefix(){
    return prefix;
}
}

If I run the command I get the error. I have looked already two hours but unfortunately found nothing . I hope you can help me.


Solution

  • If you take a look at the stack trace, you can see that the exception was:

    Caused by: java.lang.NullPointerException
    at Main.main.switch_mode_if(main.java:253) ~[?:?]
    

    This tells you that there is a NullPointerException in the method switch_mode_if (line 253):

    public static boolean switch_mode_if(Player p){
    if(inst.switch_mode_code_if(p)){
        return true;
    }else{
        return false;
    }
    

    The only way that a NullPointerException could be thrown in this method is if inst was null (which is the case). You will notice that private static main inst; was never initialized (given a value) and thus the exception is thrown.

    To fix this, put inst = this; into the method onEnable().

    You could have solved this yourself if you knew how to read stack traces yourself: java - What is a stack trace, and how can I use it to debug my application errors?