Getting a nullpointer...

Discussion in 'Plugin Development' started by yupie_123, Aug 29, 2014.

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

    yupie_123

    Hello,

    I keep getting a nullpointer and I can't seem to fix it.

    The nullpointer happens whenever I call this method:

    Code:java
    1. public boolean exists(String arena) {
    2. if (plugin.arenadata.getConfig().contains(arena))
    3. return true;
    4. else
    5. return false;
    6. }
    7.  


    This is the code for plugin.arenadata:

    Code:java
    1. public ConfigAccessor arenadata = new ConfigAccessor(this, "arenas.yml");
    2.  


    And finally this is my ConfigAccessor class: (I copied this from https://gist.github.com/SagaciousZed/3174347)

    Code:java
    1. /*
    2. * Copyright (C) 2012
    3. *
    4. * Permission is hereby granted, free of charge, to any person obtaining a copy
    5. * of this software and associated documentation files (the "Software"), to deal
    6. * in the Software without restriction, including without limitation the rights
    7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    8. * copies of the Software, and to permit persons to whom the Software is
    9. * furnished to do so, subject to the following conditions:
    10. *
    11. * The above copyright notice and this permission notice shall be included in all
    12. * copies or substantial portions of the Software.
    13. *
    14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
    17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20. * SOFTWARE.
    21. */
    22. import java.io.File;
    23. import java.io.IOException;
    24. import java.io.InputStream;
    25. import java.io.InputStreamReader;
    26. import java.util.logging.Level;
    27.  
    28. import org.bukkit.configuration.file.FileConfiguration;
    29. import org.bukkit.configuration.file.YamlConfiguration;
    30. import org.bukkit.plugin.java.JavaPlugin;
    31.  
    32. public class ConfigAccessor {
    33.  
    34. private final String fileName;
    35. private final JavaPlugin plugin;
    36.  
    37. private File configFile;
    38. private FileConfiguration fileConfiguration;
    39.  
    40. public ConfigAccessor(JavaPlugin plugin, String fileName) {
    41. if (plugin == null)
    42. throw new IllegalArgumentException("plugin cannot be null");
    43. this.plugin = plugin;
    44. this.fileName = fileName;
    45. File dataFolder = plugin.getDataFolder();
    46. if (dataFolder == null)
    47. this.configFile = new File(plugin.getDataFolder(), fileName);
    48.  
    49. }
    50.  
    51. public void reloadConfig() {
    52. fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
    53.  
    54. // Look for defaults in the jar
    55. InputStream defConfigStream = plugin.getResource(fileName);
    56. if (defConfigStream != null) {
    57. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream));
    58. fileConfiguration.setDefaults(defConfig);
    59. }
    60. }
    61.  
    62. public FileConfiguration getConfig() {
    63. if (fileConfiguration == null) {
    64. this.reloadConfig();
    65. }
    66. return fileConfiguration;
    67. }
    68.  
    69. public void saveConfig() {
    70. if (fileConfiguration != null && configFile != null) {
    71. try {
    72. getConfig().save(configFile);
    73. } catch (IOException ex) {
    74. plugin.getLogger().log(Level.SEVERE, "Could not save config to " + configFile, ex);
    75. }
    76. }
    77. }
    78.  
    79. public void saveDefaultConfig() {
    80. if (!configFile.exists()) {
    81. this.plugin.saveResource(fileName, false);
    82. }
    83. }
    84.  
    85. }


    I've tried a lot but I simply don't know what's wrong, I suspect it has to do with bukkit not being able to find the config file, but I've got no idea how to fix this.

    Any kind of help is appreciated :)

    EDIT: I should also say that the arenadata variable is created in the main class (That's why I used "this")
     
  2. Offline

    TheHandfish

    How are you declaring your variable "plugin"?
     
  3. Offline

    yupie_123

  4. Offline

    TheHandfish

    Alright, can I see your Main class?
     
  5. Offline

    yupie_123

    TheHandfish

    Code:java
    1. package me.Yupie.YuPball;
    2.  
    3. import me.Yupie.YuPball.Teams.Team;
    4. import me.Yupie.YuPball.Commands.ArenaCommands;
    5. import me.Yupie.YuPball.Commands.KitCommands;
    6. import me.Yupie.YuPball.Commands.VoteCommands;
    7. import me.Yupie.YuPball.Listeners.BlockListener;
    8. import me.Yupie.YuPball.Listeners.CTFUtil;
    9. import me.Yupie.YuPball.Listeners.DamageListener;
    10. import me.Yupie.YuPball.Listeners.DashUtil;
    11. import me.Yupie.YuPball.Listeners.DropListener;
    12. import me.Yupie.YuPball.Listeners.FT500Util;
    13. import me.Yupie.YuPball.Listeners.FreezeballUtil;
    14. import me.Yupie.YuPball.Listeners.InteractListener;
    15. import me.Yupie.YuPball.Listeners.JoinListener;
    16. import me.Yupie.YuPball.Listeners.KitListener;
    17. import me.Yupie.YuPball.Listeners.PortalListener;
    18. import me.Yupie.YuPball.Listeners.TDMUtil;
    19.  
    20. import org.bukkit.Bukkit;
    21. import org.bukkit.plugin.Plugin;
    22. import org.bukkit.plugin.PluginManager;
    23. import org.bukkit.plugin.java.JavaPlugin;
    24.  
    25. public class YuPball extends JavaPlugin {
    26.  
    27. public BlockListener bl = new BlockListener(this);
    28. public CTFUtil ctf = new CTFUtil(this);
    29. public DamageListener dl = new DamageListener(this);
    30. public DashUtil dash = new DashUtil(this);
    31. public DropListener drops = new DropListener(this);
    32. public FreezeballUtil fb = new FreezeballUtil(this);
    33. public FT500Util ft500 = new FT500Util(this);
    34. public InteractListener il = new InteractListener(this);
    35. public JoinListener jl = new JoinListener(this);
    36. public KitListener kl = new KitListener(this);
    37. public PortalListener pl = new PortalListener(this);
    38. public TDMUtil tdm = new TDMUtil(this);
    39.  
    40. public Plugin instance;
    41. public ConfigAccessor playerdata = new ConfigAccessor(this, "playerdata.yml");
    42. public ConfigAccessor arenadata = new ConfigAccessor(this, "arenas.yml");
    43.  
    44. @Override
    45. public void onEnable() {
    46. instance = this;
    47. PluginManager pm = this.getServer().getPluginManager();
    48. pm.registerEvents(this.bl, this);
    49. pm.registerEvents(this.ctf, this);
    50. pm.registerEvents(this.dl, this);
    51. pm.registerEvents(this.dash, this);
    52. pm.registerEvents(this.drops, this);
    53. pm.registerEvents(this.fb, this);
    54. pm.registerEvents(this.ft500, this);
    55. pm.registerEvents(this.il, this);
    56. pm.registerEvents(this.jl, this);
    57. pm.registerEvents(this.kl, this);
    58. pm.registerEvents(this.pl, this);
    59. pm.registerEvents(this.tdm, this);
    60.  
    61. getCommand("vote").setExecutor(new VoteCommands(this));
    62. getCommand("arena").setExecutor(new ArenaCommands(this));
    63. getCommand("kit").setExecutor(new KitCommands(this));;
    64.  
    65. playerdata.getConfig().options().copyDefaults(true);
    66. playerdata.saveConfig();
    67. arenadata.getConfig().options().copyDefaults(true);
    68. arenadata.saveConfig();
    69.  
    70. GameStateUtil.get().setCurrentGame(GameState.LOBBY);
    71.  
    72. TimerUtils.get().startLobbyTime();
    73.  
    74. }
    75. }
    76.  


    This is my main class
     
  6. Offline

    TheHandfish

    If arena data was in fact the problem, you should be getting errors in your Main class.

    When you initialize ConfigAccessor, I believe you need to be getting the actual plugin, not using the "this" class.
     
  7. Offline

    fireblast709

    yupie_123 Let's start with an actual stacktrace and the code that contains the method 'exists' which supposedly causes the error.
     
  8. Offline

    yupie_123

    fireblast709 Sure, here it is:

    Code:
    [19:28:12 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'aren
    a' in plugin YuPball v0.0.4
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:740) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerCon
    nection.java:957) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :818) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
    Caused by: java.lang.NullPointerException
            at me.Yupie.YuPball.Arena.exists(Arena.java:307) ~[?:?]
            at me.Yupie.YuPball.Commands.ArenaCommands.onCommand(ArenaCommands.java:
    52) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks]
            ... 13 more
    
    fireblast709 And here's the code for the command:

    Code:java
    1. if(args[0].equalsIgnoreCase("create")){
    2. if(args.length !=2){
    3. p.sendMessage(msg("&cError: &7Correct format: /arena create <arena name>"));
    4. return false;
    5. }
    6.  
    7. if(Arena.get().exists(args[1].toString())){
    8. p.sendMessage(msg("&cError: &7There is already an arena with that name!"));
    9. return false;
    10. }
    11.  
    12. Arena.get().addArena(args[1]);
    13. p.sendMessage(msg("&cInfo: &7Successfully created the arena &e" + args[1]));
    14. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  9. Offline

    fireblast709

  10. Offline

    yupie_123

    fireblast709 Can I send it in a PM? It's quite big and a bit messy atm
     
  11. Offline

    fireblast709

    yupie_123 You can always just paste it in a pastebin or gist :p. Your issue is that you never initialized 'plugin'.

    Personally I would recommend to not use static, mainly because these kind of issues. However, if you insist on using it, the fix is rather easy. First you need a setter for the plugin field, like setPlugin(YuPball plugin) which sets the plugin field. Then, in onEnable, you would simply call setPlugin(this) to initialize the field.

    That aside, it is preferred you clean up your statics since they could cause memory leaks (even though I doubt it would be the case, it's better to be cautious). Simply call setPlugin(null) onDisable when you are done with all the saving and such.

    (Just so the rest understands what I am going on about, I pasted it on gist myself)
     
  12. Offline

    yupie_123

    fireblast709 Ok, I'll try that, and I'll use pastebin next time. Thanks :)

    fireblast709 Ok, I added the following to my Main class:
    Code:java
    1. public YuPball plugin;
    2.  
    3. onEnable(){
    4. setPlugin(this);
    5. }
    6.  
    7. public void setPlugin(YuPball plugin){
    8.  
    9. this.plugin = plugin;
    10.  
    11. }
    12.  


    But the error still happens

    And whenever I remove the static modifier, in all my other classes where I use Arena.get().METHOD it gives me an error: Cannot make a static reference to the non-static method get() from the type Arena.
    What could this mean?

    EDIT: I tried initializing arenadata inside the onEnable using "plugin" instead "this". But nothing changed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  13. Offline

    yupie_123

    The error is in the main class for sure, I tried running other commands and I got the same nullpointer pointing to a plugin.METHOD

    I really don't know what's wrong... It seems to be working fine in my other plugins.

    Also, I still haven't figured out how to remove the statics.
     
Thread Status:
Not open for further replies.

Share This Page