java.lang.NumberFormatException: null(Android Studio 中的错误)
java.lang.NumberFormatException: null (Error in Android Studio)
我在下面收到此错误。我正在从 firebase 和 sqlite 检索我的数据库浏览器。希望你们能帮我解决这个问题。
提前致谢!
这是我的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);
}
}
这是我的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)
检查您的 Cart.java 第 128 行,如以下错误所述:
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
有一行是这样的:
int val = Integer.parseInt(stringValue);
如果您的 stringValue 为 null 或为空,这将引发错误。您需要先检查字符串是否是正确的数字
我在下面收到此错误。我正在从 firebase 和 sqlite 检索我的数据库浏览器。希望你们能帮我解决这个问题。
提前致谢!
这是我的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);
}
}
这是我的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)
检查您的 Cart.java 第 128 行,如以下错误所述:
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
有一行是这样的:
int val = Integer.parseInt(stringValue);
如果您的 stringValue 为 null 或为空,这将引发错误。您需要先检查字符串是否是正确的数字