You have not initialized the 'next' node in your constructor, you can set the 'next' to null. This way, when you create a new 'ListNode', the 'next' field is set to null by default -
class ListNode {
int data;
ListNode next;
public ListNode(int data) {
this.data = data;
this.next = null;
}
}