Java单链表

Java单链表,java,singly-linked-list,Java,Singly Linked List,因此,对于一个赋值,我基本上需要创建一个单链接列表,有两种方法,add(x)这会在列表的末尾添加一个新节点,以及从列表末尾删除的deleteMin() 底部是我编写的代码。我一直在第66行得到错误java.lang.NullPointerException 在head.next=u处的add(x)方法内部 我已经尝试了几次修改代码来修复这个错误,但似乎没有任何效果 package assignment1; import java.util.InputMismatchException; imp

因此,对于一个赋值,我基本上需要创建一个单链接列表,有两种方法,
add(x)
这会在列表的末尾添加一个新节点,以及从列表末尾删除的
deleteMin()

底部是我编写的代码。我一直在第66行得到错误
java.lang.NullPointerException

head.next=u处的add(x)方法内部

我已经尝试了几次修改代码来修复这个错误,但似乎没有任何效果

package assignment1;

import java.util.InputMismatchException;
import java.util.Scanner;

public class SLL {
    private int item;
    private SLL next;

    SLL(int x, SLL y){
        item = x;
        next = y;
    }

    public static SLL head;
    public static SLL tail;
    public static int n;

    public static void main(String[] args){
        boolean x = true;
        Scanner user_input = new Scanner(System.in);
        System.out.println("A = add element, R = remove element, L = to list items inside Singly-Linked-List, Q = to stop session");
        while (x == true) {
            String user = user_input.next();
            String userLow = user.toLowerCase();
            switch (userLow) {
                case "a":
                    System.out.println("Add an integer");
                    int a;
                    try {
                        a = user_input.nextInt();
                    } catch (InputMismatchException e) {
                        System.out.println("Incorect input, please input an integer.");
                        break;
                    }
                    System.out.println("");
                    add(a);
                    break;
                case "r":
                    deleteMin();
                    break;
                case "l":
                    //list();
                    break;
                case "q":
                    x = false;
                    break;
                case "help":
                    System.out.println("A = add element, R = remove element, L = to list items inside queue, Q = to stop session");
                    break;
                case "?":
                    System.out.println("A = add element, R = remove element, L = to list items inside queue, Q = to stop session");
                    break;
                default:
                    System.out.println("Not a recognized command: try 'help' or '?' for a list of commands");
                    break;
            }
        }
    }

    public static void add(int x){
        SLL u = new SLL(x, head);
        if (n == 0) {
            head.next = u; // <<<------------
            head.next.next = tail;
        } else {
            SLL current = head;
            for (int i = 0; i < n; i++) {
                current = current.next;
            }
            current.next = u;
            current.next.next = tail;
        }
        n++;
    }
    public static void deleteMin(){
        if (n == 0) {
            System.out.println("No elements inside list");
        } else if (n == 1) {
            head.next = null;
            tail.next = null;
            n--;
        } else {
            head.next = head.next.next;
            head.next.next = null;
            n--;
        }
    }
}
包分配1;
导入java.util.InputMismatchException;
导入java.util.Scanner;
公共类SLL{
私人物品;
私人SLL next;
SLL(整数x,整数y){
项目=x;
next=y;
}
公共静态SLL头;
公共静态SLL尾;
公共静态int n;
公共静态void main(字符串[]args){
布尔x=真;
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“A=添加元素,R=删除元素,L=列出单链表中的项目,Q=停止会话”);
while(x==true){
字符串user=user_input.next();
字符串userLow=user.toLowerCase();
开关(用户低){
案例“a”:
System.out.println(“添加整数”);
INTA;
试一试{
a=用户输入。nextInt();
}捕获(输入不匹配异常e){
System.out.println(“输入不正确,请输入一个整数”);
打破
}
System.out.println(“”);
添加(a);
打破
案例“r”:
deleteMin();
打破
案例“l”:
//list();
打破
案例“q”:
x=假;
打破
案例“帮助”:
System.out.println(“A=添加元素,R=删除元素,L=列出队列中的项目,Q=停止会话”);
打破
案例“?”:
System.out.println(“A=添加元素,R=删除元素,L=列出队列中的项目,Q=停止会话”);
打破
违约:
System.out.println(“不可识别的命令:请尝试“help”或“?”以获取命令列表”);
打破
}
}
}
公共静态无效添加(int x){
SLL u=新的SLL(x,头部);
如果(n==0){

head.next=u;//您尚未初始化
head
,但请尝试访问它的
next
属性。这会导致
NullPointerException
,因为
head
当时为
null

在尝试访问
head
变量的值之前,需要使用
head=new SSL(item,next)
之类的内容初始化该变量

当您在该行中设置下一个属性并且您的
SSL
构造函数也设置该属性时,您可能需要将
head.next=u;
替换为
head=new SSL(x,u);

按照您的操作方式,下一行还将导致
null点异常
,因为当您分配
head.next.next=tail;
head.next
null
。当您在
SLL u=new SLL(x,head);
中将其交给构造函数时,
head
是空的
另外,这一行也可能无效,因为此时
tail
也是
null
。所以您基本上是这样做的
head.next.next=null;

不要忘了说非常无组织well@KickButtowski实际上,我几秒钟前就输入了类似的内容,因为
u
似乎有点移位,但请扔掉它那就走吧。