我正在努力找出为什么我得到一个空指针异常。我知道这是因为null,但我似乎无法找到哪一个

时间:2021-10-13 07:32:39

Here is the code. When I started to run the program I entered the option number 1 and it gave me an error saying Exception in thread "main" java.lang.NullPointerException. Any help would be appreciated.

这是代码。当我开始运行程序时,我输入了选项编号1,它在线程“main”java.lang.NullPointerException中给出了一个错误说错误。任何帮助,将不胜感激。

The Database.txt should look like this

Database.txt应该如下所示

510421600;Shelley;Morgan
790701850;Holton;Jose
932371897;Hynes;Naomi
714797789;Kunkel;Dylan
878566780;Grisham;Ellie
810639750;Childs;Lillian

Code

import java.io.File;

import java.util.Scanner;

import java.io.*;

public class ListExample
{

    Node head;
    Node tail;

    public ListExample()
    {
        head = null;
    }

    public ListExample(Node head)
    {
        this.head = head;
    }

    //Load Database
    public void Database()
    {
        head = null;
        Node tmp = null; 
        Node lastFirst = tmp;
        Node lastSecond = tmp;
        Node lastThird = tmp;

        for(int i = 9;i >= 0; i--)
        {
            String L1 = Integer.toString(i);
            for(int j = 9;j >= 0; j--)
            {
                String L2 = Integer.toString(j);
                for(int k = 9;k >= 0; k--)
                {

                    String L3 = Integer.toString(k);
                    String allNum = L1 + L2 + L3 + "000000";

                    tmp = new Node(allNum);
                    tmp.third = lastThird;
                    tmp.fourth = lastThird;
                    lastThird = tmp;    
                }
                tmp.second = lastSecond;
                lastSecond = tmp;
            }
            tmp.first = lastFirst;
            lastFirst = tmp;    
        }
        head = tmp;
    }

    //Inserting Skip Search
    public void insertInSkip(String SSN, String lName, String fName)
    {

        Node tmp = head;

        while((tmp != null) && SSN.charAt(0) >= tmp.first.SSN.charAt(0) )
        {
            tmp = tmp.first;
        }
        while((tmp != null) && SSN.charAt(1) >= tmp.second.SSN.charAt(1) )
        {
            tmp = tmp.second;
        }
        while((tmp != null) && SSN.charAt(2) >= tmp.third.SSN.charAt(2) )
        {
            tmp = tmp.third;
        }
        System.out.println(SSN);
        System.out.println(tmp.fourth.SSN);
        System.out.println(SSN.compareTo(tmp.fourth.SSN));
        while((tmp != null) && (SSN.compareTo(tmp.fourth.SSN) > 0))
        {
            tmp = tmp.fourth;
        }
        if(SSN.compareTo(tmp.SSN) == 0)
        {
                return;
        }
        else
        {
            Node temp = new Node(SSN, fName, lName);
            temp.fourth = tmp.fourth;
            tmp.fourth = temp;
        }
        endTimer();  
    } 

    // SkipSearch method
    public void skipSearch(String SSN)
    {
        Node tmp = head;

        while((tmp.first != null) && (SSN.charAt(0) < tmp.first.SSN.charAt(0)))
        {
            tmp = tmp.first;
        }
        while((tmp.second != null) && SSN.charAt(1) < tmp.second.SSN.charAt(1))
        {
            tmp = tmp.second;
        }
        while((tmp.third != null) && SSN.charAt(2) < tmp.third.SSN.charAt(2))
        {
            tmp = tmp.third;
        }
        while((tmp.fourth != null) && SSN.charAt(3) < tmp.fourth.SSN.charAt(3))
        {
            tmp = tmp.fourth;
        }
        while(tmp.fourth != null)
        {
            tmp = tmp.fourth;
            if(tmp.SSN.equals(SSN))
            {
                System.out.println(tmp.toString()+"has been found.");
                endTimer();
                System.out.println("Search took: " + timeElapsed()+ " seconds");
            }
        }

    }  

    // Read the file Database.txt
   public void readFile(String data)
    {
        startTimer();
        File file = new File(data);
        String firstName = " ";
        int count = 0;
        try
        {
            Scanner scanner = new Scanner(file);
            while(scanner.useDelimiter(";") != null && scanner.hasNext())
            {
                String ssn = scanner.next();
                String lastN = scanner.next();
                if(scanner.useDelimiter("\r") != null)
                {
                    scanner.skip(";");
                    String firstN = scanner.next();
                    firstName = firstN;
                    if(scanner.hasNext())
                    {
                        scanner.skip("\r");
                        scanner.skip("\n");
                    }
                }
                insertInSkip(ssn, firstName, lastN);
                count++;
                if(count % 1000 == 0)
                {
                    System.out.print(".");
                } 
            }
            scanner.close();
            endTimer();
            System.out.println("Data loaded in " + timeElapsed() + " secs.");
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }
    } 

