Опитвам се да създам 2d двойно свързан кръгъл масив, като чета данни от txt файл и автоматично създавам възли. Моята програма чете правилно първия ред, но когато достигне следващия ред и време за създаване на следващия възел, се появява нулев указател. Не разбирам защо се случва, моля, помогнете ми.
public class project1 {
public static void main(String[] args) {
File file = new File("Input0.txt");
List mList = new List();
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
String line = sc.nextLine();
Node kNode = new Node(line.charAt(0));
mList.insertLast(kNode);
for (int j = 1; j < line.length(); j++) {
System.out.println(line.charAt(j));
}
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
class Node {
int data = 0;
char key;
Node nPrev, nNext, tNode, prev, next;
Node() {
}
Node(char c) {
key = c;
}
Node(Node x, Node p, Node q) {
tNode = x;
nPrev = p;
nNext = q;
}
Node(int x, Node p, Node q) {
data += x;
prev = p;
next = q;
}
}
class List {
Node head;
List() {
head = new Node();
head.prev = head;
head.next = head;
}
void insertFirst(char x) {
insertBefore(head.next, x);
}
void insertFirst(Node x) {
insertBefore(head.next, x);
}
void insertLast(char x) {
insertAfter(head.prev, x);
}
void insertLast(Node x) {
insertAfter(head.prev, x);
}
void insertAfter(Node pos, int i) {
Node n = new Node(i, pos, pos.next);
pos.next.prev = n;
pos.next = n;
}
void insertAfter(Node pos, Node x) {
Node n = new Node(x, pos, pos.next);
pos.next.prev = n;
pos.next = n;
}
void insertBefore(Node pos, int i) {
Node n = new Node(i, pos.prev, pos);
pos.prev.next = n;
pos.prev = n;
}
void insertBefore(Node pos, Node x) {
Node n = new Node(x, pos.prev, pos);
pos.prev.next = n;
pos.prev = n;
}
}
това са грешките. Нулев указател се случва, когато се опитва да създаде втори възел. той създава първия възел правилно, отколкото казва Нулев указател веднага след това.
ред 77 = поз.следващ = n;
ред 69 = insertAfter(head.prev, x);
ред 18 = mList.insertLast(kNode);