title: Building a linked list as the basis of a graph Null pointer errors
body: <p>I am trying to build a graph based on a linked list, where I build the linked list of nodes, and each node points to a linked list of edges. I build the graph based on an input file.</p>
  
  <p>My input file will be on the following scheme:</p>
  
  <blockquote>
    <p>Number of Nodes in graph</p>
    
    <p>SourceNode1 EndNode1</p>
    
    <p>SourceNode2 EndNode2</p>
    
    <p>....</p>
  </blockquote>
  
  <p>For example:</p>
  
  <pre><code>4 //Number of nodes
  
  1 2 //An edge between 1 and 2
  
  1 3 //An edge between 1 and 3
  
  2 4 //An edge between 2 and 4
  </code></pre>
  
  <p>An assumption is that the nodes in the graph will numbered 1 through the number of nodes and that no node will have more than 1 "parent" (though a node might have more than 1 "child").</p>
  
  <p>My problem is trying to build the linked list containing the nodes. Each node has 3 fields: the edges coming from that node, the node value (1, 2, 3, etc.), and the next node (because is a linked list of nodes). I attempt to parse in the number of nodes, create a first node manually, and attach the rest of the nodes in an in an iterative fashion. </p>
  
  <p>Note: The parent field is for some external analysis unrelated to this question. You can ignore it.</p>
  
  <p>Node class:</p>
  
  <pre><code>public class Node {
    private Edge firstEdge;
    private Node parent;
    private Node nextNode;
    private int element;
  
    //Constructor
    public Node() {
      parent = null;
      firstEdge = null;
      nextNode = null;
    }
  
    //Accsessor and Modifier Methods
    public void setElement(int e) {element = e;}
    public Node getNextNode() {return nextNode;}  
    public Edge getFirstEdge() {return firstEdge;}
    public void setFirstEdge(Edge a) {firstEdge = a;}
    public void setNextNode(Node a) {nextNode = a;}
    public int getElement() {return element;}
    public Node getParent() {return parent;}
    public void setParent(Node p) {parent = p;}
  
    //Checks for a non-null parent
    public boolean hasParent() { return parent == null; }
  
    //checks iff node has next edge
    public boolean hasFirstEdge() { return firstEdge == null; }
  
    //checks if a node has a next node
    public boolean hasNextNode() { return nextNode == null; }
  
  }
  </code></pre>
  
  <p>Edge class:</p>
  
  <pre><code>public class Edge {
   //Instance Variables
   private Node nextNode;
   private Edge nextEdge;
  
   //Constructor
   public Edge() {
    nextNode = null;
    nextEdge = null;
   }
  
   //Accsessor and Modifier Methods
   public void setNextNode(Node a) {nextNode = a;}
   public void setNextEdge(Edge a) {nextEdge = a;}
   public Node getNextNode() {return nextNode;}
   public Edge getNextEdge() {return nextEdge;}
  
   public boolean hasNextEdge() {
    return nextEdge == null;
   }
  }
  </code></pre>
  
  <p>Driver class:</p>
  
  <pre><code>import java.util.Scanner;
  import java.io.*;
  
  public class Driver {
    public static void main(String[] args)throws FileNotFoundException{
      //Get text file for building the graph
      Scanner console = new Scanner(System.in);
  
      System.out.print("Please enter the text file name: ");
      String fileName = console.nextLine();
      Scanner in = new Scanner(new File(fileName));
      //in contains the file reading scanner
  
  
      int numNodes = in.nextInt(); //first line of the text file
  
      Node first = new Node(); //first is head of the list
      first.setElement(1);
  
      int i = 2; //counter
  
      //Build the nodes list; I get problems in this loop
      while (i &lt;= numNodes) {
        Node head = new Node(); //Tracker node
        head = first; //head is the first node of the list
  
        /*Loop to end of the list*/          
        while(head.hasNextNode()) {
  
          //Null check; without it, I get NullPointerExceptions.
          //If it is not needed, or there is a better way, please inform me.
          if (head.getNextNode() == null) {
            break;
          }
  
          head = head.getNextNode(); //get to the end of the ilst    
        }
  
  
        //Next node to add
        Node newNode = new Node();
        newNode.setElement(i); //Because of the 1, 2, 3 nature of the graph 
  
        head.setNextNode(newNode); //Set the last element as the next node
  
        i++;
      }
  
      //Manually check if graph is made (check if the nodes are linked correctly)
      System.out.println("First elem (expect 1): " + first.getElement());
      System.out.println("Second elem (expect 2): " + first.getNextNode().getElement()); //It prints 4 here for some reason
  
      System.out.println("Third elem (expect 3): " + first.getNextNode().getNextNode().getElement()); //Getting a NullPointerException
  
      System.out.println("Fourth elem (expect 4): " + first.getNextNode().getNextNode().getNextNode().getElement());
  
      System.out.println("Expecting null: " + first.getNextNode().getNextNode().getNextNode().getNextNode().getElement());
  }
  </code></pre>
  
  <p>When I'm checking if the graph is built, I get problems. I am manually checking it (for this small graph, its possible), and simply print out the first node and the value of the subsequent nodes. I am expecting 1, 2, 3, 4, and null (for the element past 4, because it does not exist). The first node is fine, it prints 1. Calling first.getNextNode().getElement() prints 4, for some odd reason. And calling the node after that gives a NullPointerException. Could someone help me solve this problem?</p>
  
  <p>Note: I haven't added the edges yet. I am just trying to get the core of the linked list of nodes built.</p>
  
  <p>This is my first post on stack overflow. I apologize if it is vague, ambigous, overly detailed, lacking in information, or is a duplicate question. I could not find the answer anywhere else. All input is welcome and appreciated.</p>