Problem with arranging objects on android

0
EditText[] PNacionales;
float [] Calculo;
int x;

PNacionales = new EditText[] {
            (EditText) findViewById(R.id.PRextra),
            (EditText) findViewById(R.id.PRprimera),
            (EditText) findViewById(R.id.PRsegunda),
            (EditText) findViewById(R.id.PRtercera),
            (EditText) findViewById(R.id.PRcuarta),
            (EditText) findViewById(R.id.PRcanica)};

Every time you type the edittext data is added to the other array.

    for (x=0; x < PNacionales.length; x++) {
    final int index = x;
    PNacionales[x].addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
          Calculo[index] = Float.parseFloat(PNacionales[index].getText().toString());
        }
    });
}
  

Error

 java.lang.NullPointerException: Attempt to write to null array

What I am doing is an array of EditText objects, that is, I have many EditText and every one that is written in one will pass that value to the other array called Calculo ;

    
asked by DoubleM 12.03.2017 в 23:22
source

2 answers

1

You must pass a variable final that can not vary within afterTextChanged to avoid that index may vary. The error is caused because when you receive afterTextChanged , the value of x is greater than the maximum index, try to access item 7 when there are 6.

    for (x=0; x < PNacionales.length; x++) {
        final int index = x;
        PNacionales[x].addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
              Calculo[index] = Float.parseFloat(PNacionales[index].getText().toString());
            }
        });
    }
    
answered by 13.03.2017 в 09:58
1

Regarding the error:

  

ArrayIndexOutOfBoundsException

The problem is obviously when trying to insert a value in an array (Calculation [x]) which does not have the expected dimension, I recommend using the same variable of the index x for the 2 array.

 @Override
            public void afterTextChanged(Editable editable) {
              Calculo[x] = Float.parseFloat(PNacionales[x].getText().toString());
            }

What you can do is to dimension the array Calculo[x] with the measure of the array PNacionales :

Calculo = new float[PNacionales.size];

If you are getting the error:

  

NullPointerException: Attempt to write to null array

initializes the array:

 Calculo = new float[PNacionales.size];

Since you are trying to add a value in an array that has no value, it is not initialized.

    
answered by 13.03.2017 в 18:04