java.util.MissingFormatArgumentException: Format specifier: s

南楼画角 提交于 2021-02-11 01:16:10

问题


I am trying to add data to SQLite database in android, But I am getting error, "java.util.MissingFormatArgumentException: Format specifier: s". I tried to figure out the problem but I can't find it.

Button OnClickListener to add data to database.

 addToCart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new Database(getBaseContext()).addToCart(new Order(
                    foodId,
                    foodItem.getName(),
                    quantity.getNumber(),
                    foodItem.getPrice(),
                    foodItem.getDiscount(),
                    foodItem.getImage()
            ));

            Toast.makeText(ItemDetailsActivity.this, "Item added to your basket.", Toast.LENGTH_SHORT).show();

        }
    });

Method to add

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

回答1:


String query = String.format("INSERT INTO OrderDetails(Productid,ProductName,Quantity,Price,Discount,Image) VALUES('%s','%s','%s','%s','%s','%s');",
        order.getProductid(),
        order.getProductName(),
        order.getQuantity(),
        order.getPrice(),
        order.getDiscount());

You have six %s format placeholders but you're supplying only five values.




回答2:


MissingFormatArgumentException

Unchecked exception thrown when there is a format specifier which does not have a corresponding argument or if an argument index refers to an argument that does not exist.

You didn't set a value for the 'Image' parameter



来源:https://stackoverflow.com/questions/48168503/java-util-missingformatargumentexception-format-specifier-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!