1

I have a method to get all the items in the database, but I don't know what is the problem, it always say java.lang.numberformatexception: invalid double: "". Any help would be highly appreciated.

My code:

@Override
        public void processFinish(String s) {
            productList = new JsonConverter<Products>().toArrayList(s, Products.class);
            BindDictionary dic = new BindDictionary();

            dic.addStringField(R.id.tvName, new StringExtractor<Products>() {
                @Override
                public String getStringValue(Products item, int position) {
                    return item.name;
                }
            });
            dic.addStringField(R.id.tvDesc, new StringExtractor<Products>() {
                @Override
                public String getStringValue(Products item, int position) {
                    return item.description;
                }
            }).visibilityIfNull(View.GONE);

            dic.addStringField(R.id.tvPrice, new StringExtractor<Products>(){

                @Override
                public String getStringValue(Products item, int position) {
                    return ""+item.price;

                    //return String.valueOf(item.price);
                }
            });

            dic.addDynamicImageField(R.id.ivImage, new StringExtractor<Products>() {
                @Override
                public String getStringValue(Products item, int position) {
                    return item.img_url;
                }
            }, new DynamicImageLoader() {
                @Override
                public void loadImage(String url, ImageView img) {
                    //Set image
                    ImageLoader.getInstance().displayImage(url, img);
                }
            });

        }

Products.java

public class Products implements Serializable {

@SerializedName("itemID")
public int id;

@SerializedName("productName")
public String name;

@SerializedName("descrt")
public String description;

@SerializedName("price")
public double price;

@SerializedName("my_img")
public String img_url;

}

Thanks in advance.

5
  • 2
    I think this is not the relavent code for exception, you are not using double any where Commented Mar 20, 2018 at 11:22
  • what do you mean? i really don't know what is the reason of that problem Commented Mar 20, 2018 at 11:25
  • Could you share the Products.java Commented Mar 20, 2018 at 11:26
  • are you converting any value to double ? Commented Mar 20, 2018 at 11:27
  • I think, i have nothing to conver Commented Mar 20, 2018 at 11:36

2 Answers 2

1

This error because of your Products.java

There is a double type property in your Products.java that called price,

When you convert the json object to java object the both types must be same. In your case,

you get a

{ 
 ...
 price:"",
 ....
}

in your json object. But the java side, it waits for double value. You could send price:0.0 instead of price:"" or write your own converter.

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

3 Comments

i will put 0.0 on that blank qoute?
Yes on the JSON side, change blank qoute to 0.0 Because the price variable is not a string.
You can generate your pojo object via jsonschema2pojo.org link. Just paste your JSON example, It'll generate java object for you
1

first see the datatype for price which you want to send.i also faced same situation long back i resolved it by passing data what is expected or before sending data parse the data to required type.

  @Override
            public double getStringValue(Products item, int position) {
                return ""+item.price;

                //return String.valueOf(item.price);
            }

4 Comments

in price you returning string value parse that string to double you problem will be solved
how sir can you help me please.
please add complete pojo class i will see and i will provide needful solution
build pojo based on json raw data using json schema to pojo converter copy the output pojo created based on json raw data so that depending on data type it takes you have to pass that value

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.