Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java LinkedList NullPointerException?_Java_List_Nodes - Fatal编程技术网

Java LinkedList NullPointerException?

Java LinkedList NullPointerException?,java,list,nodes,Java,List,Nodes,嘿,我似乎无法让我的代码正常工作,构造函数有点问题,我就是搞不清楚它是什么。 我让它为构造函数工作,并尝试了一些方法,但即使如此,它仍然给了我NullPointerException import java.util.NoSuchElementException; public class LinkedString { //Required Nodes/counters to track positions in LinkedList private No

嘿,我似乎无法让我的代码正常工作,构造函数有点问题,我就是搞不清楚它是什么。 我让它为构造函数工作,并尝试了一些方法,但即使如此,它仍然给了我NullPointerException

    import java.util.NoSuchElementException;

    public class LinkedString
     {

//Required Nodes/counters to track positions in LinkedList

    private Node start;
    private Node end;
    private int counter;
    private int i = 0;

//Inner class Node provides the node object required to create links

    private class Node
    {
        public char data;
        public Node next;
    }

//This constructor allows the user to input a array of characters
//and have them created into a LinkedString object

    public LinkedString(char[] value)
    {
        start.data = value[0];
        counter = 0;
        for(int i = 0 ; i < value.length ; i++)
        {
            if ( start == null)
            {
                start.data = value[i];
                start.next = end;
                counter++;
            }
            else
            {   
                Node newNode = new Node();
                newNode.data = value[i];
                end.next = newNode;
                newNode.next = null;
                end = newNode;
                counter++;
            }
        }   
    }
//This constructor allows the user to input a String
//getting each character and creating it into a LinkedString object

    public LinkedString(String value)
    {
        counter=0;
        for(int i = 0; i < value.length(); i++)
        {
            if(start == null)
            {
                start.data = value.charAt(i);
                start.next = end;
                counter++;
            }
            else
            {
                Node newNode = new Node();
                newNode.data=value.charAt(i);
                end.next = newNode;
                newNode.next = null;
                end = newNode;
            }
        }
    }

//This accessor returns a value at the specified index
//The 1st character has an index of 0 and will throw
//a no such element exception if there is no char @ given index

    public char charAt(int index)
    {
        Node current = start;
        boolean found = false;
        char temp = 0;
        int i = 0;
        while (current.next != null && i <= index)
        {
            current = current.next;
            if(i == index)
            {
                found = true;
                //Return done, and Data is wrong Edit.
            }
            i++;
            temp = current.data;
        }

        if(found == false) {
            throw new NoSuchElementException();
        }
        return temp;

    }   

//concat connects two linkedString objects together and updates the
//counter for how many chars are in the combined list

    public LinkedString concat(LinkedString value)
    {
        end.next = value.start;
        end = value.end;
        counter = counter + value.length();
        return value;
    }
//isEmpty checks to see if the list has a initial value
//if it doesnt its empty and returns true

    public boolean isEmpty()
    {
        if(start == null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
//length() returns the counter variable which counts the number of
// characters in the linked string

    public int length()
    {
        return counter;
    }

//substring returns a copy of the linked string from a specified point
//to another, if the user attempts to copy a value from a not existing 
//index point it throws NoSuchElementException  

    public Node substring(int startIndex, int endIndex)
    {
        Node current = start;
        Node sIndex;
        Node eIndex;
        Node copy = null;
        int i = 0;
        while(current.next != null && i <= startIndex)
        {
            current = current.next;
            if(i == startIndex)
            {
                sIndex = current;
                sIndex.next = copy;
                while(current.next != null && i <= endIndex)
                {
                    current = current.next;
                    copy.next = current;
                    copy = current;
                    if(i == endIndex)
                    {                           
                        eIndex = current;
                        eIndex.next = null;
                        copy.next = eIndex;
                    }
                    i++;
                }
                if(i < endIndex)
                    throw new NoSuchElementException();

            }
            i++;    
        }
        return copy;

    }
    }


    public class LinkedStringMain {
    public static void main(String[] args) {
        LinkedString linked = new LinkedString("stack");
        System.out.println(" " + linked.charAt(0));
        System.out.println(" " + linked.isEmpty());
    }
    }
import java.util.NoSuchElementException;
公共类链接字符串
{
//跟踪LinkedList中位置所需的节点/计数器
专用节点启动;
专用节点端;
专用int计数器;
私有整数i=0;
//内部类节点提供创建链接所需的节点对象
私有类节点
{
公共字符数据;
公共节点下一步;
}
//此构造函数允许用户输入字符数组
//并将它们创建为LinkedString对象
公共链接字符串(字符[]值)
{
start.data=值[0];
计数器=0;
for(int i=0;iwhile(current.next!=null&&i错误是,在检查是否为null后,未在任何构造函数中初始化启动。若要解决此问题,请使用
start=new Node();
初始化启动。更改如下所示:

//This constructor allows the user to input a array of characters
//and have them created into a LinkedString object

public LinkedString(char[] value)
{
    start.data = value[0];
    counter = 0;
    for(int i = 0 ; i < value.length ; i++)
    {
        if ( start == null)
        {
            start = new Node(); //initialize start
            start.data = value[i];
            start.next = end;
            counter++;
        }
        else
        {   
            Node newNode = new Node();
            newNode.data = value[i];
            end.next = newNode;
            newNode.next = null;
            end = newNode;
            counter++;
        }
    }   
}
//This constructor allows the user to input a String
//getting each character and creating it into a LinkedString object

public LinkedString(String value)
{
    counter=0;
    for(int i = 0; i < value.length(); i++)
    {
        if(start == null)
        {
            start = new Node(); //initialize start
            start.data = value.charAt(i);
            start.next = end;
            counter++;
        }
        else
        {
            Node newNode = new Node();
            newNode.data=value.charAt(i);
            end.next = newNode;
            newNode.next = null;
            end = newNode;
        }
    }
}
//此构造函数允许用户输入字符数组
//并将它们创建为LinkedString对象
公共链接字符串(字符[]值)
{
start.data=值[0];
计数器=0;
for(int i=0;i
错误是从哪一行抛出的?哪一行代码向您提供了NullPointerException,以及在什么情况下?抱歉,应该在LinkedString的线程“main”java.lang.NullPointerException中添加该异常。(LinkedString.java:56)LinkedStringMain.main(LinkedStringMain.java:4)中可能存在的重复