0

我正在尝试制作一个程序,在其中读取 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();
    }
}
4

2 回答 2

1

它应该是

public static void main

不是

public void main

(另外,恭喜,您在莱斯大学的运行 Java 代码的系统中发现了一个错误。向他们报告!)

于 2013-11-08T20:39:06.220 回答
0

看起来您正在使用非标准 Java 编译器。尝试使用 Sun 的/Oracle 或 IBM 的 javac 编译它,看看它是否会给您不同的跟踪。如果是这样,那么它可能只是您大学实施 javac 的错误。

我提到这一点是因为使用 JavacCompiler 类对代码的运行时执行是可疑的。

于 2013-11-08T20:38:50.010 回答