0

I am new to java language and this my first time using scanner lib. I pretty sure I prevent null pointer happen in the method, but when I run the main, the stack trace says:

Exception in thread "main" java.lang.NullPointerException at Big_data_package.sort_test_main.readintoarr(sort_test_main.java:22) at Big_data_package.sort_test_main.main(sort_test_main.java:60)

It happen on nextInt function on scanner method. could you guys help me point the error in my code? Thanks alot.

public class sort_test_main{

static File inputfile = new File("E:\\Java_workspace\\Big_Data_Sort\\io\\input.txt");
static mergeSort merge = new mergeSort();
static Scanner scanner;
static int[] inputarr;

public static int[] readintoarr (int[] list, Scanner scanner) throws IOException{
    int i = 0;
    boolean endfile = false;

    scanner = new Scanner(inputfile);

        while(!endfile)
        {
            if(scanner.hasNextInt()){
             list[i++] = scanner.nextInt();
            }
            else{
                endfile = true;
            }
        }
        scanner.close();

    return list;
}// end of readintoarr

public static void arrtofile (int[]x) throws IOException{

    File file = new File("E:\\Java_workspace\\Big_Data_Sort\\io\\output.txt");

    // if file doesnt exists, then create it
    if (!file.exists()) {
        file.createNewFile();
    }

    BufferedWriter outputWriter = null;


      outputWriter = new BufferedWriter(new FileWriter(file));
      for (int i = 0; i < x.length; i++) {
        // Maybe:
        outputWriter.write(x[i]+"");
        // Or:
        outputWriter.write(Integer.toString(x[i]));
        outputWriter.newLine();
      }
      outputWriter.flush();  
      outputWriter.close();  
}//end of arrtofile

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

    readintoarr(inputarr,scanner);
    merge.mergeSort1(inputarr,0,inputarr.length);
    arrtofile(inputarr);

}}
SLePort
  • 15,211
  • 3
  • 34
  • 44

1 Answers1

1

You have not allocated memory to list/inputarr. Try this:

inputarr = new inputarr[100]
Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
  • the truth is, the number of int i want to process is about ten millions. what your suggestion on this? should i make inputarr = new inputarr[Integer.MAX_VALUE]? – grahmada Jun 12 '16 at 05:29
  • Integer.MAX_VALUE is some 10^9. ten millions is 10^7. So make an array of size 10^7. – Priyansh Goel Jun 12 '16 at 05:31