-1

So I have a file manager class where it handles all the data being loaded and saved. But for some reason when I declare the file manager to be used in another class it turns up to be null but when I test the file manager via debug messages, it then shows up as, well not null. but for some reason when I use the declaration it shows up as null.

[16:21:22 ERROR]: Error occurred while enabling CreditCard v1.0 (Is it up to 
date?)
java.lang.NullPointerException
    at com.dogboy602k.CreditCard.main.Util.Manager.<init>(Manager.java:47) ~[?:?]
    at com.dogboy602k.CreditCard.main.Main.Main.onEnable(Main.java:42) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot.jar:git-Spigot-e50d38c-02f4218]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.loadPlugin(CraftServer.java:362) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugins(CraftServer.java:322) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.reload(CraftServer.java:746) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at org.bukkit.Bukkit.reload(Bukkit.java:539) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:646) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchServerCommand(CraftServer.java:632) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at net.minecraft.server.v1_10_R1.DedicatedServer.aL(DedicatedServer.java:437) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot.jar:git-Spigot-e50d38c-02f4218]
    at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot.jar:git-Spigot-e50d38c-02f4218]

File Manager:

public class FileManager {
private Main plugin;
private ArrayList <UUID> list = new ArrayList();
private ArrayList <String> Rlist = new ArrayList();

public FileManager(Main plugin) {
    this.plugin = plugin;
    SendConsoleMessage.debug("pl: "+plugin);
}

//EXTRA CODE
}

Main:

