0

I am trying to read names from a file into an String array and use binary search to find the name based on what the user types in but I keep getting a null pointer exception? I feel like it has something to do with the comparators when seeing if anything returns a null but i am not 100% sure if its that.

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



    }

The compiler is telling me that there is a null pointer exception on: sorter.sort(names) , quicksort(0, length-1), and starting at:
while (this.names[i].compareToIgnoreCase(pivot) < 0) {

1 Answers1

1

In line 16 you have defined names as String[65] and probably there is no 65 names in your file. so some of names elements are null. try using 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);
        }
    }
}
Hamid Ghasemi
  • 880
  • 3
  • 13
  • 32
  • thanks for your reply! The file i am reading from does have 65 names however. The names get printed out but after they are , it throws an exception. I can show you the error message – nathan Heath Apr 06 '18 at 21:04
  • Exception in thread "main" java.lang.NullPointerException at java.lang.String$CaseInsensitiveComparator.compare(String.java:1193) at java.lang.String$CaseInsensitiveComparator.compare(String.java:1186) at java.lang.String.compareToIgnoreCase(String.java:1239) at nameSearch.quickSort(nameSearch.java:59) at nameSearch.sort(nameSearch.java:50) at nameSearch.main(nameSearch.java:28) – nathan Heath Apr 06 '18 at 21:04
  • there was another problem in your while loop in main method. edited my answer. check it please :) – Hamid Ghasemi Apr 06 '18 at 21:27
  • It says it cannot find the variable Collections.swap(this.names, i, j); I probably have to import something, not 100% sure though – nathan Heath Apr 06 '18 at 21:29
  • absolutely you should import `java.util.Collections`. – Hamid Ghasemi Apr 06 '18 at 21:32
  • Awesome, it works. Ill give you an upvote, thank you so much!! – nathan Heath Apr 06 '18 at 21:51