简体   繁体   中英

LinkedList NullPointerException?

Hey I can't seem to get my code to work, there is something wrong with the constructor and I just can't figure out what it is. I had it working for the constructor and tried it for some of the methods but even then it still gave me 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());
    }
    }

The error is that start is not being initialized in your either of your constructors after you check if it is null. To fix this, initialize start using start = new Node(); . The changes are implemented below:

//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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM