【问题标题】:Array Index out of bounds exception error for java listjava列表的数组索引越界异常错误
【发布时间】:2015-10-22 06:32:25
【问题描述】:

我正在尝试将二维数组放入二维列表中。我正在构建一个谷歌 protobuf。我不知道为什么我会抛出异常。我在堆栈上尝试了多个建议,但没有一个能够提供帮助。

这是我的客户端代码:

public class google_cl extends Thread {


 static Array createArray() throws IOException{

     Array.Builder array = Array.newBuilder();

    double[][]arrays = new double[15][15000];

        for(int i=1; i< 15; i++){
            for(int j=1; j<15000;j++){
            arrays[i][j]=Math.random()*100; 
            }
        }
        for(int i=1;i<15;i++){
            for(int j=1;j<15000;j++){
                  array = Array.newBuilder().setArrays(i, arrays[1][j]);    
            }

            }
        //System.out.println(array);

        // Make connection and initialize streams
        Socket socket;
        try {
            socket = new Socket("localhost", 9898);
             OutputStream outputStream = socket.getOutputStream();
                InputStream inputStream = socket.getInputStream();


                ObjectOutputStream os = new ObjectOutputStream(outputStream);



        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
return null;     

}


public static void main(String[] args) throws IOException {


Average.Builder avger = Average.newBuilder();

//avger.mergeFrom();

try{
    avger.mergeFrom(new FileInputStream(args[0]));


}
 catch (IOException e) {
      System.out.println(args[0] + ": File not found.  Creating a new file.");
    }

    FileOutputStream output = new FileOutputStream(args[0]);// This is where              my error occurs.
    avger.build().writeTo(output);
    output.close();
}

}

这是我的服务器代码:

public class google_sr {


static void average(Average average){



List<Double> list = new ArrayList<Double>();

list = Array.newBuilder().getArraysList();

double max = 0.00;
Double sum = 0.00;

  if(!list.isEmpty()) {
    for (int i =1; i<15000;i++) {

        sum += list.get(i);
    }
   max= sum.doubleValue() / list.size();
  }

  Average.Builder avg = Average.newBuilder();
  System.out.println(avg.setAverages((int)max));

}


 public static void main(String[] args) throws Exception {

google_sr p = new google_sr();
     Average averag = Average.parseFrom(new FileInputStream(args[0]));//Where my error occurs
     average(averag);
        if (args.length != 1) {

          System.err.println("Usage Error");
          System.exit(-1);
        }

 }
  }

最后,这是我的 proto 文件:

message Array{
repeated double arrays = 1;
}

message Array2{

repeated Array arrays=1;

}

message Average{
optional int32 averages =1;

}

这是我的堆栈跟踪:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at cisc_434_google_protocol.google_cl.main(google_cl.java:82)

【问题讨论】:

  • 发布您的堆栈跟踪
  • 发布控制台输出,以便问题清晰
  • 刚刚做了,@SanjaySinghRawat
  • 看起来 try 有一个未捕获的索引越界异常,这就是错误在它之后的原因。我养成的一个好习惯是在对参数进行任何操作之前先检查 args 的长度。

标签: java arrays exception arraylist protocol-buffers


【解决方案1】:

您没有将 args[0] 中的文件名作为参数传递。您在错误的地方进行了检查:创建文件后,您正在检查 args.length。更改代码语句的顺序,以便先检查 args.length,然后再创建文件。

试试这个代码。

if ( args.length != 1){
    System.out.println("Exiting program. Usage: java google_cl fileName");
    System.exit(-1);
}
google_sr p = new google_sr();
Average averag = Average.parseFrom(new FileInputStream(args[0]));
average(averag);
  1. 如果你使用命令行运行程序,调用程序为

    java google_cl someFileName

    或者

  2. 如果您使用 Eclipse 之类的 IDE,请右键单击 java 类名称 -> 单击运行方式 -> 运行配置 -> 参数 -> 程序参数。在程序参数中添加文件名。

【讨论】:

    【解决方案2】:

    线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0
    在 cisc_434_google_protocol.google_cl.main(google_cl.java:82)

    表示在你的类的第 82 行,你试图访问数组的第一项,但数组是空的。

    如果这是第 82 行:

    Average averag = Average.parseFrom(new FileInputStream(args[0]));
    

    表示args 为空。换句话说,您没有将任何参数传递给您的程序。

    【讨论】:

    • 只是为了添加到您的答案中,这些参数需要在命令行中指定。如果您正在使用任何 IDE,例如 eclipse,请查看-cs.colostate.edu/helpdocs/cmd.pdf
    • 正是.. 正如@CrakC 提到的.. 你必须传递命令行参数,因为你似乎正在尝试获取命令行数据但没有传递它。
    猜你喜欢
    • 2014-11-30
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    相关资源
    最近更新 更多