Java 如何摆脱NullPointerException?

Java 如何摆脱NullPointerException?,java,exception,exception-handling,nullpointerexception,Java,Exception,Exception Handling,Nullpointerexception,似乎无法摆脱NullPointerException错误。已经编写了大约一年了。无论如何,我已经尝试了所有可能的组合!xyz.equals(null)或xyz[0]!=null,但似乎找不到解决方案。我现在已经花了大约两个小时了。非常感谢您的帮助。谢谢 public class PedMapReducer extends Reducer<Text, Text, NullWritable, Text> { Map<String, String> map = new Li

似乎无法摆脱NullPointerException错误。已经编写了大约一年了。无论如何,我已经尝试了所有可能的
组合!xyz.equals(null)
xyz[0]!=null,但似乎找不到解决方案。我现在已经花了大约两个小时了。非常感谢您的帮助。谢谢

public class PedMapReducer extends Reducer<Text, Text, NullWritable, Text> 
{

Map<String, String> map = new LinkedHashMap<String, String>();
Map<String, String> ped = new LinkedHashMap<String, String>();
Set<String> s = new LinkedHashSet<String>();
List<String> arr = new ArrayList<String>();
String joined = null;

public void reduce(Text key, Iterable<Text> values, Context context)
{

    String [] lines = values.toString().split(",");

    if (lines[0] != null && lines[0] == "map_")
    {
        map.put(lines[1], lines.toString());
    }

    else if (lines[0] != null && lines[0] == "ped_")
    {
        ped.put(lines[1], lines.toString());
    }

}

public void cleanup(Context context) throws IOException, InterruptedException
{
    if(!map.entrySet().equals(null) && !ped.entrySet().equals(null))
    {
        for (Entry<String, String> entMap: map.entrySet())
        {
            for(Entry<String, String> entPed: ped.entrySet())
            {
                if(entMap.getKey().equals(entPed.getKey()))
                    joined = entMap.getValue() + "," + entPed.getValue();
            }
        }
        context.write(NullWritable.get(), new Text(joined));
    }


}

}
公共类PedMapReducer扩展了Reducer
{
Map Map=newlinkedhashmap();
Map ped=newlinkedhashmap();
Set s=新的LinkedHashSet();
List arr=new ArrayList();
字符串连接=空;
公共void reduce(文本键、Iterable值、上下文)
{
String[]line=values.toString().split(“,”);
如果(行[0]!=null和行[0]=“映射”)
{
map.put(第[1]行,第.toString()行);
}
如果(第[0]行!=null和第[0]行=“ped_”)
{
ped.put(第[1]行,第.toString()行);
}
}
公共无效清除(上下文上下文上下文)引发IOException、InterruptedException
{
如果(!map.entrySet().equals(null)和&!ped.entrySet().equals(null))
{
对于(条目entMap:map.entrySet())
{
对于(条目entPed:ped.entrySet())
{
if(entMap.getKey().equals(entPed.getKey()))
joined=entMap.getValue()+,“+entPed.getValue();
}
}
write(nullwriteable.get(),新文本(已连接));
}
}
}
堆栈

14/10/20 16:15:03 INFO mapreduce.Job: Task Id : attempt_1413663101908_0026_r_000110_0, Status : FAILED
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 Map_Ped1.PedMapReducer.cleanup(PedMapReducer.java:49)
        at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:179)
        at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:627)
    at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:389)
    at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:168)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:415)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1614)
    at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)
14/10/20 16:15:03信息mapreduce。作业:任务Id:尝试\u 1413663101908\u 0026\u r\u 000110\u 0,状态:失败
错误:java.lang.NullPointerException
位于org.apache.hadoop.io.Text.encode(Text.java:450)
位于org.apache.hadoop.io.Text.set(Text.java:198)
位于org.apache.hadoop.io.Text.(Text.java:88)
位于Map_Ped1.PedMapReducer.cleanup(PedMapReducer.java:49)
位于org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:179)
位于org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:627)
位于org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:389)
位于org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:168)
位于java.security.AccessController.doPrivileged(本机方法)
位于javax.security.auth.Subject.doAs(Subject.java:415)
位于org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1614)
位于org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:163)

检查字符串是否相等时,代码使用==而不是.equals,因此不会将任何条目添加到任何一个集合中。请改用.equals():

因为这些集合(map和ped)是空的,所以joined将为null。新文本(连接)被传递一个null,这可能是null指针异常的来源。文本。暗示新文本()是空指针异常的源:

 at org.apache.hadoop.io.Text.<init>(Text.java:88)
org.apache.hadoop.io.Text.(Text.java:88)

它出现在
cleanup()
哪行代码触发NPE?请显示堆栈跟踪的完整堆栈跟踪。不清楚代码中的哪一行导致NPE。你能指出这条线吗?可能重复:@Multisync,我不知道。我只知道它在清理方法中,你检查过joined是否为null吗?lines数组中的第一个字符串实际上是“map_uu”或“ped_uu”,还是它们只是以这些字符串开头?它们以这些字符串开头。它们只是标识。在这种情况下,您需要使用startsWith()而不是equals()
 at org.apache.hadoop.io.Text.<init>(Text.java:88)