Java 正在获取NullPointerException,但找不到源
我有这段MapReduce代码。它说我的Java 正在获取NullPointerException,但找不到源,java,exception,mapreduce,Java,Exception,Mapreduce,我有这段MapReduce代码。它说我的cleanup()中有一个NullPointerException,但我似乎能找到它的位置。我甚至添加了一个while循环来避免空值,但这似乎不起作用。这是一项MapReduce工作,但基本原理保持不变。我会描述我的数据,但我不确定它是否与这个问题相关 @SuppressWarnings("deprecation") public class xvaluesMapper extends Mapper<LongWritable, Text, Text,
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));
}
}
};
@SuppressWarnings(“弃用”)
公共类xvaluesMapper扩展映射器
{
public Map snpLocations=new LinkedHashMap();
public Map nList=新建LinkedHashMap();
public ArrayList l=new ArrayList();
公共无效设置(上下文上下文)引发IOException、InterruptedException
{
超级设置(上下文);
URI[]SNPLocation=context.getCacheFiles();
如果(SNPLocation.length==0)
抛出新的FileNotFoundException(“未找到分布式缓存文件”);
File localFile=新文件(SNPLocation[0]);
FileInputStream f=新的FileInputStream(localFile);
int lineStream;
而((lineStream=f.read())!=-1)
{
String strLine=String.valueOf(lineStream);//行;
String[]tokens=strLine.toString().split(\\t”);
字符串SNPID=tokens[1];
long location=long.parseLong(令牌[3]);
snpLocations.put(SNPID,位置);
}
f、 close();
l、 addAll(snpLocations.values());
}
公共void映射(可长写键、文本值、上下文)
{
String[]value=values.toString().split(\\s”);
长位置=0;
for(字符串s:值)
{
++位置;
nList.put(位置s);
}
}
公共无效清除(上下文上下文上下文)引发IOException、InterruptedException
{
while(l!=null){
用于(长位置:l)
{
长实际值=n位置+7;
long secActualPos=实际值+1;
//我使用了7,因为我假设核苷酸位置的计数从1开始,而不是从零开始。还有6个其他变量,如familyID,individual ID…等等
//在数据集中,“secActualPos”用于说明第二对。
字符串twoSNPphenoTindID=nList.get(actualPos.toString()+”,“+nList.get(secActualPos.toString()+”,“+nList.get(5.toString()+”,“+nList.get(1));
对于(条目entryPos:snpLocations.entrySet())
{
if(entryPos.getValue().equals(nPosition))
write(新文本(String.valueOf(actualPos)),新文本(twosnphenotinodid));
}
}
};
}}
l
是一个数组列表
。这意味着它可以保存空值的对象。对(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));
}
for(长位置:l)
{
if(nPosition==null)
继续;
长实际值=n位置+7;
long secActualPos=实际值+1;
//我使用了7,因为我假设核苷酸位置的计数从1开始,而不是从零开始。还有6个其他变量,如familyID,individual ID…等等
//在数据集中,“secActualPos”用于说明第二对。
字符串twoSNPphenoTindID=nList.get(actualPos.toString()+”,“+nList.get(secActualPos.toString()+”,“+nList.get(5.toString()+”,“+nList.get(1));
对于(条目entryPos:snpLocations.entrySet())
{
if(entryPos.getValue().equals(nPosition))
write(新文本(String.valueOf(actualPos)),新文本(twosnphenotinodid));
}
如果您在本网站上对NullPointerException进行了最轻微的搜索,您会发现我们需要查看实际的堆栈跟踪,并确切知道哪一行引发了异常。好吗?