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