我是java语言的新手,这是我第一次使用 scanner lib 。 我很确定我在方法中阻止了空指针,但是当我运行main时,堆栈跟踪说:
线程中的异常" main" java.lang.NullPointerException at Big_data_package.sort_test_main.readintoarr(sort_test_main.java:22) 在Big_data_package.sort_test_main.main(sort_test_main.java:60)
它发生在nextInt
方法的scanner
函数上。
你能帮我指出我的代码中的错误吗?非常感谢。
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);
}}
答案 0 :(得分:1)
您尚未为list / inputarr分配内存。试试这个:
import java.util.Scanner;
public class sampleMethods {
public static void main(String[] args) {
printYikes();
int x = 12;
x = getAnInt("Please enter an integer min and a max seperated by a space: ",y, z);
System.out.println("\nThat was a valid Number.");
System.out.println("You entered " + x + "!");
System.out.println( "x is " + x );
x = doubleMyNumber(x);
System.out.println( "x is " + x );
x = tripleMyNumber(x);
System.out.println( "x is " + x );
}
static void printYikes() {
System.out.println("Yikes");
}
static int doubleMyNumber(int a) {
int b = a * 2;
return b;
}
static int tripleMyNumber(int a) {
int c = a * 3;
boolean odd = isOddOrEven(c);
if (odd) {
System.out.println("Number is Odd!!");
} else {
System.out.println("Number is Even!!");
}
return c;
}
static boolean isOddOrEven (int d) {
boolean isOdd = false;
if (d % 2 == 0) {
isOdd = false;
} else {
isOdd = true;
}
return isOdd;
}
static int getAnInt(String prompt, int min, int max) {
Scanner keyboard = new Scanner(System.in);
boolean numberError = false;
int enteredNumber = 0;
String enteredString = "";
do {
try {
System.out.print(prompt);
enteredString = keyboard.nextLine(); //Read into a string
enteredNumber = Integer.parseInt(enteredString.trim()); //then cast as a integer
numberError = false; //if we haven't bailed out, then the number must be valid.
if (enteredNumber < min || enteredNumber > max) {
numberError = true;
System.out.println("Your entry: \"" + enteredNumber + "\" is out of range...Please try again");
}
} catch(Exception e) {
System.out.println("Your entry: \"" + enteredString + "\" is invalid...Please try again");
numberError = true; //Uh-Oh...We have a problem.
}
} while (numberError == true ); //Keep asking the user until the correct number is entered.
return enteredNumber;
}
}