2014-10-20 3 views
1

Cant가 NullPointerException 오류를 떨쳐 버리는 것처럼 보입니다. 지금 약 1 년 동안 코딩하고 있습니다. 어쨌든 !xyz.equals(null) 또는 xyz[0] != null의 모든 가능한 조합을 시도했지만 해결책을 찾을 수 없습니다. 나는 지금 약 2 시간을 보냈다. 도움을 많이 주시면 감사하겠습니다. 감사.NullPointerException을 어떻게 제거합니까?

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)); 
    } 


} 

} 

스택

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) 
+0

는'정리()' –

+1

코드의 라인 NPE를 트리거에 나타납니다 < 초기화 > 새로운 텍스트() 널 포인터 예외의 원인 의미? 전체 스택 추적 표시하기 – Lolo

+1

스택 트레이스에서 코드에서 NPE를 일으키는 라인이 명확하지 않습니다. 이 라인을 가리킬 수 있습니까? – Multisync

답변

2

코드 대신 .equals의 == 사용하고, 그래서 항목 이제까지 중 하나를 컬렉션에 추가되지 않습니다 문자열 어떤지를 검사합니다. .equals()를 대신 사용하십시오 :

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

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

이러한 컬렉션 (지도 및 ped)은 비어 있기 때문에 join이 null이됩니다. new Text (joined)가 널 포인터 예외의 소스 일 수있는 널 (null)을 전달 중입니다. 본문.

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

나는 변화를 만들었지 만 그것은 작동하지 않았다. 여전히 동일한 오류 –

+0

가입 여부가 null인지 확인 했습니까? 라인 배열의 첫 번째 문자열은 실제로 "map_"또는 "ped_"입니까? 아니면 해당 문자열로 시작합니까? –

+0

해당 문자열로 시작합니다. 그들은 단순히 식별자입니다 –

관련 문제