我正在尝试制作一个程序,在其中读取 2 个文件中的所有整数并将它们放入 2 个单独的数组列表中。然后我必须将列表合并在一起并对合并的列表进行排序。当我运行我的程序时,我不断收到以下错误,我不知道为什么
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
有谁知道我将如何解决这个问题?
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class ArraySort{
ArrayList<Integer> numberList = new ArrayList<Integer>();
ArrayList<Integer> numberList2 = new ArrayList<Integer>();
ArrayList<Integer> numberList3 = new ArrayList<Integer>();
public static void main(String[] args){
ArraySort x = new ArraySort();
x.merge();
x.splitList(9);
}
public void ArraySort(){
Scanner s = new Scanner(System.in);
try {
s = new Scanner (new File ("list1.txt")).useDelimiter("\\s+");
} catch(FileNotFoundException fnfe) {
System.out.println("file not found");
}
while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
numberList.add(s.nextInt());
} else {
s.next(); // else read the next token
}
}
try {
s = new Scanner (new File ("list2.txt")).useDelimiter("\\s+");
} catch(FileNotFoundException fnfe) {
System.out.println("file not found");
}
while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
numberList2.add(s.nextInt());
} else {
s.next(); // else read the next token
}
}
}
public void ArraySort(String x, String y){
Scanner s = new Scanner(System.in);
try {
s = new Scanner (new File (x)).useDelimiter("\\s+");
} catch(FileNotFoundException fnfe) {
System.out.println("file not found");
}
while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
numberList.add(s.nextInt());
} else {
s.next(); // else read the next token
}
}
try {
s = new Scanner (new File (y)).useDelimiter("\\s+");
} catch(FileNotFoundException fnfe) {
System.out.println("file not found");
}
while (s.hasNext()) {
if (s.hasNextInt()) { // check if next token is an int
numberList2.add(s.nextInt());
} else {
s.next(); // else read the next token
}
}
}
public void bubbleSort() {
for (int pass = 0; pass < numberList.size()-1; pass++) {
for (int i = 0; i < numberList.size()-1; i++) {
if (numberList.get(i) > numberList.get(i+1)) {
int temp = numberList.get(i);
numberList.set(i,numberList.get(i+1));
numberList.set(i+1,temp);
}
}
}
}
public void merge(){
int currentPosition = 0;
for( int i = 0; i < numberList.size(); i++) {
numberList3.set(currentPosition, numberList.get(i));
currentPosition++;
}
for( int j = 0; j < numberList2.size(); j++) {
numberList3.set(currentPosition, numberList.get(j));
currentPosition++;
}
}
public void splitList(int x){
int count1 = 0;
int count2 = 0;
for(int i=0;i<numberList.size();i++){
if(numberList.get(i)>=x){
numberList2.set(count1,numberList.get(i));
count1++;
}
else{
numberList3.set(count2,numberList.get(i));
count2++;
}
}
System.out.println();
for (int i = 0; i < numberList2.size(); i++){
System.out.print(numberList2.get(i) + " ");
}
System.out.println();
for (int i = 0; i < numberList3.size(); i++){
System.out.print(numberList3.get(i) + " ");
}
System.out.println();
}
}