0

iam trying to fill a list from type attendance with a list of students but i keep getting null pointer exception even though the students list is not empty but the attendance is empty ad the attendance has a property of student as following

  • At each `i` iteration, the `j` loop is same so you'll change each time the student value, and at the end all will have as student the last one – azro Jul 04 '18 at 08:38
  • At which line is the `NullPointerException` occurring? – Kevin Anderson Jul 04 '18 at 08:42
  • at this line finalList.get(j).setstudent(temp); and when i debugged it temp is not null – kernelPain Jul 04 '18 at 08:44
  • i removed j and still i got the same issue – kernelPain Jul 04 '18 at 08:46
  • did you initialize the finalList? does it have j items so j-th item is not null? – Coreggon Jul 04 '18 at 08:55
  • also in second loop you iterate over the size of the listOfStudents list, I suppose you ment j < finalList.size() as mentioned in an answer below? – Coreggon Jul 04 '18 at 08:58
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 04 '18 at 09:21
  • @kernelPain can you post structure for `listOfStudents` and `finalList` – Pramod Jul 04 '18 at 12:21

2 Answers2

0

You have a correct loop but not in logic. Try this thank me later.

for (int i = 0; i < listOfStudents.size(); i++) {
    Learner temp = listOfStudents.get(i);
    for (int j = 0; j < finalList.size(); j++) {
        finalList.get(j).setstudent(temp);
    }
}
Alex
  • 35
  • 1
  • 10
  • final list is empty so it will cause a null pointerException – kernelPain Jul 04 '18 at 08:57
  • well then if it is empty then Item on j-th position is null and it gives you a NullPointerException – Coreggon Jul 04 '18 at 08:59
  • i tried saving the length of listofstudents in a variable and used it as an iterator and it didnt work as well i still got null pointer exception – kernelPain Jul 04 '18 at 09:07
  • @kernelPain because it's not an issue with iterating over `listOfStudents`. It's an issue with `finalList.get(j).setstudent(temp)`, when you try to retrieve element from empty list, it will throw Exception. – Pramod Jul 04 '18 at 12:16
  • i tried to set the student in an attendance object and instead of finalList.get(j) i tried finalList.add(object) still got the exception – kernelPain Jul 04 '18 at 13:34
0

Let's say you have listOfStudents of size 10 and finalList of size 5.

So when the first loop proceeds for the first time the temp is set to the first element of listOfStudents.

Now your seconds loop has to go from 0 to listOfStudents.size() which is equal to 10. So when it comes to j = 5, you want to execute finalList.get(5).setstudent(temp); but! the finalList only has 5 items, so when calling get(5) and above you get a NullPointerException because the size of the list is only 5.

Coreggon
  • 505
  • 2
  • 5
  • 17
  • when it gets to this loop a condition finalList.isEmpty should be true – kernelPain Jul 04 '18 at 09:45
  • yes, but in the code you posted you don't check whether it is empty or not... so anyway, if it is of size 0, get(5) still returns a NullPointerException... – Coreggon Jul 04 '18 at 09:52
  • iwhen i tried to add the obk=ject to a list that is not empty it worked fine but when i tried to on a new list it didnt work what am i messing here> – kernelPain Jul 04 '18 at 10:41