Strange null pointer exception.

Discussion in 'Plugin Development' started by darknesschaos, Jan 22, 2011.

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

    darknesschaos

    It's late, I'm tired and cannot figure this one out.

    Jan 22, 2011 3:50:15 AM org.bukkit.plugin.SimplePluginManager callEvent
    SEVERE: Could not pass event PLAYER_COMMAND to VoidMage
    java.lang.NullPointerException
    at com.bukkit.darknesschaos.voidmage.VoidSpell.cast(VoidSpell.java:32)
    at com.bukkit.darknesschaos.voidmage.VoidMagePlayerListener.onPlayerComm
    and(VoidMagePlayerListener.java:49)
    at org.bukkit.plugin.java.JavaPluginLoader.callEvent(JavaPluginLoader.ja
    va:135)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:201)
    at net.minecraft.server.NetServerHandler.c(NetServerHandler.java:547)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:523)
    at net.minecraft.server.Packet3Chat.a(SourceFile:24)
    at net.minecraft.server.NetworkManager.a(SourceFile:232)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:65)
    at net.minecraft.server.NetworkListenThread.a(SourceFile:104)
    at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:261)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:197)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:512)

    is caused by:
    Code:java
    1. x = target.getLocation().getBlockX() + radius;
    2. z = target.getLocation().getBlockZ() + radius;
    3.  
    4. //Store the blocks into an array
    5. for(int i = 0; i < maxHeight; i++) //for each layer of y
    6. {
    7. for(int j = 0; j < (radius*2)+1 ; j++) //for each row of x
    8. {
    9. for(int k = 0; k < (radius*2)+1; k++) //for each column of z
    10. {
    11. hole[j][k] = target.getWorld().getBlockAt(x + j, i + 1, z + k);
    12. Block airBlock = target.getWorld().getBlockAt(x + j, i + 1, z + k);
    13. airBlock.setTypeId(0);
    14. }
    15. }
    16. }
    17.  

    The variables are:
    final static int maxHeight = 128;
    static boolean voided = false;
    static int radius = 4;
    static Block hole[ ][ ][ ]; //y, x, z
    static int x;
    static int z;

    I have no idea how to fix, for all i know it is a simple mistake on my part.
    --- merged: Jan 22, 2011 12:01 PM ---
    oh, and target is a player object.
     
  2. Offline

    snpnx

    you should add line numbers to your code.
    As far as I can guess, you did not initialize a variable that you're trying to use now, which is why it's null and you get the exception. So check your variables :D
     
  3. Offline

    blaatz0r

    Which one is line 32?

    My guess is that either airBlock or target is null.
     
  4. Offline

    darknesschaos

    hole[j][k] = target.getWorld().getBlockAt(x + j, i + 1, z + k);

    is line 32
    --- merged: Jan 22, 2011 12:48 PM ---
    also, it compiles without issue in eclipse.


    What I'm trying to do is replace all blocks from the bottom of t he map to the top with air blocks, but apparently I fail at getting the block locations.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 6, 2016
  5. Offline

    Timberjaw

    Maybe I'm missing something, but it looks like you're creating a 3-dimensional array ([][][]) but writing blocks to the 2nd dimension ([][]).
     
  6. Offline

    darknesschaos

    it's just a bad copy and paste XD
     
  7. Offline

    Raphfrk

    Why are you using an array at all? Block objects are really just Block position reference objects. If your plan is to replace all the blocks again, then that doesn't store block info.

    Also, I wonder if you are going off the top edge.

    i goes from 0 to 127

    You then add 1 to it when getting the block.

    Finally, I don't think this centres on the player in question.

    Code:
    x = target.getLocation().getBlockX() + radius;
    z = target.getLocation().getBlockZ() + radius;
    
    should be


    Code:
    x = target.getLocation().getBlockX() - radius;
    z = target.getLocation().getBlockZ() - radius;
    
    That means that they go from, e.g. (for x)

    (target.getLocation().getBlockX() - radius)

    to

    target.getLocation().getBlockX() - radius + 2*radius = target.getLocation().getBlockX() + radius
     
  8. Offline

    Purre

    As the y position of blocks is stored in an index-like format (0-127), this line will start with getting block #1, the 2nd lowest block in the world. As the block index therefor is one block higher, it will end with trying to get a block which is one block higher than the maximum height. This block doesn't exist, so it will return null. Changing the line to use i, instead of i + 1 would solve the problem.

    Please correct me if I'm wrong

    EDIT: It seems like I was too slow..
     
  9. Offline

    darknesschaos

    The i+1 did not fix the issue when I tested and retested. I figured out there was something wrong with the radius when I removed the array code. That was an easy fix once I tested that bit. The code clears all of the blocks nicely. It's just that the array sucked. Now if you are saying the array is wrong for storing the type of block, should I be using the ID and store that? But when I did that, I also had a Null Pointer Exception.... what a pain this has become.
     
  10. Offline

    Purre

    Did you change the i+1 on the line below too?
     
  11. Offline

    darknesschaos

    I put the +1s where I figured they were needed, but lets see what you guys think after I post the source for this class. Here is the code in it's entirety:

    Code:
    package com.bukkit.darknesschaos.voidmage;
    
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerChatEvent;
    
    public class VoidSpell
    {
        final static int maxHeight = 128;
        static boolean voided = false;
        static int radius = 4;
        static int hole [ ][ ][ ]; //y, x, z
        static int x;
        static int z;
        static long time;
    
        public static boolean cast(PlayerChatEvent event, Player target)
        {
            System.out.println("Trying");
            if (target != null && !voided)
            {
                x = target.getLocation().getBlockX();
                z = target.getLocation().getBlockZ();
                System.out.println("x:" + x + "and z:" + z);
    
                //Store the blocks into an array
                for(int i = 0; i < maxHeight - 1; i++) //for each layer of y
                {
                    for(int j = 0; j < (radius*2)+1 ; j++) //for each row of x
                    {
                        for(int k = 0; k < (radius*2)+1; k++) //for each column of z
                        {
                            hole [i][j][k] = target.getWorld().getBlockTypeIdAt(x + (radius - j), i + 1, z + (radius - k));
                            Block airBlock = target.getWorld().getBlockAt(x + (radius - j), i + 1, z + (radius - k));
                            airBlock.setTypeId(0);
                        }
                    }
                }
                //time = System.currentTimeMillis();
                //voided = true;
                return true;
            }
            return false;
        }
    }
     
  12. Offline

    Purre

    It looks fine to me. If i get this right, the hole array is used for filling this back afterwards? Also, you might want to consider using this if you plan to use the array when the player is at a different location:
    Code:
    hole[x + i][j][z + k]
    --- merged: Jan 23, 2011 12:21 PM ---
    Now that I saw you had x and y variables outside the function too, this might not be necessary.
     
  13. Offline

    Raphfrk

    You haven't initialised hole.

    Java doesn't support dynamically resized arrays. You need to tell it the size of the array before you can use it.

    You need

    hole = new int[128][radius*2+1][radius*2+1]

    However, presumably, you are making hold static so that you don't have to keep creating/destroying it?
     
  14. Offline

    darknesschaos

    i need it for when i do a recall replacing the removed blocks.
     
  15. Offline

    Purre

    I was thinking about this too. But as I don't know everything about Java arrays, I thought that was okay, somehow. However, if you go with x and y for the replacing and initialize the array at the beginning, this thing should work.
     
  16. Offline

    darknesschaos

    doing this fixed it:

    static int[ ][ ][ ] hole = new int[128][radius*2+1][radius*2+1];

    complete derp moment for me.
     
  17. Offline

    Purre

    Nice to hear :)
     
  18. Offline

    darknesschaos

    now to continue work on my evil plugin :D

    Thanks for all the help guys!
    (and I got to learn a bit about java too)
     
Thread Status:
Not open for further replies.

Share This Page