0

I'm parsing an XML file and assigning values of XML to the class variable. Here is the main activity and variable class also:

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

    }
  }      
}

Here is the variable class:

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;
}
3
  • currentquestion is null doesn't it? Commented May 29, 2014 at 13:22
  • yes currentquestion is null Commented May 29, 2014 at 13:27
  • @user3384222 So initialize it. Check my answer. Commented May 29, 2014 at 13:28

2 Answers 2

3

Initialize your QuestionArrayList class. You have just declared variable for it.

currentquestion = new QuestionArrayList():
Sign up to request clarification or add additional context in comments.

Comments

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

      questions= new ArrayList<QuestionArrayList>();
currentquestion = new QuestionArrayList():

//initialize cuurrentqeustion here.

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

    }
  }      

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.