-1

I am new to this one I need to parse json for Question and Answer Bank and set data in Custom ArrayList I unable to set the data using getter and setter.

My First Class

//Custom ArrayList
ArrayList<DataBO> dt=new  ArrayList<DataBO>();

//Class With Getter Setter

    public class DataBO
    {

        **public Questions[] Questions;**


        public String PointsCanBeEarnedMsg;

        public String ContentUrl;

        public String getPointsCanBeEarnedMsg ()
        {
            return PointsCanBeEarnedMsg;
        }

        public void setPointsCanBeEarnedMsg (String PointsCanBeEarnedMsg)
        {
            this.PointsCanBeEarnedMsg = PointsCanBeEarnedMsg;
        }

        public String getContentUrl ()
        {
            return ContentUrl;
        }

        public void setContentUrl (String ContentUrl)
        {
            this.ContentUrl = ContentUrl;
        }

        public Questions[] getQuestions ()
        {
            return Questions;
        }

        public void setQuestions (Questions[] Questions)
        {
            this.Questions = Questions;
        }


    }

2nd Class for Question Array

    public class Questions
    {
        public String CorrectOptionCount;

        public String QuestionId;

        public String QuestionString;

    public String getCorrectOptionCount ()
    {
        return CorrectOptionCount;
    }

    public void setCorrectOptionCount (String CorrectOptionCount)
    {
        this.CorrectOptionCount = CorrectOptionCount;
    }

    public String getQuestionId ()
    {
        return QuestionId;
    }

    public void setQuestionId (String QuestionId)
    {
        this.QuestionId = QuestionId;
    }

    public String getQuestionString ()
    {
        return QuestionString;
    }

    public void setQuestionString (String QuestionString)
    {
        this.QuestionString = QuestionString;
    }

    public Options[] getOptions ()
    {
        return Options;
    }


    }

These

Questions [] qs=new  Questions[len];
qs[i].setQuestionId(data);

are not working, getting NullPointer Exception??? How to add data in Questions after that in ArrayList??

ranjan
  • 136
  • 1
  • 12

2 Answers2

3

With this:

Questions [] qs=new  Questions[len];
qs[i].setQuestionId(data);

You just allocated space for array with default values i.e. null. You need to initialize every element like

//loop to initialize Questions which is held by array
qs[i] = new Questions();
...
qa[i].set..
SMA
  • 36,381
  • 8
  • 49
  • 73
1
Questions [] qs=new  Questions[len];
qs[i].setQuestionId(data);

With this code you're only creating an array of size len, but you aren't allocating each element. At this point the array looks like this:

{null, null, ..., null}

You need to fill the array with instances of Questions in a loop. An example code to do this would be

Questions [] qs=new  Questions[len];
for(int i = 0; i < len; i++)
    qs[i] = new Questions();

So that each element of the array is assigned a Questions instance. After you do this you are able to access each element as you did before.

Iamsomeone
  • 233
  • 2
  • 15