0

i am trying to delete an event from calendar so i am fetching the event id from the database and sending them to local calendar to delete them,

when i try to execute them it showing me error like

logcat

03-22 08:29:41.474 27018-27018/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
       Process: com.example.myapplication, PID: 27018
       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.Schedule_Delete}: java.lang.NumberFormatException: For input string: " "
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
           at android.app.ActivityThread.-wrap14(ActivityThread.java)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
           at android.os.Handler.dispatchMessage(Handler.java:102)
           at android.os.Looper.loop(Looper.java:154)
           at android.app.ActivityThread.main(ActivityThread.java:6776)
           at java.lang.reflect.Method.invoke(Native Method)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
        Caused by: java.lang.NumberFormatException: For input string: " "
           at java.lang.Long.parseLong(Long.java:432)
           at java.lang.Long.parseLong(Long.java:485)
           at com.example.myapplication.Schedule_Delete.Event_delete(Schedule_Delete.java:99)
           at com.example.myapplication.Schedule_Delete.writeCalendarEvent(Schedule_Delete.java:76)
           at com.example.myapplication.Schedule_Delete.onCreate(Schedule_Delete.java:36)
           at android.app.Activity.performCreate(Activity.java:6956)
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
           at android.app.ActivityThread.-wrap14(ActivityThread.java) 
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
           at android.os.Handler.dispatchMessage(Handler.java:102) 
           at android.os.Looper.loop(Looper.java:154) 
           at android.app.ActivityThread.main(ActivityThread.java:6776) 
           at java.lang.reflect.Method.invoke(Native Method) 
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 

android code

private void writeCalendarEvent ()
{
    String jsonString = getIntent().getStringExtra("rootJson");
    String data = "";
    try {
        JSONObject jsonRootObject = new JSONObject(jsonString);
        JSONArray jsonArray = jsonRootObject.getJSONArray("NEW_SCHEDULE");
        Event_id = new String[jsonArray.length()];

        for (L = 0; L < jsonArray.length(); L++) {
            JSONObject jsonObject = jsonArray.getJSONObject(L);
             Event_id[L] = jsonObject.getString("Event_id");
            Event_delete(Event_id[L]);
        }
    } catch (JSONException e)
    {
        e.printStackTrace();
    }
}

private void Event_delete(String s)
{
    String id;

    if(s.equals(""))
    {
        Toast.makeText(context, "id is empty"+s, Toast.LENGTH_LONG).show();

    }
    else
    {
        Toast.makeText(context, "id"+s, Toast.LENGTH_LONG).show();

        id=s;
        long eventID = Long.parseLong(id);

        ContentResolver cr = getContentResolver();
        ContentValues values = new ContentValues();
        Uri deleteUri = null;
        deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
        int rows = getContentResolver().delete(deleteUri, null, null);
        Log.i("identifier", "Rows deleted: " + rows);
    }
}

This is my code i fetching the event id from the database and using them to delete the event in the calendar

in the database i have no value for "Event_id[L]" so i try to put check in " private void Event_delete(String s)" and try to execute the function, but the problem is it not executinng the if part executing the else part and showing me this error

 Caused by: java.lang.NumberFormatException: For input string: " "
       at java.lang.Long.parseLong(Long.java:432)
       at java.lang.Long.parseLong(Long.java:485)
       at com.example.myapplication.Schedule_Delete.Event_delete(Schedule_Delete.java:99)
       at com.example.myapplication.Schedule_Delete.writeCalendarEvent(Schedule_Delete.java:76)
       at com.example.myapplication.Schedule_Delete.onCreate(Schedule_Delete.java:36)

can anyone help me to fix this error!

Komal12
  • 3,340
  • 4
  • 16
  • 25
  • possible duplicate https://stackoverflow.com/questions/18711896/how-to-resolve-java-lang-numberformatexception-for-input-string-n-a – ZarNi Myo Sett Win Mar 22 '18 at 03:30
  • 1
    Possible duplicate of [How to resolve java.lang.NumberFormatException: For input string: "N/A"?](https://stackoverflow.com/questions/18711896/how-to-resolve-java-lang-numberformatexception-for-input-string-n-a) – AskNilesh Mar 22 '18 at 03:31

2 Answers2

0

So, parseLong getting null. I guess thats why it cannot parse to Long. Maybe problem is you should add if (s == null || s.trim().equals("")) to prevent null reference.

0

Use trim method it will remove extra space form string.

id = id.trim();

and add a try catch for safety.

    try {
        eventID = Long.parseLong(id);
    } catch (ParseException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50