尝试从空数组中读取

时间:2018-09-17 14:42:03

标签: java android arrays string nullpointerexception

这个问题可能是重复的,但是我没有找到关于虚拟问题的虚拟解释。 我现在放弃了,我试图使其正常运行,但似乎可以做到。 您如何处理

问题是我得到

  

java.lang.NullPointerException:尝试从空数组读取

使用我的transformChoices方法

我知道一些C,但是Java完全不同,我对此一无所知。我仍然很惊讶,在我努力工作以至于没有值之后,某个地方有一个空值。 到目前为止,我的代码很乱,我试图绕过null指针异常。

一些注意事项:如果您有一个更好的主意,请随机调整字符串,让我知道。

另外,如果您有一种经过测试的工作方式可以将2d数组有意传递给其他活动,那也会对我产生影响。目前,我将二维数组转换为一维数组。它可能不是最好的,但是我不必为捆绑包和其他东西而苦恼。我搜索了一种使用Intent和2d数组的方法,但找不到适合我的方法。

请记住,这只是一个原型,数组将具有约1000个值。如果您知道与他们合作的更好更快的方法,请分享。

问题出在这里

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.lang.Math;

public class LevelsActivity extends Activity {

private QuestionLibrary mQuestionLibrary = new QuestionLibrary();
int iSize = mQuestionLibrary.mQuestion.length;

private String[] sNewQuestion = new String[iSize];
private String[][] sNewChoice = new String[iSize][4];
private String[] sNewAnswer = new String[iSize];

public String[] sFinalQuestion;
public String[][] sFinalChoice;
public String[] sChoice0;
public String[] sChoice1;
public String[] sChoice2;
public String[] sChoice3;
//public String[] sChoice4;

public String[] sFinalAnswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_levels);

    for (int i = 0; i < iSize ; i++) {

        sNewQuestion[i] = mQuestionLibrary.getQuestion(i);

        sNewChoice[i][0] = mQuestionLibrary.getChoice1(i);
        sNewChoice[i][1] = mQuestionLibrary.getChoice2(i);
        sNewChoice[i][2] = mQuestionLibrary.getChoice3(i);
        sNewChoice[i][3] = mQuestionLibrary.getChoice4(i);
        //sNewChoice[i][4] =mQuestionLibrary.getChoice5(i);

        sNewAnswer[i] = mQuestionLibrary.getCorrectAnswer(i);
    }

    configureCardiovascularButton();
}

private void configureCardiovascularButton() {

    Button mButtonCardiovascular;
    mButtonCardiovascular = findViewById(R.id.cadiovascularButton);

    //Start CardiovascularButton Listener
    mButtonCardiovascular.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int iFrom = 0;
            int jTo = 4;

            shuffleQuestionsChoicesAnswers(iFrom, jTo);

            Intent intent = new Intent(getBaseContext(), MainQuestionsActivity.class);
            intent.putExtra("Questions", sFinalQuestion);
            intent.putExtra("Answer", sFinalAnswer);
            intent.putExtra("Choice0", sChoice0);
            intent.putExtra("Choice1", sChoice1);
            intent.putExtra("Choice2", sChoice2);
            intent.putExtra("Choice3", sChoice3);
            //intent.putExtra("Choice4", sChoice4);


            startActivity(intent);
        }

    });
    //End CardiovascularButton Listener


}

