This is my Java program , where i am setting the Objects with in the for loop as shown
ArrayList list = new ArrayList();
for(int j = 0;j<=4;j++)
{
Student student = new Student();
studunt.name="Ravi";
list.add(student);
}
Then i need to parse this List and set it inside a StudentResponse ( Which is consisting of a Student[])
StudentResponse response = new StudentResponse();
for (int in = 0; in < list.size(); in++) {
{
Student data = (TopListsQuoteData) list.get(in);
response. student[in] = data;
}
This is my StudentResponse class
public class StudentResponse
{
public Student[] student;
}
I am getting a NullPointerException at this line
response. student[in] = data;
Please help , Thanks .
You need to initialize the array before you can use it. Something like this:
Also i would suggest to use the iterator of the list instead of accessing the items by index. This works, but I feel it’s not so clean and definitely not as efficient.