Why am I getting null pointers here?

Discussion in 'Plugin Development' started by Huffmanbran, Oct 21, 2014.

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

    Huffmanbran

    This is a plugin that will spawn space meteors. Nowhere in that code do I see any sort of null. All I did was create a second class named MeteorTimer and extended it to a BukkitRunnable. One reason I believe why I'm getting a nullpointer is because I didn't set the constructor to anything, I don't know if I need to really do anything with it or not, I just know it should be there. If anything belongs in there could someone elaborate as to why it is needed? Thank you! :D

    Here is my onEnable():
    Code:java
    1. public void onEnable() {
    2. meteorBlocks.add(Material.DIAMOND_ORE);
    3. meteorBlocks.add(Material.GOLD_ORE);
    4. meteorBlocks.add(Material.COAL_ORE);
    5. meteorBlocks.add(Material.STONE);
    6. meteorBlocks.add(Material.DIRT);
    7. meteorBlocks.add(Material.GLOWSTONE);
    8. meteorBlocks.add(Material.WOOL);
    9.  
    10. MeteorTimer mt = new MeteorTimer();
    11.  
    12. int r = (int) (Math.random() * 30);
    13. int x = (int) (Math.random() * 2000);
    14. int y = (int) (Math.random() * 230);
    15. int z = (int) (Math.random() * 2000);
    16. int v = (int) (4 / 3 * 3.14 * r * r);
    17.  
    18. World w = Bukkit.getServer().getWorld(Bukkit.getServerName());
    19. Location loc = new Location(w, x, y, z);
    20.  
    21. generateSphere(loc, r);
    22. @SuppressWarnings("deprecation")
    23. Player p = Bukkit.getServer().getPlayer(Bukkit.getServerName());
    24. p.sendMessage(ChatColor.BLUE + "Meteors:" + ChatColor.WHITE
    25. + " New meteor has been located at " + x + " " + y + " " + z
    26. + ". Meteor size is roughly: " + v + " blocks.");
    27. mt.runTaskTimer(this, LONG_DELAY, LONG_PERIOD);
    28. //out.print("METEOR SPAWNED!");
    29.  
    30. }


    My MeteorTimer class:
    Code:java
    1. public class MeteorTimer extends BukkitRunnable{
    2.  
    3. public MeteorTimer(){
    4. // what goes here?
    5. }
    6.  
    7.  
    8. @Override
    9. public void run() {
    10.  
    11.  
    12. }
    13.  
    14.  
    15.  
    16.  
    17.  
    18. }


    generateSphere method:
    Code:java
    1. public static List<Location> generateSphere(Location centerBlock, int radius) {
    2. List<Location> centerBlocks = new ArrayList<Location>();
    3.  
    4. int bX = centerBlock.getBlockX();
    5. int bY = centerBlock.getBlockY();
    6. int bZ = centerBlock.getBlockZ();
    7. for (int x = bX - radius; x <= bX + radius; x++) {
    8. for (int y = bY - radius; y <= bY + radius; y++) {
    9. for (int z = bZ - radius; z <= bZ + radius; z++) {
    10.  
    11. double distance = ((bX - x) * (bX - x))
    12. + ((bZ - z) * (bZ - z) + ((bY - y)) * (bY - y));
    13.  
    14. if (distance < radius * radius
    15. && !(distance < ((radius - 1) * (radius - 1)))) {
    16.  
    17. for (Location l : generateSphere(centerBlock, radius)) {
    18. Material type = meteorBlocks.get((int) (Math
    19. .random() * meteorBlocks.size()));
    20. l.getBlock().setType(type);
    21.  
    22. centerBlocks.add(l);
    23.  
    24. }
    25. }
    26.  
    27. }
    28.  
    29. }
    30.  
    31. }
    32. return centerBlocks;
    33. }
    34.  
    35. }
    36.  


    Edit: I was getting a nullpointer but I ran into another problem in my stack trace log:
    Code:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
            at me.huffmanbran.bukkit.five.MeteorCore.generateSphere(MeteorCore.java:
    71) ~[?:?]
    Thank you for the help! :D
     
  2. Offline

    teej107

    Post your generateSphere method.
     
  3. Offline

    Huffmanbran

    Here you are:
    Code:java
    1. public static List<Location> generateSphere(Location centerBlock, int radius) {
    2. List<Location> centerBlocks = new ArrayList<Location>();
    3.  
    4. int bX = centerBlock.getBlockX();
    5. int bY = centerBlock.getBlockY();
    6. int bZ = centerBlock.getBlockZ();
    7. for (int x = bX - radius; x <= bX + radius; x++) {
    8. for (int y = bY - radius; y <= bY + radius; y++) {
    9. for (int z = bZ - radius; z <= bZ + radius; z++) {
    10.  
    11. double distance = ((bX - x) * (bX - x))
    12. + ((bZ - z) * (bZ - z) + ((bY - y)) * (bY - y));
    13.  
    14. if (distance < radius * radius
    15. && !(distance < ((radius - 1) * (radius - 1)))) {
    16.  
    17. for (Location l : generateSphere(centerBlock, radius)) {
    18. Material type = meteorBlocks.get((int) (Math
    19. .random() * meteorBlocks.size()));
    20. l.getBlock().setType(type);
    21.  
    22. centerBlocks.add(l);
    23.  
    24. }
    25. }
    26.  
    27. }
    28.  
    29. }
    30.  
    31. }
    32. return centerBlocks;
    33. }
    34.  
    35. }
    36.  
     
  4. Offline

    teej107

    What is line 71 of the class?
     
  5. Offline

    Huffmanbran

    Code:java
    1. for (Location l : generateSphere(centerBlock, radius)) {
     
  6. Offline

    teej107

    Huffmanbran Hmm... weird. I don't see any possible NPEs. I do see a possible IndexOutOfBoundsException though with:
    Code:java
    1. meteorBlocks.get((int) (Math
    2. .random() * meteorBlocks.size()))
    And the reason you are getting spammed with the same console message is because you created a recursive method without a stopping point. Are you sure you are getting an NPE? This thread will help you with stack-traces a bit more. http://forums.bukkit.org/threads/ho...ubleshoot-your-own-plugins-by-yourself.32457/
     
  7. Offline

    Huffmanbran

    I did have an NPE, but the recursive method spams the NPE exception error out of the console. I'll just fix the recursive method real quick and give you the NPE exception.

    Couldn't you just stop it by adding an if else? I don't seem to know how to stop it apparently :/

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

    Watto

  9. Offline

    fireblast709

    Huffmanbran the recursion is useless (it breaks your code without adding any functionality), just remove it.
     
  10. Offline

    teej107

    You are adding an if statement to prevent recursion, (I'm too lazy to do the math if the test would even work :p), your problem is you are putting in the exact same arguments in your recursive call as your initial method call. Nothing is changing.
     
  11. Offline

    Huffmanbran

    I have gotten rid of it. And it got rid of my StackOverFlow error. Here is the NPE:
    Code:
    java.lang.NullPointerException
            at me.huffmanbran.bukkit.five.MeteorCore.onEnable(MeteorCore.java:43) ~[
    ?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:316) ~[s
    pigot.jar:git-Spigot-1649]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:332) [spigot.jar:git-Spigot-1649]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:417) [spigot.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugin(CraftServer.jav
    a:476) [spigot.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.enablePlugins(CraftServer.
    java:394) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.n(MinecraftServer.java:3
    60) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.g(MinecraftServer.java:3
    34) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.a(MinecraftServer.java:2
    90) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.jav
    a:210) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :458) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1649]
    I know the nullpointer is being causing somewhere in the onEnable() method.
     
  12. Offline

    WesJD

    Huffmanbran What's line 43 of MeteorCore? Is MeteorCore your main class?

    Also, please show us your full classes, not just parts of it. It would really help.
     
  13. Offline

    fireblast709

    Huffmanbran is there a Player with the same name as your server? If no, then that is the reason ;3.
     
  14. Offline

    Huffmanbran

    Yeah it's because I instantiated the player as the server name for some reason.. -.-

    Code:java
    1. Player p = Bukkit.getServer().getPlayer(Bukkit.getServerName());


    Edit: What would I need to set in this code in order to retrieve the player and its name?
     
  15. Offline

    WesJD

    Huffmanbran
    Code:java
    1. Player p = Bukkit.getServer().getPlayer().getDisplayName();

    Should work
     
  16. Offline

    Konato_K

    WesJD Why are you assigning a String object to a Player?

    Huffmanbran You can't do it on the onEnable unless you reload your server with a player, otherwise you'll need to do it elsewhere, either a PlayerLoginEvent or something like that.
     
  17. Offline

    Huffmanbran

    So could I set it to where when the player logs in it will store their name into an array or something? And when that method is called it will check if the player is in the array or another data storage? Would that work?
     
  18. Offline

    WesJD

    Konato_K Oh, I thought that's what he wanted? My bad.
     
  19. Offline

    fireblast709

    Huffmanbran wouldn't you want to spawn the meteor on an event or command instead of onEnable in the first place?
     
  20. Offline

    Huffmanbran

    Well I got it to run, turns out I'm just not going to use Player, I don't need to.
    Here is the successful stacktrace:
    Code:
    [20:00:05 INFO]: [Meteors] Enabling Meteors v1.0
    [20:00:05 INFO]: [Meteors] New meteor spawned at 996 76 1428
    [20:00:05 INFO]: [Meteors] Size of meteor: 1256 blocks!
    Yet, at that location there is no sign of a meteor or a single block containing in the list of materials that the meteor will be made of. Why is it not generating the blocks?

    Here is my generateSphere method:
    Code:java
    1. public static List<Location> generateSphere(Location centerBlock, int radius) {
    2. List<Location> centerBlocks = new ArrayList<Location>();
    3.  
    4. int bX = centerBlock.getBlockX();
    5. int bY = centerBlock.getBlockY();
    6. int bZ = centerBlock.getBlockZ();
    7. for (int x = bX - radius; x <= bX + radius; x++) {
    8. for (int y = bY - radius; y <= bY + radius; y++) {
    9. for (int z = bZ - radius; z <= bZ + radius; z++) {
    10.  
    11. double distance = ((bX - x) * (bX - x))
    12. + ((bZ - z) * (bZ - z) + ((bY - y)) * (bY - y));
    13.  
    14. if (distance < radius * radius
    15. && !(distance < ((radius - 1) * (radius - 1)))) {
    16.  
    17. centerBlocks.add(centerBlock);
    18.  
    19. }
    20.  
    21. }
    22.  
    23. }
    24.  
    25. }
    26. return centerBlocks;
    27. }
    28.  
    29. }
    30.  
     
  21. Offline

    fireblast709

    Huffmanbran generateSphere just returns a List of Locations... with a lot of centerBlock in it (note that all these will be the same). It doesn't change any block or spawn any entity.
     
  22. Offline

    Huffmanbran

    What if I added:
    Code:java
    1. for (Location l : generateSphere(centerBlock, radius)) {
    2. Material type = meteorBlocks
    3. .get((int) (Math.random() * meteorBlocks.size()));
    4. l.getBlock().setType(type);
    5.  
    6. centerBlocks.add(centerBlock);
    7.  
    8. //note this function is in the generateSphere method
    9.  



    Edit: I added it, now I get a StackOverFlow..
     
  23. Offline

    fireblast709

    Huffmanbran invoke it in onEnable (where you get your List<Location> from the generateSphere method)
     
Thread Status:
Not open for further replies.

Share This Page