Hey again. Trying to add elements to a DList, this happens when I try to add the second car (500, Porsche). The whole insert algorithm sucks and am strongly considering trying to figure out a better one.
This is what shows up in the console:
Enter name of file with test date:
list
Expecting 8 vehicles to process.
-----
Reading data from file and inserting...
New vehicle to process: BMW M
Exception in thread "main" java.lang.NullPointerException
at DList.addAfter(DList.java:79)
at OrderedList.insert(OrderedList.java:43)
at TestOrderedList.main(TestOrderedList.java:36)
Here is all of my source code.
/**
*
* @author Harry Luthi
* @work Assignment 1
*
*/
public class Vehicle
{
private int vin;
private String d;
// static String d, else main will return "null"
// for vehicle's description when tested.
public Vehicle(int x, String d)
{
vin = x;
this.d = d;
}
// toString
public String toString()
{
return "VIN: " + vin + ". " + "Description: " + d + "\n";
}
// Accessors and modifiers
public int getVin()
{
return vin;
}
public String getDescription()
{
return d;
}
}
public class OrderedList
{
private int vin;
int pos;
private int count;
private DList dlist;
private DNode dnode;
private Vehicle newElem;
private DNode next;
private DNode prevID;
public int getCount()
{
return count;
}
public OrderedList()
{
count = 0;
}
public void insert(DNode v) // Insert vehicle to list
{
DList list = new DList();
dnode = v;
Vehicle vehic = v.getElement();
if(count == 0)
{
v.setElement(vehic);
v.setNext(null);
v.setPrev(null);
list.addFirst(v);
count++;
return;
}
else
{
DNode w = v.getNext();
v.setElement(vehic);
v.setNext(null);
v.setPrev(w);
list.addAfter(v, v);
count++;
return;
}
}
public void display()
{
String s = "";
for (int i=0; i<count; i++)
s = s+dlist;
System.out.println(s);
}
// boolean find(DNode v) // Find Vehicle in list
// {
//
// }
//
//
// public void remove(int vin)
// {
//
// }
//
//
// public Vehicle retrieve(int vin)
// {
//
// }
//
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class TestOrderedList
{
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Running test driver...");
System.out.println("Enter name of file with test date: ");
Scanner scan = new Scanner(System.in);
String fileName = scan.nextLine();
File inputFile = new File(fileName);
Scanner inFile = new Scanner(inputFile);
int count = 8;
System.out.println("Expecting " + count + " vehicles to process.");
OrderedList ol = new OrderedList();
System.out.println("-----");
System.out.println("Reading data from file and inserting...");
for(int i=0; i<count; i++)
{
int vin = inFile.nextInt();
String d = inFile.nextLine();
Vehicle v1 = new Vehicle(386, "BMW");
DNode t1 = new DNode (v1, null, null);
Vehicle v2 = new Vehicle(500, "Porsche");
DNode t2 = new DNode(v2, t1, null);
OrderedList list = new OrderedList();
System.out.println("New vehicle to process: " + d);
list.insert(t1);
list.insert(t2);
// for (int j=0; j<count; j++)
// {
// list.addFirst(t1);
// list.addAfter(t1, t2);
// }
System.out.println("Verify it was added to list.");
ol.display();
System.out.print("-- end of display -- \n\n");
}
System.out.println("-------");
}
}
/** Doubly linked list with nodes of type DNode storing strings.
* Code by Goodrich and Tamassia, Data Structurs and Algorithms, 5th edition
* */
public class DList
{
protected int size; // number of elements
protected DNode header, trailer; // sentinels
/** Constructor that creates an empty list */
public DList()
{
size = 0;
header = new DNode(null, null, null); // create header
trailer = new DNode(null, header, null); // create trailer
header.setNext(trailer); // make header and trailer point to each other
}
/** Returns the number of elements in the list */
public int size() { return size; }
/** Returns whether the list is empty */
public boolean isEmpty() { return (size == 0); }
/** Returns the first node of the list */
public DNode getFirst() throws IllegalStateException
{
if (isEmpty()) throw new IllegalStateException("List is empty");
return header.getNext();
}
/** Returns the last node of the list */
public DNode getLast() throws IllegalStateException
{
if (isEmpty()) throw new IllegalStateException("List is empty");
return trailer.getPrev();
}
/** Returns the node before the given node v. An error occurs if v
* is the header
*/
public DNode getPrev(DNode v) throws IllegalArgumentException
{
if (v == header) throw new IllegalArgumentException("Cannot move back past the header of the list");
return v.getPrev();
}
/** Returns the node after the given node v. An error occurs if v
* is the trailer
*/
public DNode getNext(DNode v) throws IllegalArgumentException
{
if (v == trailer) throw new IllegalArgumentException("Cannot move forward past the trailer of the list");
return v.getNext();
}
/** Inserts the given node z before the given node v. An error
* occurs if v is the header
*/
public void addBefore(DNode v, DNode z) throws IllegalArgumentException
{
DNode u = getPrev(v); // may throw an IllegalArgumentException
z.setPrev(u);
z.setNext(v);
v.setPrev(z);
u.setNext(z);
size++;
}
/** Inserts the given node z after the given node v. An error occurs
* if v is the trailer
*/
public void addAfter(DNode v, DNode z)
{
DNode w = getNext(v); // may throw an IllegalArgumentException
z.setPrev(v);
z.setNext(w);
w.setPrev(z);
v.setNext(z);
size++;
}
/** Inserts the given node at the head of the list */
public void addFirst(DNode v)
{
addAfter(header, v);
}
/** Inserts the given node at the tail of the list */
public void addLast(DNode v)
{
addBefore(trailer, v);
}
/** Removes the given node v from the list. An error occurs if v is
* the header or trailer
*/
public void remove(DNode v)
{
DNode u = getPrev(v); // may throw an IllegalArgumentException
DNode w = getNext(v); // may throw an IllegalArgumentException
// unlink the node from the list
w.setPrev(u);
u.setNext(w);
v.setPrev(null);
v.setNext(null);
size--;
}
}
public class DNode
{
protected Vehicle element;
protected DNode next, prev;
/**
* Constructor that creates node with given fields
*/
public DNode (Vehicle v, DNode p, DNode n){
element = v;
prev = p;
next = n;
}
/**
* Returns the element of this node
*/
public Vehicle getElement() { return element; }
/**
* Returns the previous nosde of this node
*/
public DNode getPrev() { return prev; }
/**
* Returns the next node
*/
public DNode getNext() { return next; }
/**
* sets the element of this node
*/
public void setElement(Vehicle newElem) { element = newElem; }
/**
* sets the previous node of this node
*/
public void setPrev (DNode newPrev) { prev = newPrev; }
/**
* sets the next node of this node
*/
public void setNext (DNode newNext) { next = newNext; }
}
This is contents of the file called "list" that I am putting in after the prompt
1000 BMW M
300 Mercedes CLS
600 Ferarri Italia
100 Porsche Cayman RS
200 Lamborghini Gallardo TT
2500 Mazda Miata
50 Chevrolet Camaro SS
150 Ford Mustang GT