2014-02-18 110 views
0

我试图从HBase表中检索Long值。但是在检索特定的列系列时它会给出NullPointerException。如何读取HBase中的空值java

示例代码段:

long total_size=0; 
total_size +=Bytes.toLong(rr.getValue(Bytes.toBytes("size"),Bytes.toBytes("size")))); 

我也只要存储值的大小。一些大小的vlaue是空的。我想检索空值并将它们分配给“0”ZERO。

请给我适当的代码片段。

谢谢。

回答

0

这是因为Bytes.toLong()方法不检查检索byte[]是否null与否并调用其byte[].length这将引发你的异常。您可以尝试以下解决办法:

String longValue = Bytes.toString(rr.getValue(Bytes.toBytes("size"),Bytes.toBytes("size"))); 
total_size += (longValue != null ? Bytes.toLong(rr.getValue(Bytes.toBytes("size"),Bytes.toBytes("size")):0l); 

try{ 
    total_size +=Bytes.toLong(rr.getValue(Bytes.toBytes("size"),Bytes.toBytes("size")))); 
}catch(NullPointerException e){ 
    // no need to increment total_size by 0 
} 
+0

在第1的方法,给左边的值作为ASCII字符,所以分配到长型的NumberFormat给予例外。第二种方法给予空指针异常。 – srinivasa

+0

你是否从早期的大小列中获得至少一个非空值?还有一件事,当你尝试第二种方法时,你每次都会得到Nullpointer异常? –

+0

在第一种方法中,longValue变量是String,我只是用它来知道'size'列值是否为null。请不要指定它。这就是为什么我检查它不为空,如果是的话,我再次从“大小”列中检索长值。在这种情况下,你得到了NumberForm异常,意味着你没有在“大小”列中保存“长”值。请确保这一点。 –