Plugin Help, Attempt #2

Discussion in 'Spigot Plugin Development' started by tspringst33n, May 25, 2015.

  1. I keep having the issue with the LocationTypeAdapter public class. It keeps giving these errors.
    Code (Text):
    "The type LocationTypeAdapter must implement the inherited abstract method JsonSerializer<Location>.serialize(Location, Type, JsonSerializationContext)"
    and
    "The type LocationTypeAdapter must implement the inherited abstract method JsonDeserializer<Location>.deserialize(JsonElement, Type, JsonDeserializationContext)"

    The code below is the class file that has the error. The full code is available here at GitHub.

    I don't know what is causing the errors and I cant change it to abstract as it breaks other classes.
    Does anyone have any ideas on what to change to make the error go away?
    Thanks,
    ~Tspringst33n

    Code (Text):
    package org.mcteam.ancientgates.gson.typeadapters;

    import java.lang.reflect.Type;

    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializationContext;
    import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializer;
    import org.bukkit.craftbukkit.libs.com.google.gson.JsonElement;
    import org.bukkit.craftbukkit.libs.com.google.gson.JsonObject;
    import org.bukkit.craftbukkit.libs.com.google.gson.JsonParseException;
    import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializationContext;
    import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializer;
    import org.mcteam.ancientgates.Plugin;

    public class LocationTypeAdapter implements JsonDeserializer<Location>,
            JsonSerializer<Location> {
        private static final String WORLD = "world";
        private static final String X = "x";
        private static final String Y = "y";
        private static final String Z = "z";
        private static final String YAW = "yaw";
        private static final String PITCH = "pitch";

        public Location deserialize(JsonElement json, Type typeOfT,
                JsonDeserializationContext context) throws JsonParseException {
            JsonObject obj = new JsonObject();// com.google.gson.JsonObject obj =
                                                // json.getAsJsonObject();

            Location location = null;
            World world = this.getWorld(obj.get(WORLD).getAsString());

            if (world != null) {
                double x = obj.get(X).getAsDouble();
                double y = obj.get(Y).getAsDouble();
                double z = obj.get(Z).getAsDouble();
                float yaw = obj.get(YAW).getAsFloat();
                float pitch = obj.get(PITCH).getAsFloat();
                location = new Location(world, x, y, z, yaw, pitch);
            }

            return location;
        }

        public JsonObject serialize(Location src, Type typeOfSrc,
                JsonSerializationContext context) {
            JsonObject obj = new JsonObject();

            if (src == null) {
                Plugin.log("Passed location is null in MyLocationTypeAdapter.");
                return obj;
            } else if (src.getWorld() == null) {
                Plugin.log("Passed location's world is null in MyLocationTypeAdapter.");
                return obj;
            }

            obj.addProperty(WORLD, src.getWorld().getName());
            obj.addProperty(X, src.getX());
            obj.addProperty(Y, src.getY());
            obj.addProperty(Z, src.getZ());
            obj.addProperty(YAW, src.getYaw());
            obj.addProperty(PITCH, src.getPitch());

            return obj;
        }

        private World getWorld(String name) {
            World world = Plugin.instance.getServer().getWorld(name);
            return world;
        }
    }
     
  2. BUMP. Some help would be greatly appreciated. I am trying to get a new release of a plugin out soon.
     
    #2 tspringst33n, May 26, 2015
    Last edited: May 26, 2015
  3. for me it works when changing the imports to: (I think you imported wrong repackaged classes)

    import com.google.gson.JsonDeserializationContext;
    import com.google.gson.JsonDeserializer;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParseException;
    import com.google.gson.JsonSerializationContext;
    import com.google.gson.JsonSerializer;
     
  4. That fixed the problem at least in Eclipse. Now for testing. Thank you @Friwi!
     
  5. After testing in a server environment, I am now getting the error below. I know something is null when it shouldn't be. It is probably right under my nose and I can't see it. The class is after the error code.
    (I deleted this post earlier because I thought it was fixed, and I thought wrong.)

    Code (Text):

    [21:17:40 INFO]: [AncientGates] Enabling AncientGates v1.9.3.5
    [21:17:40 INFO]: [PlugMan] AncientGates_BungeeTest20 has been loaded and enabled
    .
    [21:17:40 WARN]: [AncientGates] Task #29261 for AncientGates v1.9.3.5 generated
    an exception
    java.lang.NullPointerException
            at org.mcteam.ancientgates.gson.typeadapters.LocationTypeAdapter.deseria
    lize(LocationTypeAdapter.java:43) ~[?:?]
            at org.mcteam.ancientgates.gson.typeadapters.LocationTypeAdapter.deseria
    lize(LocationTypeAdapter.java:1) ~[?:?]
            at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58) ~[spigo
    t.jar:git-Spigot-2ec6f06-7722428]
            at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(Type
    AdapterRuntimeTypeWrapper.java:40) ~[spigot.jar:git-Spigot-2ec6f06-7722428]
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.re
    ad(CollectionTypeAdapterFactory.java:81) ~[spigot.jar:git-Spigot-2ec6f06-7722428
    ]
            at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.re
    ad(CollectionTypeAdapterFactory.java:60) ~[spigot.jar:git-Spigot-2ec6f06-7722428
    ]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(Ref
    lectiveTypeAdapterFactory.java:93) ~[spigot.jar:git-Spigot-2ec6f06-7722428]
            at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.re
    ad(ReflectiveTypeAdapterFactory.java:172) ~[spigot.jar:git-Spigot-2ec6f06-772242
    8]
            at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(Type
    AdapterRuntimeTypeWrapper.java:40) ~[spigot.jar:git-Spigot-2ec6f06-7722428]
            at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapT
    ypeAdapterFactory.java:187) ~[spigot.jar:git-Spigot-2ec6f06-7722428]
            at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapT
    ypeAdapterFactory.java:145) ~[spigot.jar:git-Spigot-2ec6f06-7722428]
            at com.google.gson.Gson.fromJson(Gson.java:803) ~[spigot.jar:git-Spigot-
    2ec6f06-7722428]
            at com.google.gson.Gson.fromJson(Gson.java:768) ~[spigot.jar:git-Spigot-
    2ec6f06-7722428]
            at com.google.gson.Gson.fromJson(Gson.java:717) ~[spigot.jar:git-Spigot-
    2ec6f06-7722428]
            at org.mcteam.ancientgates.Gate.load(Gate.java:335) ~[?:?]
            at org.mcteam.ancientgates.Gates.load(Gates.java:165) ~[?:?]
            at org.mcteam.ancientgates.Plugin$1.run(Plugin.java:176) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot.jar:git-Spigot-2ec6f06-7722428]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    20) [spigot.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-2ec6f06-7722428]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_75]
     
    Code (Text):

    package org.mcteam.ancientgates.gson.typeadapters;

    import java.lang.reflect.Type;

    import org.bukkit.Location;
    import org.bukkit.World;
    //import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializationContext;
    //import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializer;
    //import org.bukkit.craftbukkit.libs.com.google.gson.JsonElement;
    //import org.bukkit.craftbukkit.libs.com.google.gson.JsonObject;
    //import org.bukkit.craftbukkit.libs.com.google.gson.JsonParseException;
    //import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializationContext;
    //import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializer;
    import org.mcteam.ancientgates.Plugin;

    import com.google.gson.JsonDeserializationContext;
    import com.google.gson.JsonDeserializer;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParseException;
    import com.google.gson.JsonSerializationContext;
    import com.google.gson.JsonSerializer;


    public class LocationTypeAdapter implements JsonDeserializer<Location>,
            JsonSerializer<Location> {
        private static final String WORLD = "world";
        private static final String X = "x";
        private static final String Y = "y";
        private static final String Z = "z";
        private static final String YAW = "yaw";
        private static final String PITCH = "pitch";

        public Location deserialize(JsonElement json, Type typeOfT,
                JsonDeserializationContext context) throws JsonParseException {
            JsonObject obj = new JsonObject();// com.google.gson.JsonObject obj =
                                                // json.getAsJsonObject();

            Location location = null;
            World world = this.getWorld(WORLD);//getWorld(obj.get(WORLD).getAsString());

            if (world != null) {
                double x = obj.get(X).getAsDouble();
                double y = obj.get(Y).getAsDouble();
                double z = obj.get(Z).getAsDouble();
                float yaw = obj.get(YAW).getAsFloat();
                float pitch = obj.get(PITCH).getAsFloat();
                location = new Location(world, x, y, z, yaw, pitch);
            }

            return location;
        }

        public JsonObject serialize(Location src, Type typeOfSrc,
                JsonSerializationContext context) {
            JsonObject obj = new JsonObject();

            if (src == null) {
                Plugin.log("Passed location is null in MyLocationTypeAdapter.");
                return obj;
            } else if (src.getWorld() == null) {
                Plugin.log("Passed location's world is null in MyLocationTypeAdapter.");
                return obj;
            }

            obj.addProperty(WORLD, src.getWorld().getName());
            obj.addProperty(X, src.getX());
            obj.addProperty(Y, src.getY());
            obj.addProperty(Z, src.getZ());
            obj.addProperty(YAW, src.getYaw());
            obj.addProperty(PITCH, src.getPitch());

            return obj;
        }

        private World getWorld(String name) {
            World world = Plugin.instance.getServer().getWorld(WORLD);
            return world;
        }

    }
     
     
  6. What's line 43? (LocationTypeAdapter.java:43)
     
  7. Lines 39-49. I think its line 40 that the null error is coming from, and its not showing the error till line 41
    Code (Text):
         
            Location location = null;
            World world = this.getWorld(WORLD);//getWorld(obj.get(WORLD).getAsString());

            if (world != null) {
                double x = obj.get(X).getAsDouble();
                double y = obj.get(Y).getAsDouble();
                double z = obj.get(Z).getAsDouble();
                float yaw = obj.get(YAW).getAsFloat();
                float pitch = obj.get(PITCH).getAsFloat();
                location = new Location(world, x, y, z, yaw, pitch);
            }
     
     
  8. Do a check for obj being null ;)

    That could be the issue
     
  9. Like insert a condition checking if obj is null?
     
  10. yus

    (your error is at line 43)
     
  11. If you're getting an error on one of these lines:

    Code (Text):

                double x = obj.get(X).getAsDouble();
                double y = obj.get(Y).getAsDouble();
                double z = obj.get(Z).getAsDouble();
     
    then it might be that the key doesn't exist in the dictionary (or json object).

    To fix this I believe you can cast the JSONObject to a JSONArray and iterate over it's elements. It's been a while since i've done this so try it:
    Code (Text):

    JSONArray arr = (JSONArray)obj;
    for (int i=0; i < arr.length; i++){
        JSONObject json = (JSONObject) new JSONParser().parse(arr[i].toString());
        System.out.println(json); //this will print out the JSONArray's keys.
    }
     
    Since I don't exactly know, can I see the JSON object your deserializing?
     
    #12 Hyphenical, May 28, 2015
    Last edited: May 28, 2015
  12. Are you still having this issue?
     
  13. It is still not working

    This plugin has been giving me a lot of trouble. It works then it doesn't and different. Sometimes it gives a stack trace error and most of the time it doesn't do what it is supposed to.
     
    #15 tspringst33n, May 29, 2015
    Last edited: May 29, 2015