0
public class MainActivity extends AppCompatActivity {


TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    textView = (TextView) findViewById(R.id.textView1);



    FloatingActionButton fab = (FloatingActionButton) 
findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
//I am getting error on this line becuse ACC_id variable is double and the data coming from server might have some blank value so it is giving me exception  
            AccountDetailsList list = new Gson().fromJson(getData(), AccountDetailsList.class);
                    Log.d("MAIN_ACTIVITY", list.getAccountDetails().size() + " ");
                    for (AccountDetails accountDetails : list.getAccountDetails()) {
                        Log.d("MAIN_ACTIVITY", accountDetails.getCUSTOMER_NAME());
                    }     
});
// this is the sample json data which is coming from server
public String getData() {
    return "{\"ACCOUNT_DETAILS\":[{\"ACC_ID\":\"\",\"ACC_NUMBER\":\"\",\"CUSTOMER_NAME\":\"\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":1,\"ACC_NUMBER\":12345,\"CUSTOMER_NAME\":\"Cheenu\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":2,\"ACC_NUMBER\":67890,\"CUSTOMER_NAME\":\"Dhruv\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":3,\"ACC_NUMBER\":13467,\"CUSTOMER_NAME\":\"Chetan\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":4,\"ACC_NUMBER\":16789,\"CUSTOMER_NAME\":\"Darsh\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":5,\"ACC_NUMBER\":98764,\"CUSTOMER_NAME\":\"Arsh\",\"CONTACT_NO\":\"\"},{\"ACC_ID\":6,\"ACC_NUMBER\":130500,\"CUSTOMER_NAME\":\"Madhur\",\"CONTACT_NO\":\"\"}]}";
}}

I am getting the following exception when I am parsing the JSON data on this line: exception

FATAL EXCEPTION: main Process: tfs.anonestep.com.tfs, PID: 26604 java.lang.NumberFormatException: Invalid double: ""at java.lang.StringToReal.invalidReal(StringToReal.java:63) at java.lang.StringToReal.parseDouble(StringToReal.java:267) at java.lang.Double.parseDouble(Double.java:301) at com.google.gson.stream.JsonReader.nextDouble(JsonReader.java:925) at com.google.gson.Gson$3.read(Gson.java:260) at com.google.gson.Gson$3.read(Gson.java:254) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217) at com.google.gson.Gson.fromJson(Gson.java:814) at com.google.gson.Gson.fromJson(Gson.java:779) at com.google.gson.Gson.fromJson(Gson.java:728) at com.google.gson.Gson.fromJson(Gson.java:700) at tfs.anonestep.com.tfs.MainActivity$1.onResponse(MainActivity.java:49) at tfs.anonestep.com.tfs.MainActivity$1.onResponse(MainActivity.java:42) at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5539) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Model Classes

    public class AccountDetailsList {
@SerializedName("ACCOUNT_DETAILS")
ArrayList<AccountDetails> accountDetails;

public ArrayList<AccountDetails> getAccountDetails() {
    return accountDetails;
}

public void setAccountDetails(ArrayList<AccountDetails> accountDetails) {
    this.accountDetails = accountDetails;
}
//Model class
}


public class AccountDetails {

double ACC_ID;
String CUSTOMER_NAME;
String ACC_NUMBER;
String CONTACT_NO;
}
  • 1
    please put proper `JSON` response which you receive, Share you error log – Farmer Mar 25 '17 at 07:26
  • The exception is clearly telling you: "invalid double for an empty string", so just clean up your JSON first by removing invalid elements from the account details array. The only element in your JSON matching this condition is `$.ACCOUNT_DETAILS[0]`; – Lyubomyr Shaydariv Mar 25 '17 at 07:46
  • Another option, I guess, you might want to use is using `null` rather than `""` to denote a missing value, if the JSON is generated by you. Where does the JSON come from? – Lyubomyr Shaydariv Mar 25 '17 at 07:56
  • @LyubomyrShaydariv data is coming from server i can't control on that – user2673923 Mar 25 '17 at 08:22
  • @Shailesh i have write proper json response coming from server – user2673923 Mar 25 '17 at 08:23
  • Check this http://stackoverflow.com/questions/33435683/deserialize-with-gson-and-null-values – Farmer Mar 25 '17 at 08:36
  • @user2673923 How do you want to deal with invalid JSONs? Make `""` turn into `null` so no `NumberFormatException` could occur, or even evict the whole invalid element from the array so the array could have single (in your case) but valid element? – Lyubomyr Shaydariv Mar 25 '17 at 08:39

0 Answers0