为了账号安全,请及时绑定邮箱和手机立即绑定

调试MapReduce代码查找NULLPointerException错误原因

调试MapReduce代码查找NULLPointerException错误原因

慕莱坞森 2021-12-30 16:14:34
我正在尝试运行 MapReduce 代码来计算每个部门的平均满意度。下面是示例数据。该文件有标题,为了删除标题,我将它们放在另一个 txt 文件中,并用它来检查行值是否不等于标题。样本数据 满意度_水平、上次评估、数量_项目、平均_montly_hours、time_spend_company、Work_accident、left、promotion_last_5years、sales、salary0.38,0.53,2,157,3,0,1,0,sales,low 0.8,0.86,5,262,6,0,1,0,sales,medium 0.11,0.88,7,272,4,0,1,0,sales,中 0.72,0.87,5,223,5,0,1,0,销售额,低 0.37,0.52,2,159,3,0,1,0,销售额,低 0.41,0.5,2,153,3,0,1,0,销售额,低 0.1,0.77,6,247,4,0,1,0,销售额,低 0.92,0.85,5,259,5,0,1,0,销售额,低 0.89,1,5,224,5,0,1,0,销量,低下面是我的映射器代码。public class SatisfactionLevelMapper extends Mapper<LongWritable, Text, Text, FloatWritable>{     String header; @Override protected void setup(Mapper.Context context) throws IOException, InterruptedException /* Headers are placed in a separate header.txt file for which the location is specified in the driver */{    BufferedReader bufferedReader = new BufferedReader(new FileReader("header.txt"));    header = bufferedReader.readLine();}    public void map(LongWritable key, Text text, Context context) throws IOException, InterruptedException{    String row = text.toString();    String [] values = row.trim().split(","); //dataset is a CSV file with 10 columns    float satisfaction = 0.0f;    String dept = null;    try    {        if(values.length == 9 && header != row)        {            satisfaction = Float.parseFloat(values[0]);         }    }    catch (Exception e)    {        e.printStackTrace();    }    context.write(new Text(dept), new FloatWritable(satisfaction));    }}我收到如下 NullPointerException 错误。Error: java.lang.NullPointerException    at org.apache.hadoop.io.Text.encode(Text.java:450)    at org.apache.hadoop.io.Text.set(Text.java:198)    at org.apache.hadoop.io.Text.<init>(Text.java:88)    at com.df.hra.SatisfactionLevelMapper.map SatisfactionLevelMapper.java:62)    at com.df.hra.SatisfactionLevelMapper.map(SatisfactionLevelMapper.java:1)有没有办法找出我的代码的哪一部分有问题?我是 Java 新手,正在尝试找出调试 MapReduce 代码的方法。任何帮助,将不胜感激。谢谢!
查看完整描述

1 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

你不能用 初始化一个Textnull,就像你在这里做的那样(用dept)。考虑使用空字符串 ( "") 代替。


查看完整回答
反对 回复 2021-12-30
  • 1 回答
  • 0 关注
  • 195 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信