public void shuffleQuestionsChoicesAnswers(int i, int j) {

    String[] sFinalQuestion = new String[j+1];
    String[][] sFinalChoice = new String[j+1][4];
    String[] sFinalAnswer = new String[j+1];

    for (int a = i; a <= j; a++){

        sFinalQuestion[a] = "";

        sFinalChoice[a][0] = "";
        sFinalChoice[a][1] = "";
        sFinalChoice[a][2] = "";
        sFinalChoice[a][3] = "";
        //sFinalChoice[a][4] = "";

        sFinalAnswer[a] = "";
    }

    int range = j - i + 1;

    for (int z = i; z <= j/2; z++) {

        while (true){

            int rand = (int) (Math.random() * range) + i;

            if (sFinalQuestion[z] == null){
                sFinalQuestion[z] = "";
            }
            if (sNewQuestion[rand] == null) {
                sNewQuestion[rand] = "";
            }
            if (sFinalChoice[rand][0] == null) {
                sFinalChoice[rand][0] = "";
            }
            if (sFinalChoice[rand][1] == null) {
                sFinalChoice[rand][1] = "";
            }
            if (sFinalChoice[rand][2] == null) {
                sFinalChoice[rand][2] = "";
            }
            if (sFinalChoice[rand][3] == null) {
                sFinalChoice[rand][3] = "";
            }
            //if (sFinalChoice[rand][4] == null) {
            //    sFinalChoice[rand][4] = "";
            //}

            if ( (sFinalQuestion[z].length() == 0) && (sNewQuestion[rand].length() != 0) ) {

               sFinalQuestion[z] = sNewQuestion[rand];
               sNewQuestion[rand] = "";

               sFinalChoice[z][0] = sNewChoice[rand][0];
               sFinalChoice[z][1] = sNewChoice[rand][1];
               sFinalChoice[z][2] = sNewChoice[rand][2];
               sFinalChoice[z][3] = sNewChoice[rand][3];
               //sFinalChoice[z][4] = sNewChoice[rand][4];
               sFinalAnswer[z] = sNewAnswer[rand];
               break;
            }
        }
    }

    for (int x = j; x > j/2; x--){
        for (int y = 0; y < j; y++) {

            if (sFinalQuestion[x] == null){
                sFinalQuestion[x] = "";
            }
            if (sNewQuestion[y] == null) {
                sNewQuestion[y] = "";
            }

            if (sFinalQuestion[x].length() == 0 && sNewQuestion[y].length() != 0) {
                sFinalQuestion[x] = sNewQuestion[y];

                sFinalChoice[x][0] = sNewChoice[y][0];
                sFinalChoice[x][1] = sNewChoice[y][1];
                sFinalChoice[x][2] = sNewChoice[y][2];
                sFinalChoice[x][3] = sNewChoice[y][3];
                //sFinalChoice[x][4] = sNewChoice[y][4];

                sFinalAnswer[x] = sNewAnswer[y];

            }
        }
    }
    transformChoices(i, j);
}

public void transformChoices(int i, int j){
    String[] sChoice0 = new String[j];
    String[] sChoice1 = new String[j];
    String[] sChoice2 = new String[j];
    String[] sChoice3 = new String[j];
    //String[] sChoice4 = new String[j+1];
    for (int a = i; a<=j; a++){

        sChoice0[a] = "";
        sChoice1[a] = "";
        sChoice2[a] = "";
        sChoice3[a] = "";
        //sChoice4[a] = "";

        if (sFinalChoice[a][0]!=null) {
            sChoice0[a] = sFinalChoice[a][0];
        }
        else {
            sChoice0[a] = "";
            }

        if (sFinalChoice[a][1]!=null) {
            sChoice1[a] = sFinalChoice[a][1];
        }
        else {
            sChoice1[a]= "";
        }

        if (sFinalChoice[a][2]!=null) {
            sChoice2[a] = sFinalChoice[a][2];
        }
        else {
            sChoice2[a] = "";

        }
        if (sFinalChoice[a][3]!=null) {
            sChoice3[a] = sFinalChoice[a][3];
        }
        else {
            sChoice3[a] = "";
        }
        //sChoice4[a] = sFinalChoice[a][4];
    }
}
}

由于某些原因,看来sFinalChoice为空。由于我的改组方法不好,是否有无人看管的变量可能为空?

如果您需要更多信息,请告诉我,我会添加它。

1 个答案:

答案 0 :(得分:1)

您两次声明 sFinalChoice ,一次在类中声明为@ATTRIBUTE,一次在 shuffleQuestionsChoicesAnswers()中声明为public String[][] sFinalChoice

我假设在 shuffleQuestionsChoicesAnswers()中,您只想对其进行初始化:

String[][] sFinalChoice = new String[j+1][4]