Reputation: 245
Can somebody explain to me why I'm getting java.lang.IllegalStateException: Could not execute method of the activity when trying to click the button to get data from database?
my xml code has an onClick like this:
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/btnDelete"
android:layout_weight="1"
android:onClick="onClick_DeleteAll"/>
my mainactivity code:
public void onClick_DeleteAll(View view){
DBTools dbTools = new DBTools(getApplicationContext())
float total = dbTools.total();
}
my database class(DBTools.java) code:
public float total(){
float total = 0;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT Sum("+ PRICE +") FROM " + TABLE_ITEM_SELL, null);
if (cursor.moveToFirst()){
total = Float.parseFloat(cursor.getString(0));
}
return total;
}
I don't know what's wrong because It only happens when the method that I try to execute will return data. Like the code above, it returns float.
Error:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3591)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.lang.StringToReal.parseFloat(StringToReal.java:285)
at java.lang.Float.parseFloat(Float.java:300)
at com.jcarlapp.carl.amystore.DBTools.total(DBTools.java:151)
at com.jcarlapp.carl.amystore.MainActivity.onClick_DeleteAll(MainActivity.java:306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 1263
Reputation: 152787
If the table doesn't contain any data, SUM(column)
will return SQL NULL
.
getString()
on a NULL
column value will return Java null
.
parseFloat(null)
causes the exception.
Consider checking for a null value before trying to parse it.
Upvotes: 1
Reputation: 1231
It is caused by NPE
Caused by: java.lang.NullPointerException
at java.lang.StringToReal.parseFloat(StringToReal.java:285)
at java.lang.Float.parseFloat(Float.java:300)
at com.jcarlapp.carl.amystore.DBTools.total(DBTools.java:151)
It seems like cursor.getString(0)
returns null
in method total.
I assume that you have no rows in the table to calculate sum.
Upvotes: 2