thchp
thchp

Reputation: 2404

Error instantiating JSONObject from Map: java.lang.NoSuchMethodError

I'm getting an error while using the JSONObject in Java.

I'm trying to instantiate a JSONObject from a Map:

Collection<Faction> factions = FactionColl.get().getAll();

        for(Faction f : factions) {
            String fac_id = f.getId();

            ResultSet count = this.db.query("SELECT COUNT(*) AS count FROM helu_fac WHERE fac_id='" + fac_id + "'");
            int exist = 0;
            while(count.next()) {
                exist = count.getInt("count");
            }

            if(exist == 0) {
                Connection conn = this.db.getConnection();
                PreparedStatement statement = conn.prepareStatement("INSERT INTO helu_fac VALUES(?,?,?,?,?,?,?)");
                statement.setInt(1, 0);
                statement.setString(2, fac_id);
                statement.setString(3, f.getName().toLowerCase());
                statement.setString(4, f.getDescription());
                statement.setString(5, new JSONObject(f.getRelationWishes()).toJSONString());
                statement.setDouble(6, f.getPower());
                statement.setInt(7, 1);

                statement.executeUpdate();
                conn.close();
            } else {

                Connection conn = this.db.getConnection();
                PreparedStatement ps = conn.prepareStatement(
                        "UPDATE helu_fac SET "
                        + "fac_id = ?,"
                        + "name = ?,"
                        + "description = ?,"
                        + "relations = ?,"
                        + "power = ?"
                        + " WHERE fac_id =  \"" + fac_id +"\"");
                    ps.setString(1, f.getId());
                    ps.setString(2, f.getName().toLowerCase());
                    ps.setString(3, f.getDescription());
                    ps.setString(4, new JSONObject(f.getRelationWishes()).toJSONString());
                    ps.setString(5, String.valueOf(f.getPower()));

                    ps.executeUpdate();

                    conn.close();
            }

        }

and i'm getting the following error:

java.lang.NoSuchMethodError: org.json.simple.JSONObject: method <init>(Ljava/util/Map;)V not found

I searched everywhere, but i'm still getting this error.

Thanks for your help !

Upvotes: 0

Views: 4645

Answers (1)

RCB
RCB

Reputation: 845

As the error tells you, org.json.simple.JSONObject does not have a constructor which accepts a Map as the only parameter.

Perhaps you were meaning to use org.json.JSONObject, which does have such a constructor?

Check which JSONObject you have imported at the top of the class.

Upvotes: 1

Related Questions