Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从文件中读取信息并将其放入arraylist_Java_Nullpointerexception - Fatal编程技术网

Java 从文件中读取信息并将其放入arraylist

Java 从文件中读取信息并将其放入arraylist,java,nullpointerexception,Java,Nullpointerexception,我试图制作一个程序,读取2个文件中的所有整数,并将它们放入2个单独的arraylist中。然后我必须将列表合并在一起,并对合并后的列表进行排序。当我运行我的程序时,我不断地得到以下错误,我不知道为什么 java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(

我试图制作一个程序,读取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();
    }
}
import java.util.ArrayList;
导入java.util.Scanner;
导入java.io.*;
公共类ArraySort{
ArrayList numberList=新的ArrayList();
ArrayList numberList 2=新的ArrayList();
ArrayList numberList 3=新的ArrayList();
公共静态void main(字符串[]args){
ArraySort x=新的ArraySort();
x、 合并();
x、 拆分列表(9);
}
公共无效ArraySort(){
扫描仪s=新的扫描仪(System.in);
试一试{
s=新扫描仪(新文件(“list1.txt”)。使用分隔符(\\s+”);
}捕获(FileNotFoundException fnfe){
System.out.println(“未找到文件”);
} 
而(s.hasNext()){
if(s.hasNextInt()){//检查下一个标记是否为int
numberList.add(s.nextInt());
}否则{
s、 next();//否则读取下一个标记
}
}
试一试{
s=新扫描仪(新文件(“list2.txt”)。使用分隔符(\\s+”);
}捕获(FileNotFoundException fnfe){
System.out.println(“未找到文件”);
} 
而(s.hasNext()){
if(s.hasNextInt()){//检查下一个标记是否为int
numberList2.add(s.nextInt());
}否则{
s、 next();//否则读取下一个标记
}
}
}
公共void数组排序(字符串x、字符串y){
扫描仪s=新的扫描仪(System.in);
试一试{
s=新扫描仪(新文件(x))。使用分隔符(\\s+);
}捕获(FileNotFoundException fnfe){
System.out.println(“未找到文件”);
} 
而(s.hasNext()){
if(s.hasNextInt()){//检查下一个标记是否为int
numberList.add(s.nextInt());
}否则{
s、 next();//否则读取下一个标记
}
}
试一试{
s=新扫描仪(新文件(y))。使用分隔符(\\s+);
}捕获(FileNotFoundException fnfe){
System.out.println(“未找到文件”);
} 
而(s.hasNext()){
if(s.hasNextInt()){//检查下一个标记是否为int
numberList2.add(s.nextInt());
}否则{
s、 next();//否则读取下一个标记
}
}
}
public void bubbleSort(){
对于(int pass=0;passnumberList.get(i+1)){
int temp=numberList.get(i);
numberList.set(i,numberList.get(i+1));
编号列表设置(i+1,温度);
} 
} 
} 
}
公共无效合并(){
int currentPosition=0;
对于(int i=0;i
看起来您使用的是非标准Java编译器。试着用Sun的/Oracle或IBM的javac编译它,看看它是否提供了不同的跟踪。如果是这样的话,那么这可能只是你所在大学的javac实现中的一个错误

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

应该是这样的

public static void main
不是


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

这是一条有趣的错误消息,因为它似乎与运行您的程序无关。你确定这是正确的错误信息吗?我同意罗宾的看法。这似乎是由于JavacCompiler类中的代码出现问题造成的,该类可能是由设置大学课程的人创建的。我在主方法中添加了static,错误消失了。但是现在当我运行我的程序时,没有打印任何内容。
public void main