I am working on leetcode problem number 876 which says:

> 
> Given a non-empty, singly linked list with head node head, return a middle node of linked list.
>  If there are two middle nodes, return the second middle node.
> 
> 
> 

This is what I have written so far but it's throwing a null pointer exception at the while loop. I thought since the while loop is checking if node.next.next is null each time before doing anything, it wouldn't throw an exception. What am I doing wrong?

```
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next.next != null) { //null pointer exception on this line
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

```