读取信息将文件放入arraylist中

时间:2013-11-08 20:33:27

标签: java nullpointerexception

我正在尝试创建一个程序,我在其中读取2个文件中的所有整数,并将它们放入2个单独的arraylist中。然后我必须将列表合并在一起并对合并列表进行排序。我运行程序时遇到以下错误,我无法弄清楚原因

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

2 个答案:

答案 0 :(得分:1)

应该是

public static void main

public void main

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

答案 1 :(得分:0)

看起来您使用的是非标准Java编译器。尝试使用Sun的/ Oracle或IBM的javac进行编译,看看它是否为您提供了不同的跟踪。如果确实如此,那么你的大学实施javac可能只是一个错误。

我提到这一点,因为使用JavacCompiler类会对代码的运行时执行产生怀疑。