当前位置: 首页 > 知识库问答 >
问题:

Java语言lang.NumberFormatException:对于输入字符串:“null”[重复]

姜俊友
2023-03-14

我一直得到这个错误:(错误出现在Donasi.class

这是课堂上的Donasi:

 int total = 0;
    for (Transaksi transaksi:donasi)
        total += (Integer.parseInt(transaksi.getMinimal()))*(Integer.parseInt(transaksi.getQuantity()));
        Locale locale = new Locale("in", "ID");
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        txtTotalDonasi.setText(fmt.format(total));

这是类DonationAdapter:

    int minimal = (Integer.parseInt(listData.get(position).getMinimal()))*(Integer.parseInt(listData.get(position).getQuantity()));

    holder.txt_minimal.setText(fmt.format(minimal));

    holder.txt_nama_donasi.setText(listData.get(position).getNamaDonasi());

这是我的日志:

12-28 14:20:36.283 2579-2579/da.glowroz.donationapp E/AndroidRuntime: FATAL EXCEPTION: main
     Process: da.glowroz.donationapp, PID: 2579
     java.lang.RuntimeException: Unable to start activity ComponentInfo{da.glowroz.donationapp/da.glowroz.donationapp.Donasi}: java.lang.NumberFormatException: For input string: "null"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NumberFormatException: For input string: "null"
at java.lang.Integer.parseInt(Integer.java:521)
at java.lang.Integer.parseInt(Integer.java:556)
at da.glowroz.donationapp.Donasi.loadListDonasi(Donasi.java:134)
at da.glowroz.donationapp.Donasi.onCreate(Donasi.java:72)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

如何解决这个问题?

共有3个答案

阎德义
2023-03-14

在解析之前,请尝试检查getMinimum()和getQuantity()上是否为null。

if (transaksi.getMinimal() != null && transaksi.getQuantity() != null) {
 total += (Integer.parseInt(transaksi.getMinimal()))*
        (Integer.parseInt(transaksi.getQuantity()));
}

将解析代码保留在try块中,并捕获NumberFormatException。这也有助于当get最小值()或getQuantity()不为空且不是数字字符串

try {
 total += (Integer.parseInt(transaksi.getMinimal()))*
        (Integer.parseInt(transaksi.getQuantity()));
}catch(NumberFormatException e){
Log.e("TAG","Excepting parsing Integer from string");
}
冯皓
2023-03-14
Caused by: java.lang.NumberFormatException: For input string: "null"

NumberFormatException是当您尝试将字符串转换为数字时可能会引发的异常,其中该数字可能是int、浮点数或任何其他Java数字类型

您将获得空值。它不是int。添加检查

total += (Integer.parseInt(transaksi.getMinimal()))*(Integer.parseInt(transaksi.getQuantity())); // Problem coming from this end

调试您的应用程序。在旁边添加BREAK-POINT。

添加try catch块

try
{
    // Your work
}
catch(NumberFormatException ex)
{
    System.out.println("wrong input");

}
林祯
2023-03-14

变速箱。getMinimal()或transaksi。getQuantity()为空

因此,请在尝试解析前进行检查

也许像

if (transaksi.getMinimal() != null && transaksi.getQuantity() != null) {
     total += (Integer.parseInt(transaksi.getMinimal()))*
            (Integer.parseInt(transaksi.getQuantity()));
}
 类似资料: