两个数组维ArrayIndexOutOfBoundException和NullPointerException

时间:2015-11-30 05:14:23

标签: android exception nullpointerexception indexoutofboundsexception

我写了一个计算一些权重的代码。它们是整数权重。 我需要在每次点击按钮时保存它们。 请帮我。我不知道为什么当我第二次按下按钮时编译器给我一个错误。这是我的完整代码:

    public class TrainingActivity extends Activity {

    private EditText etIn1, etIn2, etDesired;
    private TextView prevInput;

    int W[][] = new int[2][];
    int X[][] = new int[30][];

    int w0=0, w1=0, w2=0, p=1, sum=0, clicks=0;

    private Button nxtData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.training_activity);

    View backgroundImage = findViewById(R.id.background);
    Drawable background = backgroundImage.getBackground();
    background.setAlpha(40);

    etIn1= (EditText) findViewById(R.id.etInput1);
    etIn2 = (EditText) findViewById(R.id.etInput2);
    etDesired = (EditText) findViewById(R.id.etDesired);

    prevInput = (TextView) findViewById(R.id.prevInput);

    nxtData = (Button) findViewById(R.id.nextData);
    nxtData.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            int sum = 0;
            ++clicks;

            int intetIn1 = Integer.parseInt(etIn1.getText().toString());
            int intetIn2 = Integer.parseInt(etIn2.getText().toString());
            int intetDesired = Integer.parseInt(etDesired.getText().toString());

            X[clicks-1] = new int[] {intetIn1, intetIn2, 1};

            prevInput.setText("Last Inputs: (" + intetIn1 + ", " + intetIn2 +
                    ", " + intetDesired + ")");

            if(clicks == 1) {
            if(intetDesired == 1) {
                W[0] = new int[] {intetIn1, intetIn2, 1};
                W[1] = W[0];
            } else if(intetDesired == (-1)){
                W[0] = new int[] {-intetIn1, -intetIn2, -1};
                W[1] = W[0];
            }
            } else if(clicks > 1) {                 
                for(int i=0; i<3; i++){
                    sum = sum + W[clicks-1][i] * X[clicks-1][i];
                } if(sum>0 && intetDesired==1) {
                    W[clicks] = W[clicks-1];
                } else if(sum<0 && intetDesired==(-1)) {
                    W[clicks] = W[clicks-1];
                } else if(sum<=0 && intetDesired==1) {
                    for(int i=0; i<3; i++) {
                    W[clicks][i] = W[clicks-1][i] + X[clicks-1][i];
                }
                } else if(sum>=0 && intetDesired==(-1)) {
                    for(int i=0; i<3; i++) {
                        W[clicks][i] = W[clicks-1][i] - X[clicks-1][i];
                    }
                }
            } 

            etIn1.setText("");
            etIn2.setText("");
            etDesired.setText("");

        }
    });


}}

这是它抛出的异常:

java.lang.ArrayIndexOutOfBoundsException: length=2; index=2

UPDATEEEEEEEE 我通过将W [2] []更改为W [20] []来修复arrayindexoutofboundexception的问题。但是在一些点击中它给了我这个错误:

java.lang.NullPointerException

并且不清楚哪些点击次数。有时它在第二次点击中。或者有时候是第四次点击。请帮忙。

1 个答案:

答案 0 :(得分:0)

W [clicks] = W [clicks - 1];

在上面一行中,您得到了错误,因为您只定义了数组的大小

int W [] [] = new int [2] [];

所以它只分配了W [0] []和W [1] []

当点击第二次变量时,点击值为2,则编译器给出ArrayIndexOutOfBoundException

<强> EDITED ........................................... ..................

由于你的逻辑错误而且没有正确的方法来构建二维数组,你有空值。请使用调试工具查找实际问题以实现逻辑并使用二维数组,如下面的例子在java或android中:

 List<List<Integer>> triangle = new ArrayList<List<Integer>>();

List<Integer> row1 = new ArrayList<Integer>(1);
row1.add(2);
triangle.add(row1);

List<Integer> row2 = new ArrayList<Integer>(2);
row2.add(3);row2.add(4);
triangle.add(row2);

triangle.add(Arrays.asList(6,5,7));
triangle.add(Arrays.asList(4,1,8,3));

System.out.println("Size = "+ triangle.size());
for (int i=0; i<triangle.size();i++)
    System.out.println(triangle.get(i));