0

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.

3 Answers 3

1

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);
        }
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

    ...
}

3 Comments

Hi Bram, doesn't seem to work when I tried it. Could you give me an example?
Well, it looks like you have Expense objects or something in your list (I cannot tell from the code). Inside the class initialize the expenseAmount property to hold a default value, like "0" or something.
Thanks for your comment, Bram. I've fixed the solution. If you'd like, you can look at the other comment for my solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.