1

So when I call the firstCompare method I get a NullPointerException on this line if(numbers1[i].compareTo(numbers2[i]) == 0){

However, printing the arrays shows they are full. Please help!

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Fugitive {
    public static int counter = 0;
    public static int flag;

    static String[] numbers1 = new String[35];  
    static String[] numbers2 = new String[35];
    static String[] numbers3 = new String[35];

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



    }
    public static void loadNumbers() throws IOException {
        String print;
        BufferedReader in = new BufferedReader(new FileReader("creditCards1.txt"));
        BufferedReader in2 = new BufferedReader(new FileReader("creditCards2.txt"));
        BufferedReader in3 = new BufferedReader(new FileReader("creditCards3.txt"));


        for (int i = 0; i < 34; i++){
                //read in the data
                numbers1[i] = in.readLine();
                numbers2[i] = in2.readLine();
                numbers3[i] = in3.readLine();
                counter++;             
            }
         in.close();         
    }

    public static void firstCompare() {
        boolean found = false;
        for (int i = 0; i < 34; i++){
            if(numbers1[i].compareTo(numbers2[i]) == 0){
                flag = i;
                found = true;
                System.out.println(flag);
            }
        }
        if (!found){
            System.out.println("No matches");
    }
    }

}
cloomx
  • 11
  • 3
  • We'll have to see where `name` is defined and assigned values, as that is likely the cause of the NPE. – Marc Baumbach Sep 01 '15 at 01:43
  • I should also note that the array []name is a global variable – cloomx Sep 01 '15 at 01:44
  • I'm going to **guess** that `name` is `null`. Maybe try a debugger? Also, it's usually a good idea to pass the array as an argument to your `sort` method. – Elliott Frisch Sep 01 '15 at 01:44
  • `public class Contacts { //global variables public static int counter = 0; static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static int x; static String[] name = new String[101]; static String[] number = new String[101];` – cloomx Sep 01 '15 at 01:45
  • 2
    Great. So `name[0]` (through `name[100]`) is `null`. Glad we could help. – Elliott Frisch Sep 01 '15 at 01:45
  • @cloomx You should add that to the original question instead of in a comment. In addition, the first time through your loops, name has never been assigned a value, so `name[j]` will be null and attempting to call `compareTo` on null will cause the NPE. – Marc Baumbach Sep 01 '15 at 01:46
  • http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – user2864740 Sep 01 '15 at 01:52
  • @ElliottFrisch I forgot to include the part where it's loaded from text files (or user input). If I call the method for displaying contacts (print every index in the array) they print out fine, but when I try to sort them the array appears empty? Yes I'm sure there are values in the array before I even call sortContacts – cloomx Sep 01 '15 at 02:07
  • @cloomx It's the only thing that could be `null` in your method. Ergo, it's null. – Elliott Frisch Sep 01 '15 at 02:09
  • @ElliottFrisch I've edited my main post to show a much broader better example. All I'm trying to do is compare 3 arrays here but it's proving to be very difficult. Also, I don't get a null pointer exception in the load numbers method? – cloomx Sep 01 '15 at 02:18
  • I see you have three `Reader` instances, but you only read from one. Also, what are you trying to do? – Elliott Frisch Sep 01 '15 at 02:24
  • @ElliottFrisch Sorry about that... I'm trying to load 3 text files (Just a bunch of numbers on a new line, imitating credit card numbers with dashes) and compare all 3 arrays to see if there's a card number thats the same in all 3 text files. I updated the post fixing the issue you pointed out, thanks. The number 34 is how many entries are in each text file by the way – cloomx Sep 01 '15 at 02:27
  • Then the last entry in your array is null, because you allocated space for three arrays of 35 elements. – Elliott Frisch Sep 01 '15 at 02:55

1 Answers1

1

You should replace the number 100 and 10 in the loops with name.length