-3
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference

This is the line where the exception is pointing to

  String tot=avg[i+2];
             total=Float.parseFloat(tot);

and

 adapter=new VivaAdapter(Attendance.this,gedata());

I am recieving data in form of string and then converting it into float ,but the app runs fine first time but second time it throws exception

I tried to change avg[i+2].trim() but dint worked

App crashes every alternative time

Eception log:

/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.rushabh123453.attendancepict, PID: 643
                                                                                      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
                                                                                          at java.lang.StringToReal.parseFloat(StringToReal.java:304)
                                                                                          at java.lang.Float.parseFloat(Float.java:306)
                                                                                          at com.example.rushabh123453.attendancepict.Attendance$Networking1.onPostExecute(Attendance.java:189)
                                                                                          at android.os.AsyncTask.finish(AsyncTask.java:636)
                                                                                          at android.os.AsyncTask.access$500(AsyncTask.java:177)
                                                                                          at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:211)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5389)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Rushabh Oswal
  • 25
  • 1
  • 1
  • 8

2 Answers2

0

You should check if String is null. You can't call method trim() String with null value.

kostyabakay
  • 1,649
  • 2
  • 21
  • 32
  • i am requesting data from html page and data is not empty,it is crashing every alternative time only – Rushabh Oswal May 05 '17 at 11:25
  • Its tricky,see i have a login that takes user id and password and then post in on html page,and response is stored in string array and showed in textview,now when we press back button and again press login,it crashes with null exception – Rushabh Oswal May 05 '17 at 12:00
  • Problem solved:- actually i was having a counter which was static , it counts the number of subject and then stores data,so if 12 subjects where there it ws showing 24 eventhough i pressed the back button and hence this error, so here i got a point that static variables store data even if we close that intent – Rushabh Oswal May 05 '17 at 12:11
0

Add a null check before parsing into float

if(!TextUtils.isEmpty(tot)){
   total=Float.parseFloat(tot);
}