0

This question might be a duplicate, but I didn't find a dummy explanation for a dummy question. I give up now, I tried to make it work but I can seem to be able to. How do you handle

The problem is that I get a

java.lang.NullPointerException: Attempt to read from null array

on my transformChoices method

I know some C, but Java is something different entirely and I didn't get the hang of it. I am still amazed that there is a null value somewhere after I worked so hard to not have one. The code is a mess by now, with my attempts to bypass the null pointer exception.

Some things to note: If you have a better idea to shuffle strings let me know.

Also if you have a tested working way to pass 2d arrays with intent to another activities hit me with that too. At the moment I convert a 2d array to one dimension arrays. It might not be the best but I don't have to strugle with bundles and other stuff. I searched for a way with intents and 2d arrays and couldn't find anything that works for me.

Please bear in mind that this is just a prototype, the arrays will have around 1000 values. If you know a better and faster way to work with them, please share.

Here's the problem:

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];
    }
}
}

It seems that sFinalChoice is null for some reason. Are there any variables left unattended that might be null because my method of shuffling is not good?

If you need anymore information please let me know and I will add it.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

You declare sFinalChoice twice, once as public String[][] sFinalChoice in the class and once in shuffleQuestionsChoicesAnswers() as String[][] sFinalChoice = new String[j+1][4]

I assume in shuffleQuestionsChoicesAnswers() you want to only initialize it:

sFinalChoice = new String[j+1][4];
cuzi
  • 978
  • 10
  • 21
  • Yes, thank you very much. Deep down I knew the issue was something stupid I over complicated it with. Now that I look at the question it was really dumb. Thanks for the quick reply. As a side note I do the same mistake on `transformChoices` too. Now I see why variables were in white instead of purple. One I did it how you suggested it worked like a charm. – MaXaRaYkIn IceStein Sep 17 '18 at 15:29