null指针异常

vddsk6oq  于 2021-05-30  发布在  Hadoop
关注(0)|答案(1)|浏览(498)

我有一个像制表符分隔符的文本文件

  1. 20001204X00000 Accident 10 9 6 Hyd
  2. 20001204X00001 Accident 8 7 vzg 2
  3. 20001204X00002 Accident 10 7 sec 1
  4. 20001204X00003 Accident 23 9 kkd 23

我想得到输出的航班号,总乘客数,这里我要把所有数字列的值加起来,总乘客数是这样的

  1. 20001204X00000 25
  2. 20001204X00001 17
  3. 20001204X00002 18
  4. 20001204X00003 55

当尝试添加四个数字列时,我得到了nullpointer异常,请帮助如何避免nullpointerexception以及如何用零替换null或空格值
实际上这是hadoop map reduce java代码

  1. package com.flightsdamage.mr;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.StringTokenizer;
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.IntWritable;
  9. import org.apache.hadoop.io.LongWritable;
  10. import org.apache.hadoop.io.NullWritable;
  11. import org.apache.hadoop.io.Text;
  12. import org.apache.hadoop.mapreduce.Job;
  13. import org.apache.hadoop.mapreduce.Mapper;
  14. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  16. public class FlightsDamage {
  17. public static class FlightsMaper extends Mapper<LongWritable, Text, Text, LongWritable> {
  18. LongWritable pass2;
  19. @Override
  20. protected void map(LongWritable key, Text value,
  21. org.apache.hadoop.mapreduce.Mapper.Context context)
  22. throws IOException, InterruptedException,NumberFormatException,NullPointerException {
  23. String line = value.toString();
  24. String[] column=line.split("|");
  25. Text word=new Text();
  26. word.set(column[0]);
  27. String str = "n";
  28. try {
  29. long a = Long.parseLong(str);
  30. long a1=Long.parseLong("col1[]");
  31. long a2=Long.parseLong("col2[]");
  32. long a3=Long.parseLong("col3[]");
  33. long a4=Long.parseLong("col4[]");
  34. long sum = a1+a2+a3+a4;
  35. LongWritable pass0 = new LongWritable(a1);
  36. LongWritable pass = new LongWritable(a2);
  37. LongWritable pass1 = new LongWritable(a3);
  38. LongWritable pass3 = new LongWritable(a4);
  39. pass2 = new LongWritable(sum);
  40. } catch (Exception e) {
  41. // TODO: handle exception
  42. }finally{
  43. context.write(word,pass2);
  44. }
  45. }
  46. }
  47. public static void main(String[] args)throws Exception {
  48. Configuration conf = new Configuration();
  49. Job job = new Job(conf, "Flights MR");
  50. job.setJarByClass(FlightsDamage.class);
  51. job.setMapperClass(FlightsMaper.class);
  52. job.setOutputKeyClass(Text.class);
  53. job.setOutputValueClass(LongWritable.class);
  54. //FileInputFormat.addInputPath(job, new Path("/home/node1/data-AviationData.txt"));
  55. FileInputFormat.addInputPath(job, new Path("/home/node1/Filghtdamage.txt"));
  56. FileOutputFormat.setOutputPath(job, new Path("/home/node1/output"));
  57. System.exit(job.waitForCompletion(true) ? 0 : 1);
  58. }
  59. }
atmip9wb

atmip9wb1#

在解析字符串之前,需要检查它是否为数字类型。比如:

  1. int value = 0;
  2. if (StringUtils.isNumeric(str)) {
  3. value = Integer.parseInt(str);
  4. }

如果输入字符串是非数字的(可以是null或其他非数字值),stringutils.isnumeric()将返回false,并且变量将具有 0 作为默认值。
下面是一个简单的程序,演示stringutils.isnumeric()的用法
测试等级:

  1. import org.apache.commons.lang3.StringUtils;
  2. public class LineParse {
  3. public static void main(String[] args) {
  4. String[] input = {
  5. "20001204X00000\tAccident\t10\t9\t6\tHyd",
  6. "20001204X00001\tAccident\t\t8\t7\tvzg\t2",
  7. "20001204X00002\tAccident\t10\t7\t\tsec\t1",
  8. "20001204X00003\tAccident\t23\t\t9\tkkd\t23"
  9. };
  10. StringBuilder output = new StringBuilder();
  11. for (String line : input) {
  12. int sum = 0;
  13. String[] tokens = line.split("\t");
  14. if (tokens.length > 0) {
  15. output.append(tokens[0]);
  16. output.append("\t");
  17. for (int i = 1;i < tokens.length;i++) {
  18. // Check if String is of type numeric.
  19. if (StringUtils.isNumeric(tokens[i])) {
  20. sum += Integer.parseInt(tokens[i]);
  21. }
  22. }
  23. }
  24. output.append(sum);
  25. output.append("\n");
  26. }
  27. System.out.println(output.toString());
  28. }
  29. }

输出:

  1. 20001204X00000 25
  2. 20001204X00001 17
  3. 20001204X00002 18
  4. 20001204X00003 55

我假设所有的数字 Integer . 否则使用 Double.parseDouble() .

展开查看全部

相关问题