快速方法异常

时间:2018-04-06 19:54:46

标签: java nullpointerexception

我正在尝试将文件中的名称读取到String数组中,并使用二进制搜索根据用户键入的内容查找名称但是我一直得到空指针异常?我觉得它与比较器有关,当看到是否有任何返回null但我不是100%肯定它是否。

    import java.util.*;
    import java.io.*;
    public class nameSearch{

                static String names[];
                int length;

                public static void main(String[] args)throws IOException{

                    nameSearch sorter = new nameSearch();
                    File f = new File("names.txt");
                    Scanner scan = new Scanner(f);
                    Scanner input = new Scanner(System.in);
                    names = new String[65];

                    int counter = 0;

                    while(scan.hasNext()){
                        counter = counter + 1;
                        scan.next();
                        for(int i=0; i < counter; i=i+1){
                                            names[i] = scan.next();
                                            System.out.println(names[i].toString());
                        }
                    }

                    sorter.sort(names);
                    System.out.println(names.toString());
                    scan.close();

                    System.out.println("Enter a name you want to search: ");
                    String s = input.nextLine();
                    sorter.binarySearch(s, names);

                }

                public String toString(){
                    return "Name: "+ names;
                }



                void sort(String[] array) {
                    if (array == null || array.length == 0) {
                        return;
                    }
                    this.names = array;
                    this.length = array.length;
                    quickSort(0, length - 1);
                }

                void quickSort(int lowerIndex, int higherIndex) {
                    int i = lowerIndex;
                    int j = higherIndex;
                    String pivot = this.names[lowerIndex + (higherIndex - lowerIndex) / 2];

                    while (i <= j) {
                        while (this.names[i].compareToIgnoreCase(pivot) < 0) {
                            i++;
                        }

                        while (this.names[j].compareToIgnoreCase(pivot) > 0) {
                            j--;
                        }

                        if (i <= j) {
                            exchangeNames(i, j);
                            i++;
                            j--;
                        }
                    }

                    if (lowerIndex < j) {
                        quickSort(lowerIndex, j);
                    }
                    if (i < higherIndex) {
                        quickSort(i, higherIndex);
                    }
                }

                void exchangeNames(int i, int j) {
                    String temp = this.names[i];
                    this.names[i] = this.names[j];
                    this.names[j] = temp;
                }

                void binarySearch(String s, String[] ar){
                     String stringToFind = s;
                        int lowestIndex = 0;
                        int highestIndex = ar.length-1;
                        int middleIndex = 0;

                        while(lowestIndex<=highestIndex){
                            middleIndex = (lowestIndex+highestIndex)/2;

                            if(stringToFind.compareTo(ar[middleIndex]) > 0){
                                lowestIndex = middleIndex+1;
                            }else if(stringToFind.compareTo(ar[middleIndex]) < 0){
                                highestIndex = middleIndex - 1;
                            }else{
                                break;
                            }
                        }

                        if(lowestIndex > highestIndex){
                            System.out.println("not found");
                        }else{
                            System.out.println("found at " + middleIndex);
        }
                }



    }

编译器告诉我有一个空指针异常:sorter.sort(names),quicksort(0,length-1),并从以下开始:
while(this.names [i] .compareToIgnoreCase(pivot)&lt; 0){

1 个答案:

答案 0 :(得分:1)

在第16行中,您已将names定义为String[65],并且您的文件中可能没有65个名称。所以一些names元素为空。 尝试使用arrayList。

public class NameSearch{

    static ArrayList<String> names;
    int length;

    public static void main(String[] args)throws IOException{

        NameSearch sorter = new NameSearch();
        File f = new File("src/names.txt");
        Scanner scan = new Scanner(f);
        Scanner input = new Scanner(System.in);
        names = new ArrayList<>();

        int counter = 0;

        while(scan.hasNext()){
            names.add(scan.next());
        }

        sorter.sort(names);
        System.out.println(names.toString());
        scan.close();

        System.out.println("Enter a name you want to search: ");
        String s = input.nextLine();
        sorter.binarySearch(s, names);

    }

    public String toString(){
        return "Name: "+ names;
    }



    void sort(ArrayList<String> array) {
        if (array == null || array.size() == 0) {
            return;
        }
        this.names = array;
        this.length = array.size();
        quickSort(0, length - 1);
    }

    void quickSort(int lowerIndex, int higherIndex) {
        int i = lowerIndex;
        int j = higherIndex;
        String pivot = this.names.get(lowerIndex + (higherIndex - lowerIndex) / 2);

        while (i <= j) {
            while (this.names.get(i).compareToIgnoreCase(pivot) < 0) {
                i++;
            }

            while (this.names.get(j).compareToIgnoreCase(pivot) > 0) {
                j--;
            }

            if (i <= j) {
                exchangeNames(i, j);
                i++;
                j--;
            }
        }

        if (lowerIndex < j) {
            quickSort(lowerIndex, j);
        }
        if (i < higherIndex) {
            quickSort(i, higherIndex);
        }
    }

    void exchangeNames(int i, int j) {
        Collections.swap(this.names, i, j);
    }

    void binarySearch(String s, ArrayList<String> ar){
        String stringToFind = s;
        int lowestIndex = 0;
        int highestIndex = ar.size()-1;
        int middleIndex = 0;

        while(lowestIndex<=highestIndex){
            middleIndex = (lowestIndex+highestIndex)/2;

            if(stringToFind.compareTo(ar.get(middleIndex)) > 0){
                lowestIndex = middleIndex+1;
            }else if(stringToFind.compareTo(ar.get(middleIndex)) < 0){
                highestIndex = middleIndex - 1;
            }else{
                break;
            }
        }

        if(lowestIndex > highestIndex){
            System.out.println("not found");
        }else{
            System.out.println("found at " + middleIndex);
        }
    }
}