-1

I'm trying to read JSON data from an asset and convert it to map. However, it does not seem to convert and gives me errors whenever I run the code.

File is in the assets folder, named "json_database_main.json"

public Map loadMapFromAsset() {
    String json = null;
    Map objects_map = new HashMap();
    Product product;
    Log.d("Started ldmpfass", "Yeah");
    try {
        InputStream is = getAssets().open(file_database);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
        JSONArray jsonArray = new JSONArray(json);
        Log.d("Sumthin", Integer.toString(size));
        for(int i = 0; i < jsonArray.length(); i++)
        {
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            Log.d("JSobject:", jsonObj.toString());
            product = mapper.readValue(jsonObj.toString(), Product.class);
            Log.d("Object:", product.getCode());
            objects_map.put(product.getCode(), product);


        }
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return objects_map;
}

Here's the message I got in logcat:

D/JSobject:: {"code":"0001","description":"Pienas","type":"Pieno produktai"}
W/System.err: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.matas.checksv3.Product: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
W/System.err:  at [Source: {"code":"0001","description":"Pienas","type":"Pieno produktai"}; line: 1, column: 2]
W/System.err:     at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
W/System.err:     at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(    DeserializationContext.java:1012)
W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1203)
W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:314)
W/System.err:     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)
W/System.err:     at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
W/System.err:     at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)
W/System.err:     at com.matas.checksv3.MainActivity.loadMapFromAsset(MainActivity.java:152)
W/System.err:     at com.matas.checksv3.MainActivity$2.onClick(MainActivity.java:85)
W/System.err:     at android.view.View.performClick(View.java:5610)
W/System.err:     at android.view.View$PerformClick.run(View.java:22260)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:154)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6077)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
D/AndroidRuntime: Shutting down VM

I know that my code is ugly, but this is my first android application, I'll try to get better, so point out anything that I could do better.

I edited my code and now I get this. I'm not sure what class Product needs. Here it is:

public class Product {

    String code;
    String description;
    String type;
    public Product(String code, String description, String type)
    {
        this.code = code;
        this.description = description;
        this.type = type;
    }
    public Product(String code, String description)
    {
        this.code = code;
        this.description = description;
    }
    public String getCode()
    {
        return code;
    }
    public String getType() {
        return type;
    }

    public String getDescription() {
        return description;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setType(String type) {
        this.type = type;
    }
}

EDIT: Got it working! Added: public Product(){} to Product class.

Sufian
  • 6,405
  • 16
  • 66
  • 120
  • Did you check the values on `getAssets()` and your `json` variable to say that the file hasn't been found? – juliano.net Oct 12 '16 at 17:36
  • share code of your MainActivity$2.onClick; you might be calling Product.getDescription() in onClick method where product is null. – Jugal Panchal Oct 12 '16 at 18:34
  • from this code, the obvious explanation is what is given in the error: there is no `ThisHasNoUse` keys in your json. post your json if you think this error should not occur. – njzk2 Oct 12 '16 at 18:40
  • @user13 and JugalPanchal: the NPE is just a consequence of the first json error, the NPE is not relevant to the current problem – njzk2 Oct 12 '16 at 18:42
  • "However, it does not find the asset file". It *does* find the file, as demonstrated by the log of the size, and as demonstrated by the fact that the `new JSONArray(json)` line does not crash (i.e. there is indeed a file, and indeed a json array in it) – njzk2 Oct 12 '16 at 18:43
  • Instead of updating your question with an answer (scroll below to find the box below "Your Answer"). And please don't write "solved" in your question title. Once you [mark an answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) as correct, your question will show up solved anywhere it is listed. – Sufian Oct 13 '16 at 12:36
  • But if in case you solved your question with the help of some other answer, please mark THAT answer as correct (you may request the answerer to update the answer to add relevant details if it is required). Marking someone's answer indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – Sufian Oct 13 '16 at 12:39

1 Answers1

0

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.matas.checksv3.Product: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)

Add an empty constructor to your data class.

Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124