   // End of program
    public void endProg()
    {
        System.out.println("Program exiting..");
        System.exit(0);
    }

    public long startTimer()
    {
        return System.currentTimeMillis();
    }

    public long endTimer()
    {
        return System.currentTimeMillis();
    }

    public float timeElapsed()
    {
        return (endTimer() - startTimer()) / 1000;
    }

    //run the program
    public void run(ListExample list)
    {
        Scanner scanner = new Scanner(System.in);
        int option;
        System.out.println("Select an option: ");
        System.out.println("1. Load database\n2. Skip Search\n3. Exit ");
        option = scanner.nextInt();    
        while(true)
        {
            switch(option)
            {
            case 1:

                list.Database();
                list.readFile("Database.txt");
                break;

            case 2:
                System.out.println("Enter SSN: ");
                String ssn2 = scanner.next();
                list.skipSearch(ssn2);                
                break;

            case 3:
                endProg();
                break;

            default:
                System.out.println("Incorrect value entered. Please enter a number between 1 and 3.");
                System.out.println("Select an option: ");
                System.out.println("1. Load database\n2. Skip Search\n3. Exit ");
                option = scanner.nextInt();
                break;
            }
        System.out.println("Select an option: ");
        System.out.println("1. Load database\n2. Skip Search\n3. Exit ");
        option = scanner.nextInt();

        }
    }

    public static void main(String[] args) {
        ListExample list = new ListExample();
        list.run(list);
    }
}


Node.java


public class Node {

    public String SSN, fName, lName, allNum;
    public Node first;
    public Node second;
    public Node third;
    public Node fourth;
    public Node head, tail, next;

    public Node(String allNum){
        this.allNum = allNum;
    }
    public Node(String SSN, String fName, String lName){
        this.SSN = SSN;
        this.fName = fName;
        this.lName = lName;
    }

}

1 个解决方案

#1


0  

I've debugged your code. So look at line

我调试了你的代码。所以看看行

while((tmp != null) && SSN.charAt(0) >= tmp.first.SSN.charAt(0) )

tmp.first.SSN gives NULL, and you're trying to get charAt from it. Check it:

tmp.first.SSN给出NULL,你正试图从中获取charAt。核实:

System.out.println(tmp.first.SSN);

So you can add some condition to prevent this exception.

因此,您可以添加一些条件来防止此异常。

Look at your insertInSkip method:

看看你的insertInSkip方法:

    //Inserting Skip Search
    public void insertInSkip(String SSN, String lName, String fName)
    {

        Node tmp = head;
//        System.out.println(tmp.first);
//        System.out.println(tmp.second);
//        System.out.println(tmp.third);
//        System.out.println(tmp.first.SSN);

        while((tmp != null) && (tmp.first.SSN != null) && SSN.charAt(0) >= tmp.first.SSN.charAt(0) ) // changed
        {
            tmp = tmp.first;
        }

        while((tmp != null) && (tmp.second.SSN != null) && SSN.charAt(1) >= tmp.second.SSN.charAt(1) ) // changed
        {
            tmp = tmp.second;
        }

        while((tmp != null) && (tmp.third.SSN != null) && SSN.charAt(2) >= tmp.third.SSN.charAt(2) ) // changed
        {
            tmp = tmp.third;
        }
        System.out.println("ssn="+SSN);
        System.out.println(tmp.fourth.SSN);
        if (tmp.fourth.SSN != null){  // changed
            System.out.println(SSN.compareTo(tmp.fourth.SSN));
        }

        while((tmp != null) && (tmp.fourth.SSN != null) && (SSN.compareTo(tmp.fourth.SSN) > 0)) // changed
        {
            tmp = tmp.fourth;
        }
        if(SSN.equals(tmp.SSN))
        {
                return;
        }
        else
        {
            Node temp = new Node(SSN, fName, lName);
            temp.fourth = tmp.fourth;
            tmp.fourth = temp;
        }
        endTimer();  
    } 

Here's the output:

这是输出:

Select an option: 
1. Load database
2. Skip Search
3. Exit 
1
ssn=510421600
null
ssn=790701850
510421600
2
ssn=932371897
510421600
4
ssn=714797789
510421600
2
ssn=878566780
510421600
3
ssn=810639750
510421600
3
Data loaded in 0.0 secs.
Select an option: 
1. Load database
2. Skip Search
3. Exit 

UPD SkipSearch :

UPD SkipSearch:

