gpt4 book ai didi

java - ArrayList 给出空指针异常

转载 作者:行者123 更新时间:2023-12-01 22:56:38 24 4
gpt4 key购买 nike

我正在解析 XML 文件并将 XML 值分配给类变量。这也是主要的 Activity 和变量类:

package com.example.questions;

public class Test extends Activity
{
private TextView question;
private RadioButton rdooption1;
private RadioButton rdooption2;
private RadioButton rdooption3;
private RadioButton rdooption4;
private static final String FILENAME = "xmlFileName.xml";
private FileInputStream fin;


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

@SuppressWarnings("null")
private void parseXML(XmlPullParser xpp) throws XmlPullParserException, IOException
{
List<QuestionArrayList> questions = null;
QuestionArrayList currentquestion = null;

while(xpp.next()!=XmlPullParser.END_DOCUMENT )
{

questions= new ArrayList<QuestionArrayList>();

if (xpp.getEventType() != XmlPullParser.START_TAG)
{
continue;
}

String name = xpp.getName();

if(name.equals("Question"))
{
//String questionmy = xpp.nextText();

//here error is giving
currentquestion.question_name=xpp.nextText();
}


questions.add(currentquestion);
System.out.println("Size of al after additions: " + questions.size());

System.out.println("Contents of al: " + questions);

}
}
}

这是变量类:

public class QuestionArrayList 
{
public int question_set_id;
public int question_id;
public String question_type;
public String question_name;
public String option_one;
public String option_two;
public String option_three;
public String option_four;
}

最佳答案

初始化您的QuestionArrayList类。您刚刚为其声明了变量。

currentquestion = new QuestionArrayList():

关于java - ArrayList 给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23934363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com