Ich bin neu in Java Sprache und das ist mein erstes Mal mit Scanner lib. ich ziemlich sicher, dass ich verhindern, dass Null-Zeiger in dem Verfahren geschehen, aber wenn ich die Haupt laufe, der Stack-Trace sagt:Java Null Pointer Ausnahme auf scanner.nextint()
Exception in thread "main" java.lang.NullPointerException bei Big_data_package.sort_test_main.readintoarr (sort_test_main.java:22) bei Big_data_package.sort_test_main.main (sort_test_main.java:60)
Es kommt auf nextInt
Funktion auf scanner
Methode. Könnten Sie mir helfen, den Fehler in meinem Code zu zeigen? Danke vielmals.
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);
}}
Wie rufen Sie diese Methode? Ist 'liste' null? –
Ich möchte ein Array durch Lesen von Datei füllen, und ich rufe die Methode auf Hauptmethode unter – grahmada
'static int [] inputarr;' ist äquivalent zu 'static int [] inputarr = null;' –