解决java.lang.ArrayindexOutOfBoundsException

问题描述 投票:0回答:3

有人可以帮助我解决此错误吗?

package bigdata.tp1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.log4j.BasicConfigurator;

public class WordCount {
    public static void main(String[] args) throws Exception {
        BasicConfigurator.configure();
        Configuration conf = new Configuration();
        Path inputPath=new Path(args[0]);
        Path outputPath=new Path(args[1]);// ligne 16
        Job job = Job.getInstance(conf, "wordcount");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(MyMapper.class);
        job.setCombinerClass(MyReducer.class);
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, inputPath);
        FileOutputFormat.setOutputPath(job,outputPath);
        if (!job.waitForCompletion(true))
            return;
    }

}

enter image description here

线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:索引1超出长度1的范围在bigdata.tp1.WordCount.main(WordCount.java:16)

java indexoutofboundsexception
3个回答
0
投票

好像您想访问第二个comandline参数,而只提供一个参数。在访问之前,请检查参数计数。


0
投票

如果您希望有2个程序参数,则需要先检查它们。

public static void main(String[] args) throws Exception {
   if(args.length != 2){
       throw new IllegalArgumentException("Exact 2 arguments needed");
   }
   //your code here
}

0
投票

我忘记在程序的参数中添加第二个参数,即:src / main / resources / output。因此,在“运行->编辑设置...→+→应用程序”下,我必须在“程序参数”下添加src / main / resources / output

© www.soinside.com 2019 - 2024. All rights reserved.