【问题标题】:Attempt to read from a null array [duplicate]尝试从空数组中读取 [重复]
【发布时间】:2019-02-21 12:41:01
【问题描述】:

这个问题可能是重复的,但我没有找到一个虚拟问题的虚拟解释。 我现在放弃了,我试图让它发挥作用,但我似乎能够做到。 你是怎么处理的

问题是我得到了一个

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

关于我的transformChoices 方法

我知道一些 C,但 Java 完全不同,我没有掌握它的窍门。在我努力工作以致没有空值之后,我仍然感到惊讶。 代码现在很乱,我试图绕过空指针异常。

注意事项:如果您有更好的随机播放字符串的方法,请告诉我。

此外,如果您有一种经过测试的工作方式可以将 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 似乎为空。由于我的洗牌方法不好,是否有任何无人看管的变量可能为空?

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

【问题讨论】:

    标签: java android arrays string nullpointerexception


    【解决方案1】:

    你声明了 sFinalChoice 两次,一次是在课堂上的 public String[][] sFinalChoice,一次在 shuffleQuestionsChoicesAnswers() 中是 String[][] sFinalChoice = new String[j+1][4]

    我假设在 shuffleQuestionsChoicesAnswers() 你只想初始化它:

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

    【讨论】:

    • 好的,非常感谢。在内心深处,我知道这个问题很愚蠢,我把它复杂化了。现在我看这个问题真的很愚蠢。感谢您的快速回复。作为旁注,我在transformChoices 上也犯了同样的错误。现在我明白为什么变量是白色而不是紫色了。一个我按照你的建议做了它就像一个魅力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多