0

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 !

4
  • Map mymap; is a null reference, other than that the error must lie elsewhere. Can you give more code? (i.e. fresh code, not just this patch) Commented Oct 11, 2015 at 15:27
  • what are the types of key & value of mymap Commented Oct 11, 2015 at 15:28
  • If you make sure your map is not null, try map.toString() instead map only. Commented Oct 11, 2015 at 15:28
  • When i'm trying do to map.toString(), eclipse tell me that the constructor JSONObject(String) is not defined Commented Oct 11, 2015 at 15:32

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.