Solved NPE on PlayerLogin

Discussion in 'Plugin Development' started by FlareLine, Oct 6, 2013.

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

    FlareLine

    I have a plugin with two classes: IPCMain and IPCListener.
    On PlayerLoginEvent, IPCListener will send some infos to the console, and log the player's name in a file called logger.yml with a dummy value (2).
    However I now want to print out the player's IP address to the console, and the method I am using currently is causing an NPE I can't seem to squash on PlayerLoginEvent. The method for obtaining the player's address and printing it out to the console is commented.

    IPCMain:
    Code:java
    1. package dev.flareline.ipcheck;
    2.  
    3. import java.io.File;
    4. import java.io.FileOutputStream;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. import java.io.OutputStream;
    8. import java.util.logging.Logger;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.Server;
    13. import org.bukkit.command.Command;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.configuration.file.FileConfiguration;
    16. import org.bukkit.configuration.file.YamlConfiguration;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class IPCMain extends JavaPlugin {
    21.  
    22. // For colouring purposes
    23. /*public static final String ANSI_RESET = "\u001B[0m";
    24.   public static final String ANSI_CYAN = "\u001B[36m";*/
    25.  
    26. static File configFile;
    27. static File loggerFile;
    28.  
    29. static FileConfiguration config;
    30. static FileConfiguration logger;
    31.  
    32. @Override
    33. public void onDisable() {
    34. saveYamls();
    35. }
    36.  
    37. @Override
    38. public void onEnable() {
    39.  
    40. getServer().getPluginManager().registerEvents(new IPCListener(), this);
    41.  
    42. configFile = new File(getDataFolder(), "config.yml");
    43. loggerFile = new File(getDataFolder(), "logger.yml");
    44.  
    45. try {
    46. firstRun();
    47. } catch (Exception e) {
    48. e.printStackTrace();
    49. }
    50. config = new YamlConfiguration();
    51. logger = new YamlConfiguration();
    52. loadYamls();
    53.  
    54. }
    55.  
    56. private void firstRun() throws Exception {
    57. if (!configFile.exists()) {
    58. configFile.getParentFile().mkdirs();
    59. copy(getResource("config.yml"), configFile);
    60. }
    61. if (!loggerFile.exists()) {
    62. loggerFile.getParentFile().mkdirs();
    63. copy(getResource("logger.yml"), loggerFile);
    64. }
    65. }
    66.  
    67. private void copy(InputStream in, File file) {
    68. try {
    69. OutputStream out = new FileOutputStream(file);
    70. byte[] buf = new byte[1024];
    71. int len;
    72. while ((len = in.read(buf)) > 0) {
    73. out.write(buf, 0, len);
    74. }
    75. out.close();
    76. in.close();
    77. } catch (Exception e) {
    78. e.printStackTrace();
    79. }
    80. }
    81.  
    82. public static void loadYamls() {
    83. try {
    84. config.load(configFile);
    85. logger.load(loggerFile);
    86. } catch (Exception e) {
    87. e.printStackTrace();
    88. }
    89. }
    90.  
    91. public static void saveYamls() {
    92. try {
    93. config.save(configFile);
    94. logger.save(loggerFile);
    95. } catch (IOException e) {
    96. e.printStackTrace();
    97. }
    98. }
    99.  
    100. public void writeLogs(Player player) {
    101. String playername = player.getName();
    102. logger.createSection(playername);
    103. saveYamls();
    104. }
    105.  
    106. public boolean onCommand(CommandSender sender, Command cmd, String label,
    107. String[] args) {
    108. if (sender instanceof Player) {
    109. if (cmd.getName().equalsIgnoreCase("ipc")) {
    110. sender.sendMessage(ChatColor.GOLD + "IPC");
    111. return true;
    112. }
    113. } else {
    114. sender.sendMessage(ChatColor.GOLD + "You must be ingame to use IPC");
    115. }
    116. return false;
    117. }
    118.  
    119. public static void checker(String playername, /*String address*/) {
    120. loadYamls();
    121. logger.set(playername, null);
    122. saveYamls();
    123. // Use this method to getLogger as to reference it in a static way.
    124. /*Server server = Bukkit.getServer();
    125.   Logger console = server.getLogger();
    126.   console.info(ANSI_CYAN + playername + "'s IP Address is: " + address + ANSI_RESET);*/
    127. }
    128.  
    129. }

    IPCListener:
    Code:java
    1. package dev.flareline.ipcheck;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.EventPriority;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerLoginEvent;
    9.  
    10. public class IPCListener implements Listener {
    11.  
    12. public static final String ANSI_RESET = "\u001B[0m";
    13. public static final String ANSI_CYAN = "\u001B[36m";
    14.  
    15. IPCMain ipcheckinstance = new IPCMain();
    16.  
    17. @EventHandler(priority = EventPriority.HIGHEST)
    18. public void onLogin(PlayerLoginEvent event) {
    19.  
    20. Player player = event.getPlayer();
    21. String playername = player.getName();
    22. /*String address = player.getAddress().toString();*/
    23.  
    24. Bukkit.getLogger().info(
    25. ANSI_CYAN + playername + " Has Joined The Server!" + ANSI_RESET);
    26. Bukkit.getLogger().info(
    27. ANSI_CYAN + "Checking Information On: " + playername + ANSI_RESET);
    28.  
    29. IPCMain.checker(playername, /*address*/);
    30.  
    31. }
    32. }

    Any help would be appreciated. :)
     
  2. Offline

    Wolvereness Bukkit Team Member

    You can't use player.getAddress() during login, so it's actually provided inside the event itself.
     
  3. Offline

    FlareLine

    Wolvereness I've fixed the above issue and used event.getAddress().toString() instead, however I am now running into an NPE at line 125 in IPCMain:
    Code:text
    1. 2013-10-07 06:33:20 [INFO] Done (20.945s)! For help, type "help" or "?"
    2. 2013-10-07 06:34:11 [INFO] [36mFlareLine Has Joined The Server![0m
    3. 2013-10-07 06:34:11 [INFO] [36mChecking Information On: FlareLine[0m
    4. 2013-10-07 06:34:11 [INFO] [36mFlareLine's IP Address is: /127.0.0.1[0m
    5. 2013-10-07 06:34:11 [SEVERE] Could not pass event PlayerLoginEvent to IPCheck v01.00.00
    6. org.bukkit.event.EventException
    7. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    8. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    9. at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    10. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    11. at net.minecraft.server.v1_6_R3.PlayerList.attemptLogin(PlayerList.java:335)
    12. at net.minecraft.server.v1_6_R3.PendingConnection.e(PendingConnection.java:121)
    13. at net.minecraft.server.v1_6_R3.PendingConnection.d(PendingConnection.java:43)
    14. at net.minecraft.server.v1_6_R3.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:41)
    15. at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:29)
    16. at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    17. at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    18. at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    19. at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    20. at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    21. Caused by: java.lang.NullPointerException
    22. at dev.flareline.ipcheck.IPCMain.checker(IPCMain.java:125)
    23. at dev.flareline.ipcheck.IPCListener.onLogin(IPCListener.java:31)
    24. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    25. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    26. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    27. at java.lang.reflect.Method.invoke(Unknown Source)
    28. at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    29. ... 13 more

    Line 125's Method
    Code:java
    1. public static void checker(String playername, String address) {
    2. Server server = Bukkit.getServer();
    3. Logger console = server.getLogger();
    4. console.info(ANSI_CYAN + playername + "'s IP Address is: " + address
    5. + ANSI_RESET);
    6. loadYamls();
    7. Object playercheck = logger.get(playername);
    8. String playerstring = playercheck.toString(); // line 125
    9. if (playerstring.equals(null)) {
    10. logger.set(playername, null);
    11. } else {
    12. logger.set(playername + ".firstip", address);
    13. saveYamls();
    14. }
    15. }

    Line 31 is just a method executor, so I'm guessing that wouldn't cause it.
    Code:java
    1. IPCMain.checker(playername, address);


    Solved, It was because I was comparing a string to null. Replacing
    Code:java
    1. if (playerstring.equals(null)) {
    2. //blahblah
    3. } else {
    4. //blahblah
    5. }

    with
    Code:java
    1. if (playeraddress != null) {

    and swapping the condition statements fixed the issue.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page