Java 尝试从空数组中读取
这个问题可能是重复的,但我没有找到一个虚拟问题的虚拟解释。 我现在放弃了,我试着让它工作,但我似乎能做到。 你怎么处理 问题是我得到了一份工作 java.lang.NullPointerException:尝试从空数组读取 关于我的Java 尝试从空数组中读取,java,android,arrays,string,nullpointerexception,Java,Android,Arrays,String,Nullpointerexception,这个问题可能是重复的,但我没有找到一个虚拟问题的虚拟解释。 我现在放弃了,我试着让它工作,但我似乎能做到。 你怎么处理 问题是我得到了一份工作 java.lang.NullPointerException:尝试从空数组读取 关于我的transformChoices方法 我知道一些C语言,但是Java是完全不同的东西,我没有掌握它的窍门。我仍然感到惊讶的是,在我如此努力地工作而没有一个空值之后,在某个地方有一个空值。 由于我试图绕过空指针异常,代码现在已经一团糟 需要注意的一些事情:如果你有更好的
transformChoices
方法
我知道一些C语言,但是Java是完全不同的东西,我没有掌握它的窍门。我仍然感到惊讶的是,在我如此努力地工作而没有一个空值之后,在某个地方有一个空值。
由于我试图绕过空指针异常,代码现在已经一团糟
需要注意的一些事情:如果你有更好的主意来洗牌,请告诉我
此外,如果您有一种经过测试的工作方式,可以将2d阵列传递给其他活动,请也告诉我。目前,我将二维数组转换为一维数组。这可能不是最好的,但我不必为包裹和其他东西而挣扎。我用意图和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];
}
}
}
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入java.lang.Math;
公共类级别的活动扩展了活动{
private QuestionLibrary mQuestionLibrary=新建QuestionLibrary();
int-iSize=mQuestionLibrary.mQuestion.length;
私有字符串[]sNewQuestion=新字符串[iSize];
私有字符串[][]sNewChoice=新字符串[iSize][4];
私有字符串[]sNewAnswer=新字符串[iSize];
公共字符串[]问题;
公共字符串[][]sFinalChoice;
公共字符串[]sChoice0;
公共字符串[]sChoice1;
公共字符串[]sChoice2;
公共字符串[]sChoice3;
//公共字符串[]sChoice4;
公共字符串[]sFinalAnswer;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_levels);
for(int i=0;ipublic String[]]sFinalChoice
,一次在shufflequestChoicesAnswers()中声明为String[]]sFinalChoice=new String[j+1][4]
我假设在ShuffleQuestionChoicesAnswers()中,您只想初始化它:
sFinalChoice = new String[j+1][4];
是的,非常感谢。在内心深处,我知道这个问题很愚蠢,我把它复杂化了。现在我看到这个问题,它真的很愚蠢。谢谢你的快速回答。顺便说一句,我在
transformChoices
上也犯了同样的错误。现在我明白了为什么变量是白色而不是紫色的。一个是我按照你的建议做的她看起来很迷人。