Solved Error Parsing String to Location

Discussion in 'Plugin Development' started by MrGriefer_, Jan 11, 2017.

Thread Status:
Not open for further replies.
  1. Hello,

    I'm getting NullPointerException when parsing string to a location. Can't figure out whats the problem. Here is the error:

    Error (open)

    Code:
    [20:42:24] [Server thread/ERROR]: Could not pass event UserLobbyLeftEvent to TimeBomb v1.0.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at timebomb.game.Game.leave(Game.java:167) [TimeBomb.jar:?]
        at timebomb.user.UserManager.leaveGame(UserManager.java:162) [TimeBomb.jar:?]
        at timebomb.listener.PlayerListener.onPlayerInteractEvent(PlayerListener.java:152) [TimeBomb.jar:?]
        at sun.reflect.GeneratedMethodAccessor13.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:231) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:198) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at org.bukkit.craftbukkit.v1_10_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:194) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:946) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at net.minecraft.server.v1_10_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:27) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at net.minecraft.server.v1_10_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_111]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111]
        at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:732) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_111]
    Caused by: java.lang.NullPointerException
        at timebomb.Util.parseLocation(Util.java:51) ~[?:?]
        at timebomb.nms.Hologram_1_10_R1.load(Hologram_1_10_R1.java:184) ~[?:?]
        at timebomb.nms.Hologram_1_10_R1.loadAll(Hologram_1_10_R1.java:174) ~[?:?]
        at timebomb.listener.PlayerListener.onUserLeave(PlayerListener.java:701) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.10.2.jar:git-Spigot-1e4dd71-4507d99]
        ... 28 more


    And my code:
    Code (open)

    Code:
        public static Location parseLocation(String s) {
            String[] split = s.split(",");
            Location loc = null;
           
            try {
                World world = Bukkit.getWorld(split[0]);
                if(split.length == 6) {
                    double x = Double.parseDouble(split[1]);
                    double y = Double.parseDouble(split[2]);
                    double z = Double.parseDouble(split[3]);
                   
                    float yaw = Float.parseFloat(split[4]);
                    float pitch = Float.parseFloat(split[5]);
                    loc = new Location(world, x, y, z, yaw, pitch);
                } else if(split.length == 4) {
                    int x = Integer.parseInt(split[1]);
                    int y = Integer.parseInt(split[2]);
                    int z = Integer.parseInt(split[3]);
                   
                    loc = new Location(world, x, y, z);
                }
            } catch(NumberFormatException | ArrayIndexOutOfBoundsException e) {
                System.err.println("[TimeBomb] Cannot parse location from string: " + s);
            }
           
            return loc;
        }


    Thanks for helping and reading.
     
  2. Offline

    EagleX21

    Try this:
    Code:
    Location loc = new Location();
    // Not sure if it works though
    EDIT: Hmm, looks like it still needs arguments in that form though... Try this then:
    Code:
    Location loc = new Location(null, 0, 0, 0);
    EDIT: Or if you want to use pitch and yaw too, you can just fill in zeroes in the float places, like this:
    Code:
    Location loc = new Location(null, 0, 0, 0, 0, 0);
    EDIT: Oh, also: I have a very good tip for you. When you get a NullPointerException it means that you have assigned a variable the value "null" when you shouldn't have. Use this when debugging your code, because it's usually very simple to fix yourself. I didn't know this before, so I was usually debugging my code for hours only to find I have assigned a variable to null.
     
    Last edited: Jan 11, 2017
  3. Offline

    kameronn

    @EagleX21 You do not get a NullPointerException when you assign a null value to a varaible. You will only get a nullpointerexception when you try to call something in a method it will return an NPE.

    Why this is important:
    Code:
        String[] list = null;
    
        public void doStuff(String[] s) {
            do {
                if(list == null) {
                    list = s;
                    System.out.println(list.toString().split(","));
                   
                }
               
            } while (list == null);
        }
    
    }
    This will NOT give you a nullpointerexception but in your explanation you said it would.
     
  4. Offline

    EagleX21

    @kameronn Oh, did I say it gives it only at variables? I'm sorry then. I was trying to say: MOST OF THE TIME it's cuased by variables (in this case loc) that have been assigned the value null when the should have been assigned something else (in this case new Location())

    EDIT: Oh, I see what you're saying. Yes, this is indeed for inside methods
     
    kameronn likes this.
  5. @kameronn @EagleX21

    No. You get a null pointer exception when you try to dereference something that is null. That is the only time you will get one, and you will get it without fail.

    You can do this by invoking a method of null, unboxing null, or a couple of other things.

    Most often it is caused by invoking a method on null.
     
    kameronn likes this.
  6. Offline

    EagleX21

    @mythbusterma And this can be done by assigning null to a variable.

    EDIT: And then, indeed, trying to do something with it afterwards.
     
    Last edited: Jan 11, 2017
  7. @EagleX21

    No. It can't.

    x = null;

    Will never give you an NPE, no matter what x is.
     
  8. Offline

    EagleX21

    @mythbusterma Yes, indeed. You do have to try to access it afterwards.

    EDIT: That will then also be the line the error is thrown on.
     
  9. Offline

    kameronn

    @EagleX21
    @mythbusterma
    What he is saying @EagleX21 is that setting a variable to null will not throw a NullPointerException.
    And no it wont create an error its just because he didnt set x to anything for example
    Code:
    int x = (Integer) null;
    won't give an error

    Also the reason why he used the word dereference in his explanation

    @MrGriefer_
    Back on the original point the reason you are most likely getting the npe is because split.length is not equal to 6
     
  10. Offline

    EagleX21

    @kameronn Oh, like that. Yes, the error is indeed thrown when trying to do something with it. Or, indeed, dereference.

    Wouldn't that have been catched by the ArrayIndex etc. catch statement in his code?
     
  11. Offline

    kameronn

    No because it isn't out of bounds because the code isnt even getting ran

    Example:
    Code:
        void ok() {
            String s = "Hi,I'm,Kameron";
            String[] list = s.split(",");
           
            System.out.println(list[204]); // this would create an out of bounds exception
           
            if(list.length == 204) {
                System.out.println(list[204]); // this would not
            }
           
        }
     
  12. @kameronn

    That should create an error, assigning an int from Integer is unboxing the value, and unboxing null will give you a null pointer exception.
     
  13. I'm storing the location in file without the yaw and pitch, but if split.length is not equal 6 it will check if its 4 which is without the yaw and pitch. I'm really confused!
     
  14. Offline

    EagleX21

    @MrGriefer_ Have you tried doing what I showed you in my first post?
    Code:
    Location loc = new Location(null, 0, 0, 0, 0, 0);
    EDIT: Although, I do also agree with the others. There might be something going on in the array. But if you want to try this for me, then we know for sure if it's because of the arrays or not.
     
  15. Can you show me an example? Do I need to use my parseLocation method? I guess I need to explain this, so what I'm trying to do is get an armor stand's location in the config file by using the parseLocation method, keep in mind that I'm using NMS ArmorStand (EntityArmorStand). Maybe I'm doing something wrong with that?
     
  16. Offline

    kameronn

    @MrGriefer_
    Code:
    System.out.println(string.length);
    Please run that
    @mythbusterma look at the comments next to the code
     
  17. OMFG! I'm the most stupid person in this website, and I'll admit it. The whole problem was instead of getting the location in file with "Holograms.<Hologram Name>.Loc" I did "Holograms.<Hologram Name>"!! Thanks for your help all!
     
  18. Offline

    EagleX21

Thread Status:
Not open for further replies.

Share This Page