java.lang.NullPointerException Error

Discussion in 'Plugin Development' started by MnMaxon, Jul 22, 2012.

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

    MnMaxon

    I keep getting this error on my plugin:
    Code:
    10:49:10 AM [SEVERE] java.lang.NullPointerException
    10:49:10 AM [SEVERE]    at me.MnMaxon.TheWalls.Main.timer1(Main.java:289)
    10:49:10 AM [SEVERE]    at me.MnMaxon.TheWalls.Main.startGame(Main.java:372)
    10:49:10 AM [SEVERE]    at me.MnMaxon.TheWalls.Main$1.run(Main.java:138)
    10:49:10 AM [SEVERE]    at org.bukkit.craftbukkit.scheduler.CraftWorker.run(CraftWorker.java:34)
    10:49:10 AM [SEVERE]    at java.lang.Thread.run(Unknown Source)
    It stops my plugin from working, and I would really appreciate it if someone could help.

    Here's my main class:
    package me.MnMaxon.TheWalls;
    Code:
    package me.MnMaxon.TheWalls;
     
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.List;
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
    public final Logger logger = Logger.getLogger("Minecraft");
    public static Main plugin;
    public static int joined = 0;
    public static int readied = 0;
    public static int mins = 14;
    public static int secs = 59;
    public int tpWhere = 1;
    public static int xx = 285;
    public static int zz = -855;
    public static int nuggets = 0;
    public final MyPlayerListener pl = new MyPlayerListener();
    public static int game = 0;
    public static int thirtySecsLeft;
    public static World worldWalls = Bukkit.getServer().getWorld("Official");
    public static World worldMain = Bukkit.getServer().getWorld("world");
     
    // if world is loading = 2
    // if game is on = 1
    // if game is off = 0
     
    @Override
    public void onEnable() {
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(this.pl, this);
    resetMap();
    }
     
    public boolean onCommand(CommandSender sender, Command cmd,
    String commandLabel, String[] args) {
    Player player = (Player) sender;
    double px = player.getLocation().getX();
    double pz = player.getLocation().getZ();
    double py = player.getLocation().getY();
    if (commandLabel.equalsIgnoreCase("TW")) {
    if (args.length == 0) {
    help(sender, cmd, commandLabel, args);
    } else if (args.length == 1) {
    if (args[0].equalsIgnoreCase("join")) {
    if (game == 0) {
    if (player.getWorld() != Bukkit.getServer().getWorld(
    "Walls")) {
    joined++;
    Location to = new Location(Bukkit.getServer()
    .getWorld("Walls"), 256, 149, -793);
    player.teleport(to);
    if (joined != 2) {
    player.sendMessage(ChatColor.GREEN
    + "You have joined TheWalls, "
    + (joined - 1)
    + " other players are also waiting for the game to start! Type"
    + ChatColor.LIGHT_PURPLE
    + " /TW Ready " + ChatColor.GREEN
    + "to ready up!");
    }
    if (joined == 1) {
    Bukkit.broadcastMessage("["
    + ChatColor.DARK_PURPLE + "TheWalls"
    + ChatColor.WHITE + "]"
    + ChatColor.AQUA
    + player.getDisplayName()
    + " has joined a new game!");
    } else if (joined == 2) {
    player.sendMessage(ChatColor.GREEN
    + "You have joined TheWalls, 1 other player is also waiting for the game to start! Type"
    + ChatColor.LIGHT_PURPLE
    + " /TW Ready " + ChatColor.GREEN
    + "to ready up!");
    }
    } else if (game == 1) {
    player.sendMessage(ChatColor.DARK_RED
    + "A game is already in progress!");
    } else if (game == 2) {
    player.sendMessage(ChatColor.DARK_RED
    + "The map is reloading!");
    }
    }
    } else if (args[0].equalsIgnoreCase("time")) {
    TimeCmd(sender, cmd, commandLabel, args);
    } else if (args[0].equalsIgnoreCase("ready")) {
    if (py > 148 && py < 152 && pz > -800 && pz < -786
    && px > 252 && px < 257) {
    readied++;
    Location readyLocation = new Location(Bukkit
    .getServer().getWorld("Walls"), 251, 149, -793);
    player.teleport(readyLocation);
    player.sendMessage(ChatColor.GREEN
    + "You have readied up for TheWalls!");
    } else {
    player.sendMessage(ChatColor.DARK_RED + "type "
    + ChatColor.LIGHT_PURPLE + "/TW JOIN"
    + ChatColor.DARK_RED + " first! :(");
    }// TODO
    if ((joined == 2 && readied == 2)
    || ((joined >= 3 && (readied / joined) >= 0.5))) {
    thirtySecsLeft = 24;
    Bukkit.broadcastMessage("[" + ChatColor.DARK_PURPLE
    + "TheWalls" + ChatColor.WHITE + "]"
    + ChatColor.AQUA
    + " A new game will start in 30 seconds! Type "
    + ChatColor.LIGHT_PURPLE + "/TW Join"
    + ChatColor.AQUA + " to join the game!");
    Bukkit.getServer()
    .getScheduler()
    .scheduleAsyncRepeatingTask(this,
    new Runnable() {
    public void run() {
    if (thirtySecsLeft < 31) {
    thirtySecsLeft++;
    if (thirtySecsLeft == 10
    || thirtySecsLeft == 20
    || thirtySecsLeft == 5
    || thirtySecsLeft == 15
    || thirtySecsLeft == 25) {
    Bukkit.broadcastMessage(ChatColor.AQUA
    + ""
    + (30 - thirtySecsLeft));
    }
    if (thirtySecsLeft == 30) {
    startGame();
    }
    }
    }
    }, 0L, 20L);
    }
    } else if (args[0].equalsIgnoreCase("reset") && player.isOp()) {
    resetMap();
    } else if (args[0].equalsIgnoreCase("joined")) {
    player.sendMessage(joined + " players have joined, and "
    + readied + " players are ready.");
    } else if (args[0].equalsIgnoreCase("start")) {
    joined = 2;
    readied = 1;
    } else {
    help(sender, cmd, commandLabel, args);
    player.sendMessage(ChatColor.DARK_PURPLE + "/"
    + commandLabel + " " + args[0] + ChatColor.DARK_RED
    + " IS NOT A VALID COMMAND!");
    }
    }
    }
    return false;
    }
     
    public void tpToStart() {
    Location oneTo = new Location(worldWalls, 286, 116, -854);
    Location twoTo = new Location(worldWalls, 286, 116, -732);
    Location threeTo = new Location(Bukkit.getServer().getWorld("Walls"),
    408, 116, -854);
    Location fourTo = new Location(worldWalls, 408, 116, -732);
    List<Player> onlinePlayers = Arrays.asList(Bukkit.getServer()
    .getOnlinePlayers());
    Iterator<Player> iterator = onlinePlayers.iterator();
    while (iterator.hasNext()) {
    Player onlinePlayer = iterator.next();
    onlinePlayer.setHealth(20);
    onlinePlayer.setFoodLevel(20);
    onlinePlayer.setExp(0);
    onlinePlayer.getInventory().clear();
    onlinePlayer.getInventory().setArmorContents(
    new ItemStack[onlinePlayer.getInventory()
    .getArmorContents().length]);
    // deletes armor
    if (tpWhere == 1) {
    onlinePlayer.teleport(oneTo);
    tpWhere++;
    } else if (tpWhere == 2) {
    onlinePlayer.teleport(twoTo);
    tpWhere++;
    } else if (tpWhere == 3) {
    onlinePlayer.teleport(threeTo);
    tpWhere++;
    } else if (tpWhere == 4) {
    onlinePlayer.teleport(fourTo);
    tpWhere = 1;
    }
    }
    }
     
    public boolean help(CommandSender sender, Command cmd, String commandLabel,
    String[] args) {
    Player player = (Player) sender;
    player.sendMessage("----" + ChatColor.GREEN + "TheWalls Help"
    + ChatColor.WHITE + "----");
    player.sendMessage(ChatColor.AQUA + "/TW" + ChatColor.WHITE + " | "
    + ChatColor.GREEN + "Shows This Screen!");
    player.sendMessage(ChatColor.AQUA + "/TW Time" + ChatColor.WHITE
    + " | " + ChatColor.GREEN
    + "Shows you the time until the walls drop!");
    player.sendMessage(ChatColor.AQUA + "/TW Join" + ChatColor.WHITE
    + " | " + ChatColor.GREEN + "Let's you join the game!");
    player.sendMessage(ChatColor.AQUA + "/TW Ready" + ChatColor.WHITE
    + " | " + ChatColor.GREEN + "Readies you up!");
    player.sendMessage(ChatColor.AQUA + "/TW Joined" + ChatColor.WHITE
    + " | " + ChatColor.GREEN
    + "Tells you how many people have readied up/joined!");
    return false;
    }
     
    public static boolean TimeCmd(CommandSender sender, Command cmd,
    String commandLabel, String[] args) {
    Player player = (Player) sender;
    if (mins > 9 && secs > 9) {
    player.sendMessage("There is" + mins + ":" + secs + " left!");
    } else if (mins < 10 && secs > 9 && mins > 0) {
    player.sendMessage("There is 0" + mins + ":" + secs + " left!");
    } else if (mins > 9 && secs < 10) {
    player.sendMessage("There is " + mins + ":0" + secs + " left!");
    } else if (mins < 10 && secs < 10 && mins > 0) {
    player.sendMessage("There is " + mins + ":0" + secs + " left!");
    } else if (mins <= 0 && secs != -1) {
    player.sendMessage("There are " + secs
    + " seconds left! HURRY UP!!!!");
    } else if (mins <= 0 && secs <= 0) {
    player.sendMessage("The Walls have dropped!");
    }
    return false;
    }
     
    public static void resetMap() {
    game = 2;
    nuggets = 0;
    plugin = (Main) Bukkit.getServer().getPluginManager()
    .getPlugin("TheWalls");
    List<Player> onlinePlayers = Arrays.asList(Bukkit.getServer()
    .getOnlinePlayers());
    Iterator<Player> iterator = onlinePlayers.iterator();
    while (iterator.hasNext()) {
    Player onlinePlayer = iterator.next();
    if (onlinePlayer.getWorld() == worldWalls) {
    Bukkit.broadcastMessage("[" + ChatColor.DARK_PURPLE
    + "TheWalls" + ChatColor.WHITE + "]" + ChatColor.AQUA
    + onlinePlayer.getDisplayName() + " has won!");
    onlinePlayer.teleport(worldMain.getSpawnLocation());
    }
    }
    Bukkit.getServer().getScheduler()
    .scheduleSyncRepeatingTask(plugin, new Runnable() {
    public void run() {
    if (nuggets <= 10) {
    if (nuggets == 0) {
    unload();
    }
    nuggets++;
    } else if (nuggets <= 20 && nuggets > 10) {
    if (nuggets == 11) {
    del();
    }
    nuggets++;
    } else if (nuggets > 20 && nuggets < 30) {
    if (nuggets == 21) {
    copy();
    }
    nuggets++;
    } else if (nuggets == 30) {
    load();
    nuggets++;
    game = 0;
    joined = 0;
    readied = 0;
    mins = 15;
    }
    }
    }, 0L, 20L);
    }
     
    public void timer1() {
    game = 1;
    mins = 14;
    secs = 59;
    worldWalls.setWaterAnimalSpawnLimit(0);
    worldWalls.setAnimalSpawnLimit(0);
    worldWalls.setMonsterSpawnLimit(0);
    Bukkit.getServer().getScheduler()
    .scheduleAsyncRepeatingTask(this, new Runnable() {
    public void run() {
    if (secs != -1 && game == 1) {
    secs--;
    if (secs <= 10 && mins == 0 && secs > 0) {
    Bukkit.broadcastMessage("["
    + ChatColor.DARK_PURPLE + "TheWalls "
    + ChatColor.WHITE + "]"
    + ChatColor.AQUA + secs + "!");
    } else if (secs == 0 && mins == 0) {
    Bukkit.broadcastMessage("["
    + ChatColor.DARK_PURPLE + "TheWalls"
    + ChatColor.WHITE + "]"
    + ChatColor.AQUA
    + "The Walls have dropped!");
    game = 0;
    } else if (mins <= 0 && secs <= 0) {
    while (xx <= 409 && zz <= -731) {
    Location delPlanksXLocation = new Location(
    Bukkit.getServer()
    .getWorld("Walls"), xx,
    112, -793);
    Location delPlanksZLocation = new Location(
    Bukkit.getServer()
    .getWorld("Walls"), 347,
    112, zz);
    delPlanksXLocation.getBlock().setType(
    Material.AIR);
    delPlanksZLocation.getBlock().setType(
    Material.AIR);
    xx++;
    zz++;
    }
    }
    } else if (secs == -1 && mins != 0) {
    secs = 59;
    if (mins != 1) {
    Bukkit.broadcastMessage("["
    + ChatColor.DARK_PURPLE + "TheWalls "
    + ChatColor.WHITE + "]"
    + ChatColor.AQUA + " There are " + mins
    + " minutes left!");
    } else {
    Bukkit.broadcastMessage("["
    + ChatColor.DARK_PURPLE + "TheWalls "
    + ChatColor.WHITE + "]"
    + ChatColor.AQUA + " There is " + mins
    + " minute left!");
    }
    mins--;
    }
    }
    }, 0L, 20L);
    // 20L = 1 second
    }
     
    public static void unload() {
    Bukkit.getServer().dispatchCommand(
    Bukkit.getServer().getConsoleSender(), "world unload Walls");
    }
     
    public static void del() {
    Bukkit.getServer().dispatchCommand(
    Bukkit.getServer().getConsoleSender(), "world delete Walls");
    }
     
    public static void copy() {
    Bukkit.getServer().dispatchCommand(
    Bukkit.getServer().getConsoleSender(),
    "world copy WallsCopy Walls");
    }
     
    public static void load() {
    Bukkit.getServer().dispatchCommand(
    Bukkit.getServer().getConsoleSender(), "world load Walls");
    }
     
    public void startGame() {
    game = 1;
    timer1();
    tpWhere = 1;
    tpToStart();
    }
    }
    
    Sorry, I don't know how to use Java Code on the forum.
     
  2. Offline

    Tempelchat

    You can't do this:
    Code:
    public static World worldWalls = Bukkit.getServer().getWorld("Official");
    It is executed when your plugin is loaded by java (and not bukkit) -> at this time there is no world loaded -> NPE.
    Assign the world in your onEnable() method. And do the same for your main world.
     
    MnMaxon likes this.
  3. Offline

    MnMaxon

    Thank you

    The problem is actually that
    Code:
    public static World worldWalls = Bukkit.getServer().getWorld("Official");
    //Should be:
    public static World worldWalls = Bukkit.getServer().getWorld("Walls");
    and
    Code:
    public static World worldMain = Bukkit.getServer().getWorld("world");
    //should be:
    public static World worldMain = Bukkit.getServer().getWorld("Official");
    Thanks anyway, you pointed it out to me.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  4. Offline

    Ayman1999

    I still don't understand how to fix it:( ,please someone explain me what to do exactly.
    I am using Java 7 update 9 (64-bit)
     
  5. Offline

    MnMaxon

    No one can help you without your code
    If you don't know what the null error means, click here
     
  6. Offline

    tom1000o

    [OMGsyntax=java]
    this is java :D
    [/syntaxOMG]

    take away the OMG, and presto:

    Code:java
    1.  
    2. this is java :D
    3.  
     
  7. Offline

    Ayman1999

    What code?

    Here is what the server type when I start the server from start.bat

    Code:
    java.lang.NullPointerException
            at java.io.Reader.<init>(Unknown Source)
            at java.io.InputStreamReader.<init>(Unknown Source)
            at net.minecraft.server.AchievementMap.<init>(SourceFile:15)
            at net.minecraft.server.AchievementMap.<clinit>(SourceFile:9)
            at net.minecraft.server.Statistic.g(SourceFile:37)
            at net.minecraft.server.CounterStatistic.g(SourceFile:15)
            at net.minecraft.server.StatisticList.<clinit>(SourceFile:30)
            at net.minecraft.server.MinecraftServer.main(MinecraftServer.java:593)
            at org.bukkit.craftbukkit.Main.main(Main.java:152)
    java.lang.ExceptionInInitializerError
            at net.minecraft.server.LocaleI18n.<clinit>(SourceFile:4)
            at net.minecraft.server.Item.t(SourceFile:492)
            at net.minecraft.server.StatisticList.a(SourceFile:142)
            at net.minecraft.server.StatisticList.c(SourceFile:88)
            at net.minecraft.server.Item.<clinit>(SourceFile:296)
            at net.minecraft.server.Block.<clinit>(Block.java:681)
            at net.minecraft.server.StatisticList.a(SourceFile:125)
            at net.minecraft.server.StatisticList.<clinit>(SourceFile:58)
            at net.minecraft.server.MinecraftServer.main(MinecraftServer.java:593)
            at org.bukkit.craftbukkit.Main.main(Main.java:152)
    Caused by: java.lang.NullPointerException
            at java.io.Reader.<init>(Unknown Source)
            at java.io.InputStreamReader.<init>(Unknown Source)
            at net.minecraft.server.LocaleLanguage.e(SourceFile:30)
            at net.minecraft.server.LocaleLanguage.<init>(SourceFile:17)
            at net.minecraft.server.LocaleLanguage.<clinit>(SourceFile:8)
            ... 10 more
    Press any key to continue . . .
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  8. Offline

    MnMaxon

    Are you trying to create a plugin, or just run a server?
     
  9. Offline

    Icyene

    Ayman1999

    This is NOT the place to ask that. That said, one of 2 things is true, in order of chance:

    • You do not have enough space for Bukkit to save its files, and so it didn't. Now its trying to read them and crashing
    • Its a Bukkit bug (VERY unlikely: the traceback points to MC failing)
    I'd go with the first. Try deleting the entire Bukkit folder and restarting Bukkit.
     
  10. Offline

    Ayman1999

    I am not creating a plugin.I just run the server in one day then i found this error.
    This error appeared in Craftbukkit 1.3.2-R3.0 Then I downloaded the 1.4.2-R0.1 in a clean folder
    with start.bat then when I started the server=>the same error appeared
     
  11. I have seen errors simulair to this when there was an ! in the path to the server, bukkit just stresses out if it sees 1, instead of just nice telling us
     
  12. Offline

    Ewe Loon

    A point about using multi worlds,

    NEVER assume they are loaded

    use code like this in you methods
    if (worldWalls == null)worldWalls = Bukkit.getServer().getWorld("Walls");
    that way it will be corrected if the world was not loaded at the time of initialization

    Ok this is what you need to do
    Change the lines

    public static World worldWalls = Bukkit.getServer().getWorld("Official");
    public static World worldMain = Bukkit.getServer().getWorld("world");

    to

    public World worldWalls;
    public World worldMain;

    add the following 2 line

    if (worldWalls == null)worldWalls = Bukkit.getServer().getWorld("Walls");
    if (worldMain == null)worldMain = Bukkit.getServer().getWorld("Official");

    to the top of your oncommand method
    and any other method you get null pointer errors in where worldWalls or worldMain is on the line that the error occurs on
    you will note, i didnt say to put them in the onEnable, that is because the worlds might not have loaded when onEnable is called (I had this problem with one of my mods)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
    ferrybig likes this.
  13. Offline

    MnMaxon

    Thank you this was solved for me a while ago, though

    This might be a problem with one of your plugins, I'm not sure if ferrybig solved your answer, I'm not sure what he was saying

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  14. Offline

    Ayman1999

    It's not a plugin i have said before that I made a folder and put the start.bat and craftbukkit.jar
    then when I start the bat the same error appeared.

    Where can i change these lines?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  15. Offline

    gamerzap

    He was talking to MnMaxon. You really should have asked for help ibn another thread on another part of the forums, because this discussion doesn't belong in Plugin Development.
     
  16. Offline

    Ayman1999

    I think the problem is from java because when I downloaded the normal vanilla server then i launch it nothing happens just loading of the mouse then java close but no windows appears or something.
    I reinstalled java no effect and i installed java 32-bit also no effect.

    Where can I find my main class?:(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  17. Offline

    gamerzap

    Why did you randomly quote my post? What you said had nothing to do with it. And what do you mean "where can I find my main class?" Main class of what?
     
  18. Offline

    Ayman1999

    The main class like MnMaxon his package... Where?
     
  19. Offline

    gamerzap

    I thought you said you weren't making a plugin...
     
  20. Offline

    Ayman1999

    Please someone help me to fix this problem I can't fix it at all!:(:(:(
     
  21. Offline

    MnMaxon

    Stop posting on this thread, ignore everything everyone has told you or you think they told you.

    Read what Icyene tried to tell you:
    Almost everyone else was trying to tell you how to make a plugin for Bukkit.
    If Icyene's post doesn't help you, post on another thread.
     
    Icyene likes this.
  22. Offline

    Ayman1999

    Then Where can I post?
     
  23. Offline

    Icyene

    Bukkit Help.....
     
Thread Status:
Not open for further replies.

Share This Page