public class Main extends JavaPlugin {
private Manager manager;
private Economy economy = null;
private FileManager fileManager;
private Commands Commands;
private Plugin plugin = this;
public void onEnable() {
    File Configfile;
    this.fileManager = new FileManager(this);
    if(getFileManager() == null){
        SendConsoleMessage.debug("its null");
    }
    this.manager = new Manager(this);
    this.Commands = new Commands(this);
    this.setupEconomy();

    this.getCommand("CreditCard").setExecutor(new Commands(this));
    File playerDataFile = new File(this.getDataFolder(), "playerdata.yml");
    if (!playerDataFile.exists()) {
        this.getFileManager().saveDefaultConfiguration(playerDataFile);
    }
    File rewardDataFile = new File(this.getDataFolder(), "rewards.yml");
    if (!rewardDataFile.exists()) {
        this.getFileManager().saveDefaultConfiguration(rewardDataFile);
    }
    if (!(Configfile = new File(this.getDataFolder(), "config.yml")).exists()) {
        this.saveDefaultConfig();
    }
    this.fileManager.loadRewardData();
    this.fileManager.loadPlayerData();

    Bukkit.getPluginManager().registerEvents(this.manager, this);
    this.getManager().addIntrest();
    manager.resentTheMaxPoints();
    manager.autoPayExecute();
}

Manager:

public class Manager implements Listener {
private Main plugin;
private MessageManager mm;
private Player p;
private List<MaxLoan> loan;
private List<CardInfo> cardInfos = new ArrayList<CardInfo>();
private List<RewardInfo> rewards = new ArrayList<RewardInfo>();
private int pinHolder;
private double debt, points;
private String PasswordHolder;
private Double loanamount1 = 0.0;
private Double payoff1 = 0.0;
private int Gperspin = 0;
private Player Gowner = null;
private ArrayList<UUID> UUIDs = new ArrayList();
private ArrayList<Player> overDuePlayersON = new ArrayList<Player>();
private ArrayList<OfflinePlayer > overDuePlayersOFF = new ArrayList<OfflinePlayer>();
private int count =0;
FileManager f = plugin.getFileManager();
//this imports the premade MSGS
private String hasNotRegistered = f.gethasNotRegistered() ,wrongPassword= f.getwrongPassword(),alreadyRegisterd= f.getalreadyRegisterd(),notEnoughCashtoPay= f.getnotEnoughCashtoPay(),
        payAmountOverDebt= f.getpayAmountOverDebt(), wrongPin= f.getwrongPin(), maxLoanReached= f.getmaxLoanReached(),maxPointsReached= f.getmaxPointsReached(), wantLoanLargerThanLimit= f.getwantLoanLargerThanLimit(), commandDidntSayAll= f.getcommandDidntSayAll(),
        dontHaveEnoughToPayAllDebt= f.getdontHaveEnoughToPayAllDebt(),DebtCantBePaidItsZero= f.getDebtCantBePaidItsZero(),payAmountLessThanZero= f.getpayAmountLessThanZero(), notEnoughPoints= f.getnotEnoughPoints(),loanLessthanZero= f.getloanLessthanZero() ;
private String foundPin= f.getfoundPin(),registered= f.getregistered(),autoPayMSG= f.getautoPayMSG(), moneyIsBeingAdded= f.getmoneyIsBeingAdded(), newTotalPoints= f.getnewTotalPoints(), youHaveZeroDebtpPayInZeroDays= f.getyouHaveZeroDebtpPayInZeroDays(),
        overDeadLine= f.getoverDeadLine(), youOwnAmountInDays= f.getyouOwnAmountInDays(), pleaseWait= f.getpleaseWait(), howMuchIsPaidOff= f.gethowMuchIsPaidOff(), youHavePoints= f.getyouHavePoints(),pointsDescription= f.getpointsDescription(),
        youGotReward= f.getyouGotReward(), closedGUIMenu= f.getclosedGUIMenu(),pointsYouCanGet= f.getpointsYouCanGet(),couldNotFindReward= f.getcouldNotFindReward(),joinMSGNoDebt= f.getjoinMSGNoDebt(),joinMSG= f.getjoinMSG();
public Manager(Main plugin) {
    this.plugin = plugin;
    this.loan = new ArrayList<MaxLoan>();
}
jack
  • 101
  • 1
  • 2
  • 6
  • Which line is `Manager.java:47` -- line 47 of Manager.java? – Hovercraft Full Of Eels Jul 27 '17 at 20:53
  • what line ? which file ? – Ali Faris Jul 27 '17 at 20:53
  • 4
    In the `Manager` class you have this statement `FileManager f = plugin.getFileManager();` when that is executed, `plugin` can't be anything else but `null` – Titus Jul 27 '17 at 20:53
  • Yep, @Titus got it right before I did. The heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. In the future, please search on the subject before posting, since this is too common a problem to post yet another NPE question. – Hovercraft Full Of Eels Jul 27 '17 at 20:54
  • but when i debug it, in file managers construct its not null aka plugin – jack Jul 27 '17 at 20:59
  • 1
    The stacktrace doesn't lie and your code doesn't lie -- there's no way that that line **won't** throw a NPE. This means that your debugging is at fault. – Hovercraft Full Of Eels Jul 27 '17 at 21:00
  • so i changed it into the construct, where its the plugin dec. then the file manager dec. and i still have the same issue – jack Jul 27 '17 at 21:04

1 Answers1

0

that because you declared plugin without initializing it in your Manager class

class Manager {
     ...
     private Main plugin;
     FileManager f = plugin.getFileManager();
     ...
}

i suggest you add constructor to Manager class and initialize plugin and then get the fileManager

something like this :

public Manager()
{
    this.plugin = new Main();
    FileManager f = plugin.getFileManager();
}
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
  • main is already instantiated by the server, that is not null – jack Jul 27 '17 at 21:01
  • I initially down-voted this answer because you're answering a question that is an *obvious* duplicate of [this question](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it), and when answering obvious dups, I tend to judge the answer in comparison to the answers given in the dup. Please read those answers to see how yours compares. – Hovercraft Full Of Eels Jul 27 '17 at 21:02
  • 2
    @jack: you are quite confused. The plugin variable is what matters here and it is null. It matters nothing that Main has been instantiated elsewhere. You're confusing objects with variables. Objects can't be null and variables can. Instantiating an object somewhere has no effect on variables elsewhere, and state changes to that object will not affect a variable until it has been assigned a reference to that object. – Hovercraft Full Of Eels Jul 27 '17 at 21:03