【问题标题】:ArrayList giving null pointer exceptionArrayList 给出空指针异常
【发布时间】:2014-07-19 00:27:39
【问题描述】:

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

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

【问题讨论】:

  • currentquestion 是 null 不是吗?
  • 是的,当前问题为空
  • @user3384222 所以初始化它。检查我的答案。

标签: java android nullpointerexception


【解决方案1】:

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

currentquestion = new QuestionArrayList():

【讨论】:

    【解决方案2】:
    while(xpp.next()!=XmlPullParser.END_DOCUMENT )
        {
    
          questions= new ArrayList<QuestionArrayList>();
    currentquestion = new 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);
          }
    
    //as per your condition it will add blank currentquestion in question array list..
    //its bettter put it question.add(currentquestion) inside if condition.
    
    
          //questions.add(currentquestion);
          System.out.println("Size of al after additions: " + questions.size());
    
          System.out.println("Contents of al: " + questions);
    
        }
      }      
    

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 2023-03-05
      • 1970-01-01
      • 2014-09-06
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多