1

Here I am trying to get values of EditText from my custom ListView and adding all values of EditText. It works well when list item is 3 or less-than 3 but when item of list view is 4 or more-than 4 then it throws a null pointer exception.I think it occurs because of invisible item of ListView ....but how can i resolve it ???

etPayAmount.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

    }

    @Override
    public void onTextChanged(CharSequence cs, int i, int i2, int i3) {

    }

    @Override
    public void afterTextChanged(Editable editable) {

            int count = lvRePayment.getCount();// count number of item in listview
            double sum = 0;
        Toast.makeText(getApplicationContext(),"child of listview: "+count,Toast.LENGTH_SHORT).show();
            for (int i = 0; i <count; i++) {
                LinearLayout layout = (LinearLayout) lvRePayment.getChildAt(i);// get child from first linear layout

                LinearLayout innerLayout = (LinearLayout) layout.getChildAt(1); // get child from inner linear layout

                if (innerLayout.getChildCount() > 1) {
                    EditText editText = (EditText) (innerLayout).getChildAt(1);
                    try {
                        double totalPayment = Double.parseDouble(editText.getText().toString());
                        sum += totalPayment;
                    }
                    catch (Exception e) {
                    }
                }
            }
            etTotalPay.setText(String.valueOf(sum));
            pay = sum;
            double balance = totalAmount - pay;
            etBalance.setText(String.valueOf(balance));

    }
});

LOGCAT

05-02 00:48:21.834  22968-22968/com.syemasoft.shopmanager E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.syemasoft.shopmanager.Activity_RePayment$RePaymentAdapter$1.afterTextChanged(Activity_RePayment.java:241)
            at android.widget.TextView.sendAfterTextChanged(TextView.java:7346)
            at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9017)
            at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
            at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
            at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
            at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
            at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:674)
            at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:198)
            at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:185)
            at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
            at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5391)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
  • Which line is giving you the null pointer exception? – AdamMc331 May 01 '15 at 18:58
  • here logcat is added ..... @MaheeraJazi-newaccount- – Jakir Hossain May 01 '15 at 19:07
  • LinearLayout innerLayout = (LinearLayout) layout.getChildAt(1); this line is giving the null pointer exception ..... 2nd line under for loop..... @McAdam331 – Jakir Hossain May 01 '15 at 19:08
  • @JakirHossain that means lvRePayment.GetChildAt(i) is returning a null object. – AdamMc331 May 01 '15 at 19:12
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – AdamMc331 May 01 '15 at 19:13
  • You should add a condition: if (layout.getChildCount() > 1) { // here you can use layout.getChildAt(1) and it won't throw an exception}. – Jerry May 01 '15 at 19:15
  • This might also be helpful: [Getting child elements from LinearLayout](http://stackoverflow.com/questions/6615723/getting-child-elements-from-linearlayout) – Jerry May 01 '15 at 19:17
  • 1
    `ListView` uses optimization where elements outside of the current screen destroy their views to save memory (the views are actually recycled). I guess this can be your problem. Use `ScrollView` if you need all elements to be present in memory. – StenSoft May 01 '15 at 19:27
  • Plus one for @StenSoft also, always `.trim()` user input, unless you prefer spaces messing up your .parse() – Petro May 01 '15 at 21:21

0 Answers0