变量不为null然后变为空?

时间:2017-07-27 20:45:40

标签: java nullpointerexception bukkit

所以我有一个文件管理器类,它处理所有正在加载和保存的数据。但由于某种原因,当我声明文件管理器在另一个类中使用时,它变为null但是当我通过调试消息测试文件管理器时,它会显示为,而不是null。但出于某种原因,当我使用声明时,它显示为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]

文件管理器:

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
}

主:

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();
}

管理器:

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>();
}

1 个答案:

答案 0 :(得分:0)

因为您在plugin

中初始化Manager而宣布了class Manager { ... private Main plugin; FileManager f = plugin.getFileManager(); ... }
Manager

我建议您将构造函数添加到plugin类并初始化public Manager() { this.plugin = new Main(); FileManager f = plugin.getFileManager(); } 然后获取fileManager

类似的东西:

void(const int y);
void(const int& y);