我有这段MapReduce代码。它说我的cleanup()
中有一个NullPointerException,但我似乎能弄清楚它在哪里。我甚至添加了一个while循环来避免null值,但这似乎不起作用。这是一项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));
}
}
};
}}
l
是ArrayList<Long>
。这意味着它可以容纳具有null值的对象。当您执行for (long nPosition: l)
时,您正在对所有元素进行迭代,但它也在调用ArrayList中Long
对象的方法longValue()
。如果有一个对象的值为null
,则会有一个NullPointerException
。和这个是一样的。
Long element = null;
long a = element;
这抛出了一个NullPointerException
,因为当你试图将Long赋给一个基元Long时,它实际上是在尝试执行这个
long a = element.longValue()
因为元素是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));
}