Development Assistance java.lang.nullpointerException

Discussion in 'Plugin Help/Development/Requests' started by 0verFull, Feb 14, 2015.

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

    0verFull

    Hello ,
    I'm coding a bukkit plugin but when I put it on my server and I turn on the server, the plugin don't load and I have this exception :
    Code:
    Could not load 'plugins/EnderDragonQuest.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-c46583f-e6f9fd4]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-c46583f-e6f9fd4]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-c46583f-e6f9fd4]
        at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugins(CraftServer.java:290) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-c46583f-e6f9fd4]
        at net.minecraft.server.v1_8_R1.DedicatedServer.init(DedicatedServer.java:152) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-c46583f-e6f9fd4]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:494) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-c46583f-e6f9fd4]
        at java.lang.Thread.run(Thread.java:722) [?:1.7.0]
    Caused by: java.lang.NullPointerException
        at yolo.overfull.edq.main.<init>(main.java:26) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[?:1.7.0]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.7.0]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525) ~[?:1.7.0]
        at java.lang.Class.newInstance0(Class.java:372) ~[?:1.7.0]
        at java.lang.Class.newInstance(Class.java:325) ~[?:1.7.0]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-c46583f-e6f9fd4]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-c46583f-e6f9fd4]
        ... 6 more
    But the plugin load and work correctly when I use the command /reload.
    How to fix it ?
     
    Last edited by a moderator: Feb 14, 2015
  2. Offline

    NepsName

    You can easily see that the null pointer exception is happening in the Main class, in line 26, so the bug is happening there.

    What it means is that you are trying to apply changes or execute something in a value that is null. What you probably did was retrieve some information like a player name, but it returned null, aka it did not find what you asked for, so it applied the value null to that variable.

    When later, at line 26, you tried to do something with that variable, it gave a null pointer exception, because the value was null.

    A solution for this would be to exclude the variable or give it a default value everytime it is null.
     
  3. Offline

    timtower Administrator Administrator Moderator

    Moved to Bukkit alternatives, as development assistance.
     
  4. Offline

    0verFull

    But the line 26 it's : Scoreboard sb = this.sbm.getNewScoreboard();
    What can I do?
     
    Last edited: Feb 14, 2015
  5. Offline

    NepsName

    this.sbm.getNewScoreboard(); is returning a null value, you gotta check exactly where it is coming from, because it is not working.
     
  6. Offline

    0verFull

    It's to create scoreboard.
     
  7. Offline

    pie_flavor

    @0verFull Why do you need to getNewScoreboard()? Just do Bukkit.getScoreboardManager().getMainScoreboard().
     
  8. Offline

    teej107

    If what you said is accurate, sbm is null.
     
  9. Offline

    0verFull

    package yolo.overfull.timer;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Server;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;

    public class main
    extends JavaPlugin
    implements Listener
    {
    ScoreboardManager manager = Bukkit.getScoreboardManager();
    Scoreboard board = this.manager.getNewScoreboard(); <=The problem
    Objective o = this.board.registerNewObjective("Nom", "morts");
    int task;
    public void onEnable()
    {
    PluginManager pm = Bukkit.getServer().getPluginManager();
    pm.registerEvents(this, this);

    ScoreBoard();
    Timer();
    }
    public void onDisable()
    {
    getConfig().set("jours", Integer.valueOf(this.jours));
    getConfig().set("heures", Integer.valueOf(this.heures));
    getConfig().set("minutes", Integer.valueOf(this.minutes));
    getConfig().set("secondes", Integer.valueOf(this.secondes));
    saveConfig();
    }
    @EventHandler
    public void onClick(PlayerMoveEvent e)
    {
    final Player p = e.getPlayer();
    Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable()
    {
    public void run()
    {
    main.this.o.getScore(p).setScore((int)p.getHealth());
    }
    }, 0L, 1L);
    }
    @EventHandler
    public void playerjoin(PlayerJoinEvent e)
    {
    Bukkit.getServer().reload();
    }
    public void ScoreBoard()
    {
    this.o.setDisplaySlot(DisplaySlot.SIDEBAR);
    for (Player online : Bukkit.getOnlinePlayers()) {
    online.setScoreboard(this.board);
    }
    }
    int secondes = getConfig().getInt("secondes");
    int minutes = getConfig().getInt("minutes");
    int heures = getConfig().getInt("heures");
    int jours = getConfig().getInt("jours");
    boolean Tache;
    public void Timer()
    {
    this.task = Bukkit.getServer().getScheduler()
    .scheduleAsyncRepeatingTask(this, new Runnable()
    {
    public void run()
    {
    if (main.this.secondes == 60)
    {
    main.this.minutes += 1;
    main.this.secondes = 0;
    main.this.o.setDisplayName(ChatColor.GOLD + "-----" +
    ChatColor.BLUE + main.this.jours + " : " + main.this.heures +
    " : " + main.this.minutes + " : " + main.this.secondes +
    ChatColor.GOLD + "-----");
    for (Player online : Bukkit.getOnlinePlayers())
    {
    Score score = main.this.o.getScore(online);
    score.setScore(0);
    }
    }
    if (main.this.minutes == 60)
    {
    main.this.heures += 1;
    main.this.minutes = 0;
    main.this.o.setDisplayName(ChatColor.GOLD + "-----" +
    ChatColor.BLUE + main.this.jours + " : " + main.this.heures +
    " : " + main.this.minutes + " : " + main.this.secondes +
    ChatColor.GOLD + "-----");
    for (Player online : Bukkit.getOnlinePlayers())
    {
    Score score = main.this.o.getScore(online);
    score.setScore(0);
    }
    }
    if (main.this.heures == 24)
    {
    main.this.jours += 1;
    main.this.heures = 0;
    main.this.o.setDisplayName(ChatColor.GOLD + "-----" +
    ChatColor.BLUE + main.this.jours + " : " + main.this.heures +
    " : " + main.this.minutes + " : " + main.this.secondes +
    ChatColor.GOLD + "-----");
    for (Player online : Bukkit.getOnlinePlayers())
    {
    Score score = main.this.o.getScore(online);
    score.setScore(0);
    }
    }
    if ((!main.this.Tache) &&
    (main.this.secondes != 60))
    {
    main.this.secondes += 1;

    main.this.o.setDisplayName(ChatColor.GOLD + "-----" +
    ChatColor.BLUE + main.this.jours + " : " +
    main.this.heures + " : " + main.this.minutes + " : " +
    main.this.secondes + ChatColor.GOLD + "-----");
    for (Player online : Bukkit.getOnlinePlayers())
    {
    Score score = main.this.o.getScore(online);
    score.setScore(0);
    }
    }
    }
    }, 0L, 20L);
    }
    }

    Admin can you delete this message please?
     
    Last edited: Feb 17, 2015
  10. Offline

    pie_flavor

    @0verFull why do you need a new scoreboard and not the main scoreboard?
     
  11. Offline

    0verFull

    It's to create timer. I have tested with MainScoreboard the same error is occured. If You want test with my code (copy-paste it)
     
  12. Offline

    pie_flavor

    @0verFull First of all, you don't need this.something and you certainly don't need main.this.something. Second, you can use the main scoreboard for a timer, why wouldn't you? Third, a pastebin or something with correctly indented code would be awesome, as we certainly can't comprehend the monoindent code you've posted.
     
  13. Offline

    0verFull

  14. Offline

    pie_flavor

    @0verFull Ah ha. Found the problem. Say Scoreboard board; and ScoreboardManager manager;, and then put manager = Bukkit.getScoreboardManager() and board = manager.getNewScoreboard() in your onEnable(). Those other things get initialized the same time as the constructor, and Bukkit doesn't necessarily exist yet, you have to wait for all Bukkit-related stuff until onEnable().
     
  15. Offline

    0verFull

    Can I have lines of code to be sure . And the timer work correctly after this?
     
  16. Offline

    pie_flavor

    @0verFull
    Yes, the timer should work correctly.
    And for examples:
    You are saying this:
    Code:
    //other things
    ScoreboardManager manager = Bukkit.getScoreboardManager();
    Scoreboard board = manager.getNewScoreboard();
    Change to this:
    Code:
    //other things
    ScoreboardManager manager;
    Scoreboard board;
    public void onEnable() {
    //other things
        manager = Bukkit.getScoreboardManager();
        board = manager.getNewScoreboard();
    }
    Because the Bukkit class doesn't exist yet and neither does the ScoreboardManager until onEnable(),
     
Thread Status:
Not open for further replies.

Share This Page