Take the 2-minute tour ×

I have a ListView that has an EditText in each row. The user can update the value that is populated in the EditText, and then click a button to refresh the new sum at the top of the ListView. My approach here is to loop through all ListView records and obtain the value of the EditText, perform the necessary calculations, then update the UI. My problem is I keep receiving a NullPointerException on my fifth iteration, even though there is definitely a value populated. It works fine for the first four iterations. Is my code correct and/or should I be taking a different approach for what I'm trying to do:

    button_finish.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            double sumOfAppliedAmounts = 0;
            Bundle extras = getIntent().getExtras();
            double exchange_rate = Double.parseDouble(extras.getString("exchange_rate")) / 100;
            double convertedPaymentAmount;

            if (exchange_rate >= 1)
                convertedPaymentAmount = Double.parseDouble(extras.getString("payment_amount")) / exchange_rate;
                convertedPaymentAmount = Double.parseDouble(extras.getString("payment_amount")) * exchange_rate;

            for (int i = 0; i < (m_listview.getCount()); i++) {
                v = m_listview.getChildAt(i);
                applied_amount = (EditText) v.findViewById(R.id.list_text_applied_amount_value);
                sumOfAppliedAmounts = sumOfAppliedAmounts + Double.parseDouble(applied_amount.getText().toString()); 

            text_remaining_converted_amount.setText(String.valueOf(convertedPaymentAmount - sumOfAppliedAmounts));
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

I am not sure if this is the problem or if you are aware of the ListView procedures but the getChild() method returns values that are relative to the views position as it is shown on the screen, not the entire position relative to the entire Adapter. I suspect that your screen is displaying less views than there are total and this is why you are getting a null pointer.

This post may be the same question as yours:

android ListView mListView.getChildAt(i) is null, how to solve it?

hope this helps.

share|improve this answer
Thanks - this lead me in the right direction. I wasn't aware that you couldn't loop through ALL items of a ListView (visible or or not.) I've done the process on the adapter layer and it's working well. –  bgeveritt Jun 14 '13 at 16:30
Glad it was of some help. I find that in certain situations, some of the UI optimizations that Android does can be a headache. –  Scott Jun 14 '13 at 21:10
add comment

Try below code : -

View v1=m_listview.getChildAt(m_listview.getPositionForView(v)-m_listview.getFirstVisiblePosition());

where v is View of onClick().

Hope It can help you.

share|improve this answer
add comment

Your Answer


