Я пытаюсь создать класс LinkedList, который может быть добавлен в виде файла данных из содержимого. Я получаю исключение NullPointerException, и я не совсем уверен, почему. Есть ли лучший способ решить эту проблему без использования Collections.sort?
public class LinkedList {
// Properties
Node head;
int count;
// Constructors
public LinkedList() {
head = null;
count = 0;
}
public LinkedList(Node newHead) {
head = newHead;
count += 1;
}
// Methods
// add
public void add(String newData) {
Node temp = new Node(newData);
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(temp);
count++;
}
//================================================================================================
public static void main(String[] args) throws IOException {
// Access the contents of a file
File file = new File("C:\\Users\\Marlene\\Workspace\\LinkedDict\\src\\com\\company\\unsorteddict.txt");
Scanner scan = new Scanner(file);
String fileContents = "";
LinkedList linkedList = new LinkedList();
com.company.LinkedList linkedList = new com.company.LinkedList();
while (scan.hasNextLine()) {
linkedList.add(fileContents);
}
FileWriter writer = new FileWriter(
"C:\\Users\\Marlene\\Workspace\\LinkedDict\\src\\com\\company\\sorteddict.txt");
writer.write(fileContents);
writer.close();
}
}