ListView中的java.lang.NullPointerException问题

ListView中的java.lang.NullPointerException问题,java,android,android-listview,Java,Android,Android Listview,我是Android开发新手,我正在尝试将字符串转换为双精度,然后将其四舍五入到小数点后2位,然后将其转换回字符串以显示在ListView中。这是我的密码: public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){ String expenseCategory = getItem(position).getExpenseCategory();

我是Android开发新手,我正在尝试将字符串转换为双精度,然后将其四舍五入到小数点后2位,然后将其转换回字符串以显示在ListView中。这是我的密码:

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){

            String expenseCategory = getItem(position).getExpenseCategory();
            String expenseAmount = getItem(position).getExpenseAmount();
            String expenseEntryDate = getItem(position).getExpenseTodayEntry();

            Double eA = Double.parseDouble(expenseAmount);
            String eAnew = String.format("%.2f",eA);
            Expenses ex = new Expenses(eAnew, expenseCategory, expenseEntryDate);

            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);

            TextView expenseCategoryTextView = (TextView) convertView.findViewById(R.id.CategoryHistoryTV);
            TextView expenseDateEntryTextView = (TextView) convertView.findViewById(R.id.DateHistoryTV);
            TextView expenseAmountTextView = (TextView) convertView.findViewById(R.id.AmountHistoryTV);

            expenseCategoryTextView.setText(expenseCategory);
            expenseDateEntryTextView.setText(expenseEntryDate);
            expenseAmountTextView.setText(eAnew);

            expenseCategoryTextView.setTextColor(Color.RED);
            expenseDateEntryTextView.setTextColor(Color.RED);
            expenseAmountTextView.setTextColor(Color.RED);

            return convertView;
    }
下面是错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
它运行正常,无需:

Double eA = Double.parseDouble(expenseAmount);
String eAnew = String.format("%.2f",eA);
但是它却给了我一个输出,比如

1000.0000
我理解这可能是一个糟糕的设计,但是由于各种原因,将expenseAmount声明为double在我代码的其他区域不起作用


我尝试使用一个条件来“捕获”错误所说的空值,但到目前为止没有任何效果。

首先,您还没有发布完整的代码,其中您正在从double转换为string,反之亦然,然后根据错误“java.lang.string java.lang.string.trim()'这意味着您正在调用trim函数的字符串为null。因此,在调用此函数trim之前,请确保字符串必须具有一些值。

这里的
费用金额
似乎为null。尝试为列表中的项目指定默认的
expenseamunit
值或处理
null
案例

比如:

或:


在指出问题的地方修改代码,并在那里添加空检查。下面我提到了代码示例

String eAnew = "00.00";
        if(expenseAmount != null && !"null".equals(expenseAmount)){
            Double eA = Double.parseDouble(expenseAmount);
            eAnew = String.format("%.2f",eA);
        }

嗨,布拉姆,我试的时候好像不起作用。你能给我举个例子吗?嗯,看起来你的列表中有费用对象或其他东西(我从代码中看不出来)。在类内部初始化ExpenseAumount属性以保存默认值,如“0”或其他。谢谢您的评论,Bram。我已经解决了这个问题。如果您愿意,您可以查看其他注释以了解我的解决方案。
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    ...
    String expenseAmount = getItem(position).getExpenseAmount();
    ...

    String eAnew;
    if (expenseAmount == null) {
        eAnew = "I don't have an expenseAmount yet!"; // handle the null case

    else {
        Double eA = Double.parseDouble(expenseAmount);
        eAnew = String.format("%.2f", eA);
    }
    Expenses ex = new Expenses(eAnew, expenseCategory, expenseEntryDate);

    ...
}
String eAnew = "00.00";
        if(expenseAmount != null && !"null".equals(expenseAmount)){
            Double eA = Double.parseDouble(expenseAmount);
            eAnew = String.format("%.2f",eA);
        }