i am creating a class call student and want to store those on the list i am using iterator to fetch the elements from list but i cant do so because an exception is happening and i cant resolve the exception which is happening here,it would be great help if someone can give me the logical reason for this.Thank you
import java.awt.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import java.util.List.*;
public class Runs {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// adding student class to list
java.util.List list = new ArrayList<Student>();
Student q = new Student("hello", 1);
Iterator<Student> l = list.iterator();
list.add(q);
// error false in this segment of the code
Student op = l.next();
String hhh = op.getnamez();
System.out.println(hhh);
System.out.println(op.getnamez());
} catch (Exception e) {
System.out.println("" + e);
}
}
public static class Student {
// student class
public String name;
private int age;
public Student(String s, int a) {
this.name = s;
this.age = a;
}
public String getnamez() {
return this.name;
}
}
}