Gabriel
Gabriel

Reputation: 1838

NoSuchMethodException in Gson JsonArray

So I have stumbled on this error, whenever I try to remove an element from a JsonArray object, i get the NoSuchMethodException. But after looking at it for a solid day, i cannot find the reason for it. The method exists in the compiled .class file in the .jar file that is included in my project. And I'm at loss now. Surely the problem is between the chair and keyboard, but I just don't see it

EDIT: after compiling the project i don't get this error, so this has to do something with how eclipse is managing dependencies, because one project does use gson-2.2.4

I'm using gson 2.3 jar which I included via maven and even downloaded the jar file itself.

Example piece of code where the error is thrown:

else if (current.isJsonObject() && hasBucket(current)) {
    JsonElement buckets = current.getAsJsonObject().get(BUCKETS);
    JsonElement next = null;
    if (buckets.isJsonArray() && buckets.getAsJsonArray().size()>0) {
        JsonArray arr = buckets.getAsJsonArray();
        arr.remove(0); //<--- exception is thrown here
    } else if (buckets.isJsonObject()) {
        Set<Entry<String,JsonElement>> entrySet = buckets.getAsJsonObject().entrySet();
        for (Entry<String, JsonElement> entry : entrySet) {
            next = entry.getValue();
            buckets.getAsJsonObject().remove(entry.getKey());
            break;
        }
    }
}

when i compile my project using maven i can see that maven is using gson 2.3

[INFO] Expanding: /Users/####/.m2/repository/com/google/code/gson/gson/2.3/gson-2.3.jar into /Users/####/Documents/workspace/proj_name/target/assembly/work/gson-2.3.jar

output of javap ~/Downloads/gson-2.3/com/google/gson/JsonArray.class from the jar file that is in classpath of my java project

Compiled from "JsonArray.java"
public final class com.google.gson.JsonArray extends com.google.gson.JsonElement implements java.lang.Iterable {
  public com.google.gson.JsonArray();
  com.google.gson.JsonArray deepCopy();
  public void add(com.google.gson.JsonElement);
  public void addAll(com.google.gson.JsonArray);
  public com.google.gson.JsonElement set(int, com.google.gson.JsonElement);
  public boolean remove(com.google.gson.JsonElement);
  public com.google.gson.JsonElement remove(int); <---- It exists!
  public boolean contains(com.google.gson.JsonElement);
  public int size();
  public java.util.Iterator iterator();
  public com.google.gson.JsonElement get(int);
  public java.lang.Number getAsNumber();
  public java.lang.String getAsString();
  public double getAsDouble();
  public java.math.BigDecimal getAsBigDecimal();
  public java.math.BigInteger getAsBigInteger();
  public float getAsFloat();
  public long getAsLong();
  public int getAsInt();
  public byte getAsByte();
  public char getAsCharacter();
  public short getAsShort();
  public boolean getAsBoolean();
  public boolean equals(java.lang.Object);
  public int hashCode();
  com.google.gson.JsonElement deepCopy();
}

Upvotes: 1

Views: 4979

Answers (2)

Jeffrey Mixon
Jeffrey Mixon

Reputation: 13616

There is something wrong with your build setup.

JsonArray.remove(int) was introduced in version 2.3 of Gson. Most likely what is happening is you are compiling against the correct version (2.3), but the version that is getting packaged in your APK is not the correct version.

Upvotes: 3

eduyayo
eduyayo

Reputation: 2038

You're trying to modify a collection you're iterating.

Apart of avoiding the abuse of calling toThis and toThat, you should iterate the collection and insert in a result one when necessary and then assign the whole thing. One of the elements go null after you've removed a previous one or so.

Upvotes: 0

Related Questions