-2
My code is as follows:

//Node class (inner class)
    private class Node
    {
        private String command;
        private String fileName;
        private int fileSize;
        private Node next;
        private Node prev;

        //constructor of Node
        private Node(String command, String fileName, int fileSize, Node prev, Node next)
        {
            this.command = command;
            this.fileName = fileName;
            this.fileSize = fileSize;
            this.prev = prev;
            this.next = next;
        }
    }

    private Node head;
    private Node tail;
    int size;

    //constructor of list
    public ReadInput()
    {
        diskSize = 0;
        head = null;
        tail = null;
        size = 0;
    }

    public void insert(String command, String fileName, int fileSize)
    {

          if (head == null)
            { 
                head = tail = new Node(command, fileName, fileSize, null, null );
                size ++;
            }

          else 
            {
                for(Node temp = head; temp != null; temp = temp.next)
                {

                        temp.next = new Node(command, fileName, fileSize, temp, temp.next.next);
                        temp.next.next.prev = temp.next;
                        size++;
                        if ( fileName == temp.fileName)
                             System.out.println("ID already exists!");
                        break;

                }
            }       
    }

I'm just trying to insert into my doubly linked list. I have another method that calls on insert with the proper arguments to add to the linked list that i have not posted here since it's unnecessay. The first insertion into the head is fine but on the second insertion, while debugging my program, i find that i get a null pointer exception on the line temp.next = new Node(command, fileName, fileSize, temp, temp.next.next); I can't see where i'm going wrong can anyone help? thanks

Chalupa
  • 367
  • 1
  • 5
  • 20
  • Have you looked at http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – John3136 Jun 21 '15 at 23:34
  • I know what a null pointer exception is... but that doesn't mean i can always resolve it! – Chalupa Jun 21 '15 at 23:36
  • Did you look at the "how to resolve" part? Your for loop says "temp is not null, so temp = temp.next (which could set temp to null). – John3136 Jun 21 '15 at 23:40
  • the first time it goes into the else part of the loop this should not happen since temp = head. but i get the error the first time my program executes the else part. – Chalupa Jun 21 '15 at 23:44

1 Answers1

0

For the first element you insert, starts with an empty list so it goes through the if block

      head = tail = new Node(command, fileName, fileSize, null, null );

so head.next = null

when you insert the second element, code jumps to the else block

       temp.next = new Node(command, fileName, fileSize, temp, temp.next.next);

in case of the second item,

temp = head

temp.next =null

temp.next.next => Null reference exception (last parameter passed to the constructor)

Also, looking at your code, it seems like instead of passing temp.next.next to the constructor you want to pass temp.next. change that statement to be

     temp.next = new Node(command, fileName, fileSize, temp, temp.next);
KnightFox
  • 3,132
  • 4
  • 20
  • 35
  • but why can't i pass a null value to the constructor? – Chalupa Jun 21 '15 at 23:50
  • you can pass a null value. temp.next is null. but when you try to access temp.next.next you are effectively trying to access the next property of null, hence the NRE. – KnightFox Jun 21 '15 at 23:58