0

I'm getting this error below. I'm retrieving my database from firebase and sqlite browser. Hoping that you guys could help me to sort this thing out.

Thank you in advance!

This is my Database.java:

public class Database extends SQLiteAssetHelper {

    private static final String DB_NAME="coolblogDB.db";
    private static final int DB_VER=1;


    public Database(Context context) { super(context, DB_NAME, null, DB_VER); }

    public List<Order> getCarts() {
        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        String[] sqlSelect = {"ProductId", "ProductName", "Quantity", "Price", "Discount"};
        String sqlTable = "OrderDetail";

        qb.setTables(sqlTable);
        Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);

        final List<Order> result = new ArrayList<>();
        if (c.moveToFirst()) {
            do {
                result.add(new Order(c.getString(c.getColumnIndex("ProductId")),
                        c.getString(c.getColumnIndex("ProductName")),
                        c.getString(c.getColumnIndex("Quantity")),
                        c.getString(c.getColumnIndex("Price")),
                        c.getString(c.getColumnIndex("Discount"))
                ));
            } while (c.moveToNext());
        }
        return result;
    }

    public void addToCart(Order order)
    {
        SQLiteDatabase db = getReadableDatabase();
        String query = String.format("INSERT INTO OrderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES('%s','%s','%s','%s','%s');",
                order.getProductId(),
                order.getProductName(),
                order.getQuantity(),
                order.getPrice(),
                order.getDiscount());
        db.execSQL(query);
    }

    public void cleanCart()
    {
        SQLiteDatabase db = getReadableDatabase();
        String query = String.format("DELETE FROM OrderDetail");
        db.execSQL(query);
    }
}

This is my logcat:

10-15 13:48:34.439 12860-12860/com.example.mariamsyafiqah.coolblog2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.mariamsyafiqah.coolblog2, PID: 12860 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mariamsyafiqah.coolblog2/com.example.mariamsyafiqah.coolblog2.Cart}: java.lang.NumberFormatException: null at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:483) at java.lang.Integer.parseInt(Integer.java:556) at com.example.mariamsyafiqah.coolblog2.Cart.loadListDrink(Cart.java:128) at com.example.mariamsyafiqah.coolblog2.Cart.onCreate(Cart.java:71) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2
  • print the stack trace Commented Oct 15, 2017 at 5:10
  • @RahulAgrawal i attached the logcat. Thank you Commented Oct 15, 2017 at 6:57

1 Answer 1

0

Check your Cart.java line 128 like the following error says:

java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Integer.java:483) at java.lang.Integer.parseInt(Integer.java:556) at com.example.mariamsyafiqah.coolblog2.Cart.loadListDrink(Cart.java:128) at 

There is a line with something like this:

int val = Integer.parseInt(stringValue);

Which will throw error if your stringValue is null or empty. You need to do the checking if the string is a correct number or not first

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

3 Comments

This is my line 128: total+=(Integer.parseInt(order.getPrice()))*(Integer.parseInt(order.getQuantity()));
You should check if order.getPrice() and order.getQuantity(‌​) return a valid integer value.
Dear sir, im sorry but im quite lost here. How can i check that my order.getPrice() and order.getQuantity() will return an integer 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.