// SkipSearch method
public void skipSearch(String SSN)
{
    Node tmp = head;
    //System.out.println(tmp);

    if (tmp != null) {
        while((tmp.first != null) 
                && (tmp.first.SSN != null) 
                && (SSN.charAt(0) < tmp.first.SSN.charAt(0))
                )
        {
            tmp = tmp.first;
        }
        while((tmp.second != null) 
                && (tmp.second.SSN != null) 
                && SSN.charAt(1) < tmp.second.SSN.charAt(1))
        {
            tmp = tmp.second;
        }
        while((tmp.third != null) 
                && (tmp.third.SSN != null) 
                && SSN.charAt(2) < tmp.third.SSN.charAt(2))
        {
            tmp = tmp.third;
        }
        while((tmp.fourth != null) 
                && (tmp.fourth.SSN != null) 
                && SSN.charAt(3) < tmp.fourth.SSN.charAt(3))
        {
            tmp = tmp.fourth;
        }
        while(tmp.fourth != null)
        {
            tmp = tmp.fourth;
            if(tmp.SSN.equals(SSN))
            {
                System.out.println(tmp.toString()+"has been found.");
                endTimer();
                System.out.println("Search took: " + timeElapsed()+ " seconds");
            }
        }
    }
}  

#1


0  

I've debugged your code. So look at line

我调试了你的代码。所以看看行

while((tmp != null) && SSN.charAt(0) >= tmp.first.SSN.charAt(0) )

tmp.first.SSN gives NULL, and you're trying to get charAt from it. Check it:

tmp.first.SSN给出NULL,你正试图从中获取charAt。核实:

System.out.println(tmp.first.SSN);

So you can add some condition to prevent this exception.

因此,您可以添加一些条件来防止此异常。

Look at your insertInSkip method:

看看你的insertInSkip方法:

    //Inserting Skip Search
    public void insertInSkip(String SSN, String lName, String fName)
    {

        Node tmp = head;
//        System.out.println(tmp.first);
//        System.out.println(tmp.second);
//        System.out.println(tmp.third);
//        System.out.println(tmp.first.SSN);

        while((tmp != null) && (tmp.first.SSN != null) && SSN.charAt(0) >= tmp.first.SSN.charAt(0) ) // changed
        {
            tmp = tmp.first;
        }

        while((tmp != null) && (tmp.second.SSN != null) && SSN.charAt(1) >= tmp.second.SSN.charAt(1) ) // changed
        {
            tmp = tmp.second;
        }

        while((tmp != null) && (tmp.third.SSN != null) && SSN.charAt(2) >= tmp.third.SSN.charAt(2) ) // changed
        {
            tmp = tmp.third;
        }
        System.out.println("ssn="+SSN);
        System.out.println(tmp.fourth.SSN);
        if (tmp.fourth.SSN != null){  // changed
            System.out.println(SSN.compareTo(tmp.fourth.SSN));
        }

        while((tmp != null) && (tmp.fourth.SSN != null) && (SSN.compareTo(tmp.fourth.SSN) > 0)) // changed
        {
            tmp = tmp.fourth;
        }
        if(SSN.equals(tmp.SSN))
        {
                return;
        }
        else
        {
            Node temp = new Node(SSN, fName, lName);
            temp.fourth = tmp.fourth;
            tmp.fourth = temp;
        }
        endTimer();  
    } 

Here's the output:

这是输出:

Select an option: 
1. Load database
2. Skip Search
3. Exit 
1
ssn=510421600
null
ssn=790701850
510421600
2
ssn=932371897
510421600
4
ssn=714797789
510421600
2
ssn=878566780
510421600
3
ssn=810639750
510421600
3
Data loaded in 0.0 secs.
Select an option: 
1. Load database
2. Skip Search
3. Exit 

UPD SkipSearch :

UPD SkipSearch:

// SkipSearch method
public void skipSearch(String SSN)
{
    Node tmp = head;
    //System.out.println(tmp);

    if (tmp != null) {
        while((tmp.first != null) 
                && (tmp.first.SSN != null) 
                && (SSN.charAt(0) < tmp.first.SSN.charAt(0))
                )
        {
            tmp = tmp.first;
        }
        while((tmp.second != null) 
                && (tmp.second.SSN != null) 
                && SSN.charAt(1) < tmp.second.SSN.charAt(1))
        {
            tmp = tmp.second;
        }
        while((tmp.third != null) 
                && (tmp.third.SSN != null) 
                && SSN.charAt(2) < tmp.third.SSN.charAt(2))
        {
            tmp = tmp.third;
        }
        while((tmp.fourth != null) 
                && (tmp.fourth.SSN != null) 
                && SSN.charAt(3) < tmp.fourth.SSN.charAt(3))
        {
            tmp = tmp.fourth;
        }
        while(tmp.fourth != null)
        {
            tmp = tmp.fourth;
            if(tmp.SSN.equals(SSN))
            {
                System.out.println(tmp.toString()+"has been found.");
                endTimer();
                System.out.println("Search took: " + timeElapsed()+ " seconds");
            }
        }
    }
}