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

Recommended Answers

All 9 Replies

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

this might help :

public void addAfter(DNode v, DNode z) 
	{
		DNode w = getNext(v);	// may throw an IllegalArgumentException
		z.setPrev(v);
		z.setNext(w);
//		w.setPrev(z); //comment this here
	  	v.setNext(z);
	  	size++;
	}

my guess as to why that corrects the error is most likely due to code here in your insert(DNode v) method:

DNode w = v.getNext();
			v.setElement(vehic);
			v.setNext(null);
			v.setPrev(w);
			list.addAfter(v, v);
			count++;

Thanks for the speedy reply. We must not make any changes to anything but OrderedList and TestOrderedList, unfortunately. So commenting that out would not be acceptable and would lead to a deduction on my points. Here is my updated error code:


Running test driver...
Enter name of file with test date:
list
Expecting 8 vehicles to process.
-----
Reading data from file and inserting...
Exception in thread "main" java.lang.NullPointerException
at DList.addAfter(DList.java:79)
at OrderedList.insert(OrderedList.java:41)
at TestOrderedList.main(TestOrderedList.java:28)
New vehicle to process: BMW M
Verify it was added to list.
DList@19b8e059
-- end of display --

New vehicle to process: Mercedes CLS


Here is updated 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
{	int pos;
	private int vin;
	private int count;
	private DList dlist;
	private DNode dnode;
	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 = new DList();
		Vehicle vehic = v.getElement();
		dnode = v;
		if(count == 0)
		{
			v.setElement(vehic);
			v.setNext(null);
			v.setPrev(null);
			dlist.addFirst(v);
			count++;
			return;
		}
		else 
		{	
			DNode z = v;
			DNode w = v.getNext();	// Not same ID  mcauleyr's "ID" variable corresponding to VIN of vehicle
			z.setElement(vehic);
			z.setNext(null);
			z.setPrev(w);
			dlist.addAfter(v, z);
			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 v = new Vehicle(vin, d);
			DNode t = new DNode (v, null, null);
			System.out.println("New vehicle to process: " + d);
	                ol.insert(t);
			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

Also, I can't get my display() method to work correctly and it needs a re-code. What information would help get me going on that?

try this in the dlist class:

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++;
	}

Check out my post above ;)

Can't modify DList or DNode classes. I'll try it just to see what happens though.


This may help, let w be the node following v

Here is the result in the console. How do I get them to display strings and not memory locations? Do I need a toString() method? I tried to make some in various places but to no avail.


Running test driver...
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
Verify it was added to list.
DNode@6791d8c1
-- end of display --

New vehicle to process: Mercedes CLS
Verify it was added to list.
DNode@182d9c06DNode@182d9c06
-- end of display --

New vehicle to process: Ferarri Italia
Verify it was added to list.
DNode@5a5e5a50DNode@5a5e5a50DNode@5a5e5a50
-- end of display --

New vehicle to process: Porsche Cayman RS
Verify it was added to list.
DNode@687b6889DNode@687b6889DNode@687b6889DNode@687b6889
-- end of display --

New vehicle to process: Lamborghini Gallardo TT
Verify it was added to list.
DNode@7e0c2ff5DNode@7e0c2ff5DNode@7e0c2ff5DNode@7e0c2ff5DNode@7e0c2ff5
-- end of display --

New vehicle to process: Mazda Miata
Verify it was added to list.
DNode@5220be79DNode@5220be79DNode@5220be79DNode@5220be79DNode@5220be79DNode@5220be79
-- end of display --

New vehicle to process: Chevrolet Camaro SS
Verify it was added to list.
DNode@63b9240eDNode@63b9240eDNode@63b9240eDNode@63b9240eDNode@63b9240eDNode@63b9240eDNode@63b9240e
-- end of display --

New vehicle to process: Ford Mustang GT
Verify it was added to list.
DNode@fee4648DNode@fee4648DNode@fee4648DNode@fee4648DNode@fee4648DNode@fee4648DNode@fee4648DNode@fee4648
-- end of display --

-------

Check out my post above ;)

Can't modify DList or DNode classes. I'll try it just to see what happens though.

oops sorry read like an idiot :)

well you could try this:

public void display()
	{
		if(count==1) {
                      System.out.println(dlist.getFirst().element);//this count ==1 checks if its the first time if we dont do this null will be shown for the first input from file which is the BMW
                 } else {
		System.out.println(dlist.getNext(dnode).element);
                 }
	}

Check out what I did as you must have been posting that above.

public void display()
	{
		System.out.println(dlist.toString());
	}

And in DList, I added the toString() method:

public String toString()
	{
		String s = "";
		DNode v = header.getNext();
		while (v != trailer)
		{
			s += v.getElement();
			v = v.getNext();
			if (v != trailer)
				s += ",";
		}
		s += "";
		return s;
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.