anonymous23
anonymous23

Reputation: 55

java.lang.NullPointerException problem in ListView

I'm new to Android development and I'm trying to convert a string to a double, then round it off to 2 decimal places, then convert it back to string to display in a ListView. Here is my code:

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;
    }

And here is the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference

it runs properly without:

Double eA = Double.parseDouble(expenseAmount);
String eAnew = String.format("%.2f",eA);

But instead it gives me an output like

1000.0000

I understand that it may be bad design, however declaring the expenseAmount as double does not work in other areas of my code for various reasons.

I've tried to use a condition to "catch" the null value as the error says but so far nothing works.

Upvotes: 0

Views: 62

Answers (3)

Bram Stoker
Bram Stoker

Reputation: 1232

It seems that expenseAmount is null here. Try to give your items in your list a default expenseAmount value or handle the null case.

Like:

public class Expense { // whatever your class is

    private String expenseAmount = "0"; // set a default value, so it's not null

    ...
}

Or:

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);

    ...
}

Upvotes: 0

Ashish Karn
Ashish Karn

Reputation: 1143

Modify your code where you have pointed the problem and add Null check there. Below I have mentioned the code sample.

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

Upvotes: 1

Daniyal Irfan
Daniyal Irfan

Reputation: 66

First of all You have not posted your complete code in which you are doing converting from double to string and vice versa and second According to error 'java.lang.String java.lang.String.trim()' it means your string is null on which you are calling trim function.So make sure that string must have some value before calling this function trim.

Upvotes: 0

Related Questions