title: Accessing methods from a ArrayList of custom objects
body: <p>I have a problem and I think is a concept error</p>
  
  <p>I created a java class that when the constructor is called a another custom object is created</p>
  
  <p>The fist time runs the code with param start == true and</p>
  
  <pre><code>questions  = new ArrayList&lt;Question&gt;();
              questions.add(this.question);
  </code></pre>
  
  <p>Then calls to writeXML and works fine</p>
  
  <p>But if start != true then first reads the content of xml and store in arraylist of custom questions objects</p>
  
  <pre><code>questions = readXML(ctx);
  questions.add(this.question);
  readXML return a arraylist of questions objects from readed xml
  </code></pre>
  
  <p>The problem is when calls to writexml method sending arraylist as parameter when the code arrives to </p>
  
  <pre><code> for( Question q : questions ){
  
  
                  serializer.startTag(null, "question");
                  serializer.attribute(null,"grupo" , q.getGroup());
                  serializer.attribute(null,"question" , q.getQuestion());
                  serializer.text(q.getAnswer());
                  serializer.endTag(null, "question");
              }
  </code></pre>
  
  <p>when the codes try to execute serializer.attribute(null,"grupo" , q.getGroup());    q.getGroup() fails and goes to catch exception with null message</p>
  
  <p>What can I be doing wrong?</p>
  
  <p>Thanks a lot</p>
  
  <p>This is the complete class</p>
  
  <pre><code>package...
  inports...
  
  class AsyncLoadXML extends AsyncTask&lt;Boolean, Integer, String&gt; {
  
      private Context ctx;    
      private List&lt;Question&gt; questions;
      private Question question;
      String TAG;
  
      public AsyncLoadXML(Context c, String g, String p, String r) {
          this.ctx = c;
          question =new Question();
          question.setGroup(g);
          question.setQuestion(p);
          question.setAnswer(r);
  
      }
  
      @Override
      protected String doInBackground(Boolean... start) {
  
          if(!start[0]){
              try { questions = readXML(ctx);
              } 
              catch (IOException e) { e.printStackTrace(); }
          }
  
          else {questions  = new ArrayList&lt;Question&gt;();
          questions.add(this.question);
          }
  
          try { writeXML(ctx, questions); } 
          catch (IOException e) { e.printStackTrace(); }
  
  
          return "done";
      }
      protected void onPostExecute(String result) {
          if(!result.equals("")) 
              Toast.makeText(this.ctx, result, Toast.LENGTH_LONG).show();
      }
  
  
  
      public String writeXML(Context ctx, List&lt;Question&gt; questions) throws IOException{   
          FileOutputStream fout = null;
  
          try {
              fout = ctx.openFileOutput("answers.xml", Context.MODE_PRIVATE);
          } catch (FileNotFoundException e) {
              return e.getMessage();
  
          }
  
          XmlSerializer serializer = Xml.newSerializer();
          try {
              serializer.setOutput(fout, "UTF-8");
              serializer.startDocument(null, true);
              serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
  
              serializer.startTag(null, "questions");
  
  
              for( Question q : questions ){
  
  
                  serializer.startTag(null, "question");
                  serializer.attribute(null,"grupo" , q.getGroup());
                  serializer.attribute(null,"question" , q.getQuestion());
                  serializer.text(q.getAnswer());
                  serializer.endTag(null, "question");
              }
  
              serializer.endTag(null, "questions");
  
  
              serializer.endDocument();
              serializer.flush();
              fout.close();
              return "done";
  
          } catch (Exception e) {
              return e.getMessage();
  
          }
      }
  
  
  
  
  
      public List&lt;Question&gt; readXML(Context ctx) throws IOException{  
          FileInputStream fin = null;
          questions= new ArrayList&lt;Question&gt;();
          try {
              fin = ctx.openFileInput("answers.xml");
          } catch (Exception e) {
  
          }
  
          XmlPullParser parser = Xml.newPullParser();
          try {
              parser.setInput(fin, "UTF-8");
  
              int event = parser.next();
              while(event != XmlPullParser.END_DOCUMENT) {
  
                  if(event == XmlPullParser.START_TAG) {
  
  
                      question = new Question();
  
                      for(int i = 0; i &lt; parser.getAttributeCount(); i++) {
  
                          if(parser.getAttributeName(i) == "group") question.setGroup(parser.getAttributeValue(i));
                          else if(parser.getAttributeName(i) == "question") question.setQuestion(parser.getAttributeValue(i));
  
                      }
  
                  }
  
                  if(event == XmlPullParser.TEXT &amp;&amp; parser.getText().trim().length() != 0)
                  {question.setAnswer(parser.getText());
  
                  questions.add(question);
                  }
  
  
                  if((event == XmlPullParser.END_TAG))
                  {
                      Log.d(TAG, "&lt;/"+ parser.getName() + "&gt;");}
  
  
                  event = parser.next();
              }
              fin.close();
  
  
          } catch (Exception e) {
  
              e.printStackTrace();
          }
          return questions;
      }
  
  }
  </code></pre>
  
  <p>this is a object class</p>
  
  <pre><code>public class Question {
  
      private String group;
      private String question;
      private String answer;
  
  
      public Question(){
  
      }
      public Question(String g, String p, String r){
      group = g;
      question = p;
      answer = r;
      }
  
      public String getGroup(){
          return group;
      }
  
      public String getQuestion(){
          return question;
      }
  
      public String getAnswer(){
          return answer;
      }
  
      public void setGroup(String g){
          group = g;
      }
  
      public void setQuestion(String p){
          question = p;
      }
  
      public void setAnswer(String r){
          answer = r;
      }
  }
  </code></pre>