Solved Quick Help with a NullPointerException

Discussion in 'Plugin Development' started by ChazSchmidt, May 8, 2014.

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

    ChazSchmidt

    Code:java
    1. package me.chazschmidt.sneakattack;
    2.  
    3. import java.io.InputStream;
    4. import java.net.MalformedURLException;
    5. import java.net.URL;
    6.  
    7. import javax.xml.parsers.DocumentBuilderFactory;
    8.  
    9. import org.w3c.dom.Document;
    10. import org.w3c.dom.Node;
    11. import org.w3c.dom.NodeList;
    12.  
    13. public class updateChecker {
    14.  
    15. private URL filesfeed;
    16.  
    17. private String version;
    18. private String link;
    19.  
    20. public updateChecker(SneakAttack plugin, String url) {
    21. try {
    22. this.filesfeed = new URL(url);
    23. } catch (MalformedURLException e) {
    24. e.printStackTrace();
    25. }
    26. }
    27.  
    28. public boolean updateNeeded() {
    29. try {
    30. InputStream input = this.filesfeed.openConnection()
    31. .getInputStream();
    32. Document document = DocumentBuilderFactory.newInstance()
    33. .newDocumentBuilder().parse(input);
    34.  
    35. Node latestFile = document.getElementsByTagName("item").item(0);
    36. NodeList children = latestFile.getChildNodes();
    37.  
    38. this.version = children.item(1).getTextContent()
    39. .replaceAll("[a-zA-Z ]", "");
    40. this.link = children.item(3).getTextContent();
    41.  
    42. if (SneakAttack.plugin.getDescription().getVersion()
    43. .equals(this.version)) {
    44. return true;
    45. }
    46. } catch (Exception e) {
    47. e.printStackTrace();
    48. }
    49.  
    50. return false;
    51. }
    52.  
    53. public String getVersion() {
    54. return this.version;
    55. }
    56.  
    57. public String getLink() {
    58. return this.link;
    59. }
    60. }
    61.  




    Code:
    [16:31:45] [Server thread/WARN]: java.lang.NullPointerException
    [16:31:45] [Server thread/WARN]:    at me.chazschmidt.sneakattack.updateChecker.updateNeeded(updateChecker.java:42)
    [16:31:45] [Server thread/WARN]:    at me.chazschmidt.sneakattack.SneakAttack.onEnable(SneakAttack.java:162)
    [16:31:45] [Server thread/WARN]:    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250)
    [16:31:45] [Server thread/WARN]:    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324)
    [16:31:45] [Server thread/WARN]:    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404)
    [16:31:45] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.java:448)
    [16:31:45] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.java:382)
    [16:31:45] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.n(MinecraftServer.java:352)
    [16:31:45] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.g(MinecraftServer.java:326)
    [16:31:45] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.a(MinecraftServer.java:282)
    [16:31:45] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.java:182)
    [16:31:45] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:436)
    [16:31:45] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    What's coming up null here? Obviously it is something in line 42, I'm just not sure what.
     
  2. Offline

    rsod

    either
    SneakAttack.plugin
    or
    SneakAttack.plugin.getDescription()
    or
    SneakAttack.plugin.getDescription().getVersion()
    insert that code before line 42 to detect what exactly causing problem
    Code:
    System.out.println(SneakAttack.plugin==null?"true":"false");
    System.out.println(SneakAttack.plugin.getDescription()==null?"true":"false");
    System.out.println(SneakAttack.plugin.getDescription().getVersion()==null?"true":"false");
     
  3. Offline

    ChazSchmidt

    Good idea. I tried it and I got:

    Code:
    [17:11:05] [Server thread/INFO]: [SneakAttack] Enabling SneakAttack v1.21
    [17:11:05] [Server thread/INFO]: SneakAttack Version 1.21 has been enabled!
    [17:11:05] [Server thread/INFO]: true
    [17:11:05] [Server thread/WARN]: java.lang.NullPointerException
    [17:11:05] [Server thread/WARN]:    at me.chazschmidt.sneakattack.updateChecker.updateNeeded(updateChecker.java:44)
    [17:11:05] [Server thread/WARN]:    at me.chazschmidt.sneakattack.SneakAttack.onEnable(SneakAttack.java:162)
    [17:11:05] [Server thread/WARN]:    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250)
    [17:11:05] [Server thread/WARN]:    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:324)
    [17:11:05] [Server thread/WARN]:    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404)
    [17:11:05] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.java:448)
    [17:11:05] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.java:382)
    [17:11:05] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.n(MinecraftServer.java:352)
    [17:11:05] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.g(MinecraftServer.java:326)
    [17:11:05] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.a(MinecraftServer.java:282)
    [17:11:05] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.java:182)
    [17:11:05] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:436)
    [17:11:05] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    Which that means it is the .getDescription()... Is that because it is interpreting it as a Java plugin and not a bukkit plugin? Would it be easier to make the PluginDescriptionFile public and just get the version through there?
     
  4. Offline

    rsod

    no, that means
    SneakAttack.plugin
    is null
    You forgot to add plugin field to constructor. Add a private variable and change your constructor like this:
    Code:
    private SneakAttack plugin;
    public updateChecker(SneakAttack plugin, String url) {
    this.plugin = plugin;
    try {
    this.filesfeed = new URL(url);
    } catch (MalformedURLException e) {
    e.printStackTrace();
    }
    }
    then to make a check use:
    this.plugin.getDescription().getVersion()
     
    ChazSchmidt likes this.
  5. Offline

    ChazSchmidt

    Tried that and I just tried that** as well, but I'm getting the same exception in the same place.
    ** private SneakAttack plugin = SneakAttack.plugin;
     
  6. Offline

    rsod

    don't you see difference between SneakAttack.plugin and this.plugin?
     
  7. Offline

    GreatBlitz

    ChazSchmidt
    Declare an instance variable in this way
    SneakAttack plugin;

    I hope you know what instance variables are.

    Now, in your constructor, do this:
    this.plugin = plugin;

    I said the exact same thing rsod said, but in different words.



    Why on earth would you try and make a static reference?
     
    ChazSchmidt likes this.
  8. Offline

    ChazSchmidt

    Oh my god I'm stupid. Sorry guys, I' sick today and thought I'd do a little coding in my off time and apparently my brain is taking a sick day too. With your help I have it straightened out. Thank you for your time and bearing with me.
     
  9. Offline

    GreatBlitz

    No problem. Yeah, everyone has bad and good days.
    Mark the thread as solved.
    From next time, thank the people who helped you with a like :) it really makes them (or me haha) feel good.
    ChazSchmidt
     
Thread Status:
Not open for further replies.

Share This Page