获取NullPointerException,但无法找到源

时间:2014-10-16 18:51:21

标签: java exception mapreduce

我有这段MapReduce代码。它说我的cleanup()中有一个NullPointerException,但我似乎可以弄清楚它在哪里。我甚至添加了一个while循环以避免空值,但这似乎不起作用。这是一个MapReduce工作,但基本原理保持不变。我会描述我的数据,但我不确定它与问题有关。

@SuppressWarnings("deprecation")
public class xvaluesMapper extends Mapper<LongWritable, Text, Text, Text> 
{
public Map<String, Long> snpLocations = new LinkedHashMap<String, Long>();
public Map<Long, String> nList = new LinkedHashMap<Long, String>(); 
public ArrayList<Long> l = new ArrayList<Long>();

public void setup(Context context) throws IOException, InterruptedException 
{
    super.setup(context);
    URI [] SNPLocation =context.getCacheFiles();

    if(SNPLocation.length == 0)
        throw new FileNotFoundException("Distributed Cache file not found.");

    File localFile = new File(SNPLocation[0]);
    FileInputStream f = new FileInputStream(localFile);


    int lineStream;
    while((lineStream = f.read()) != -1)
    {
        String strLine = String.valueOf(lineStream);// line;
        String[] tokens = strLine.toString().split("\\t");
        String SNPID =  tokens[1];
        long location = Long.parseLong(tokens[3]);

        snpLocations.put(SNPID, location);  
    }

    f.close();

    l.addAll(snpLocations.values());




}

public void map(LongWritable key, Text values, Context context)
{
    String [] value = values.toString().split("\\s");
    long position=0;

    for(String s: value)
    {
        ++position;
        nList.put(position, s);
    }
}

public void cleanup(Context context) throws IOException, InterruptedException
{

    while(l != null){
    for (long nPosition: l)

        {
        long actualPos = nPosition+7;
        long secActualPos = actualPos+1;
        //I used 7 because I am assuming the count for the nucleotide position starts from one instead of zero. Also There are 6 other variables like familyID, individual ID....etc
        //in the data set. The "secActualPos" is to account for the second pair.

        String twoSNPphenoTindID = nList.get(actualPos).toString() + "," + nList.get(secActualPos).toString()+  "," + nList.get(5).toString() + "," + nList.get(1);

        for(Entry<String, Long> entryPos: snpLocations.entrySet())
        {
            if(entryPos.getValue().equals(nPosition))


            context.write(new Text(String.valueOf(actualPos)), new Text(twoSNPphenoTindID));
        }
    }

};

}}

1 个答案:

答案 0 :(得分:1)

lArrayList<Long>。这意味着它可以保存具有null值的对象。执行此操作for (long nPosition: l)时,您将迭代所有元素,但它也调用ArrayList中longValue()个对象的方法Long。如果有一个null值的对象,您将拥有NullPointerException。它与此相同。

Long element = null;
long a = element;

这会抛出一个NullPointerException,因为当你尝试将Long指向一个原始的long时,它真的试图这样做

long a = element.longValue()

因为element为null,所以不能引用null对象的方法。

使用for (Long nPosition: l)代替for (long nPosition: l),然后在此循环中检查nPosition null

for (Long nPosition: l)
    {
    if(nPosition == null)
        continue;

    long actualPos = nPosition+7;
    long secActualPos = actualPos+1;
    //I used 7 because I am assuming the count for the nucleotide position starts from one instead of zero. Also There are 6 other variables like familyID, individual ID....etc
    //in the data set. The "secActualPos" is to account for the second pair.

    String twoSNPphenoTindID = nList.get(actualPos).toString() + "," + nList.get(secActualPos).toString()+  "," + nList.get(5).toString() + "," + nList.get(1);

    for(Entry<String, Long> entryPos: snpLocations.entrySet())
    {
        if(entryPos.getValue().equals(nPosition))


        context.write(new Text(String.valueOf(actualPos)), new Text(twoSNPphenoTindID));
    }