テキスト ファイルをオブジェクトの線形リンク リストに読み込もうとしています。テキスト ファイル (payfile.txt) には、次の情報が含まれています。
DEBBIE STARR F 3 W 1000.00
JOAN JACOBUS F 9 W 925.00
DAVID RENN M 3 H 4.75
ALBERT CAHANA M 3 H 18.75
DOUGLAS SHEER M 5 W 250.00
SHARI BUCHMAN F 9 W 325.00
SARA JONES F 1 H 7.50
RICKY MOFSEN M 6 H 12.50
JEAN BRENNAN F 6 H 5.40
JAMIE MICHAELS F 8 W 150.00
名を独自のリストに挿入し、姓をリストに挿入したいと考えています。問題の残りの部分で、LLL への変更の追加と削除を行う必要があります。これが、ファイルをリストに読み込むための正しいコーディングかどうかはわかりません。
nullpointerexception @ firstname.addFirst(lineScanner.next()); が発生しています。
これまでのところ、私はこれを持っています:
public class Payroll
{
private LineWriter lw;
private ObjectList output, input;
private ObjectList firstname, lastname, gender, tenure, rate, salary;
public Payroll(LineWriter lw)
{
this.lw = lw;
}
public void readfile()
{
File file = new File("payfile.txt");
try{
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine())
{
String line = scanner.nextLine();
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter(" ");
while(lineScanner.hasNext())
{
firstname.addFirst(lineScanner.next());
lastname.addFirst(lineScanner.next());
gender.addFirst(lineScanner.next());
tenure.addFirst(lineScanner.next());
rate.addFirst(lineScanner.next());
salary.addFirst(lineScanner.next());
}
}
}catch(FileNotFoundException e)
{e.printStackTrace();}
}
// ObjectList.java
public class ObjectList {
private ListNode list;
/**
* Constructs an empty list
*/
public ObjectList() {
list = null;
}
/**
* Returns the first node in the list
*/
public ListNode getFirstNode() {
return list;
}
/**
* Returns the first element in the list
*/
public Object getFirst() {
if (list == null)
return null;
return list.getInfo();
}
/**
* Returns the last element in the list
*/
public Object getLast() {
if (list == null)
return null;
ListNode p = list;
while (p.getNext() != null)
p = p.getNext();
return p.getInfo();
}
/**
* Adds the given element to the beginning of the list
* @param o - the element to be inserted at the beginning of the list
*/
public void addFirst(Object o) {
ListNode p = new ListNode(o, list);
list = p;
}
/** Appends the given element to the end of the list
* @param o - the element to be appended to the end of the list
*/
public void addLast(Object o) {
ListNode p = new ListNode(o);
if (list == null)
list = p;
else {
ListNode q = list;
while (q.getNext() != null)
q = q.getNext();
q.setNext(p);
}
}
/**
* Removes and returns the first element from the list
*/
public Object removeFirst() {
if (list == null) {
System.out.println("removeFirst Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode p = list;
list = p.getNext();
return p.getInfo();
}
/**
* Removes and returns the last element from the list
*/
public Object removeLast() {
if (list == null) {
System.out.println("removeLast Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode p = list;
ListNode q = null;
while (p.getNext() != null) {
q = p;
p = p.getNext();
}
if (q == null)
list = null;
else
q.setNext(null);
return p.getInfo();
}
/**
* Inserts a node after the node referenced by p
* @param p - reference to node after which the new node will be added
* @param o - reference to node that will be inserted into the list
*/
public void insertAfter(ListNode p, Object o) {
if (p == null) {
System.out.println("insertAfter Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode q = new ListNode(o, p.getNext());
p.setNext(q);
}
/**
* Deletes the node after the node referenced by p
* @param p - reference to node after which the node will be deleted
*/
public Object deleteAfter(ListNode p) {
if (p == null || p.getNext() == null) {
System.out.println("deleteAfter Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode q = p.getNext();
p.setNext(q.getNext());
return q.getInfo();
}
/**
* Inserts a node into its correct location within an ordered list
* @param o - The element to be inserted into the list
*/
public void insert(Object o) {
ListNode p = list;
ListNode q = null;
while (p != null && ((Comparable)o).compareTo(p.getInfo()) > 0) {
q = p;
p = p.getNext();
}
if (q == null)
addFirst(o);
else
insertAfter(q, o);
}
/**
* Removes and returns the first occurrence of the specified
* element in the list
* @param o - The object to be removed from the list
*/
public Object remove(Object o) {
ListNode p = list;
ListNode q = null;
while (p != null && ((Comparable)o).compareTo(p.getInfo()) != 0) {
q = p;
p = p.getNext();
}
if (p == null)
return null;
else return q == null ? removeFirst() : deleteAfter(q);
}
/**
* Returns true if the list contains the specified element.
* @param o - The object to search for in the list
*/
public boolean contains(Object o) {
ListNode p = list;
while (p != null && ((Comparable)o).compareTo(p.getInfo()) != 0)
p = p.getNext();
return p != null;
}
/**
* Returns true if this list contains no elements
*/
public boolean isEmpty() {
return list == null;
}
/**
* Removes all elements from the list
*/
public void clear() {
list = null;
}
/**
* Returns the number of elements in the list
*/
public int size() {
int count = 0;
ListNode p = list;
while (p != null) {
++count;
p = p.getNext();
}
return count;
}
}
public class Driver
{
public static void main(String args[])
{
LineWriter lw = new LineWriter("csis.txt");
Payroll payroll = new Payroll(lw);
payroll.readfile();
// payroll.printer(lw);
lw.close();
}
}