获取显示java.lang.NullPointerException
我试图来存储所有的消息对象接受了一分钟,一棵树地图和之后的一个分钟的车一过,序列化和字节[]返回到另一个阶级同时清除地图,并开始储存在下一分钟收到的消息等等。获取显示java.lang.NullPointerException
public class StoreMessage extends Thread implements Serializable{
public static byte[] b=null;
public static Map <Long,Message> map1=Collections.synchronizedMap(new TreeMap<Long,Message>());
public static Calendar c1=Calendar.getInstance();
public static int year=c1.get(Calendar.YEAR);
public static int month=c1.get(Calendar.MONTH);
public static int day=c1.get(Calendar.DAY_OF_MONTH);
public static int hour=c1.get(Calendar.HOUR_OF_DAY);
public static int min=c1.get(Calendar.MINUTE);
public static GregorianCalendar gc = new GregorianCalendar(year, month, day, hour, min);
public static Date d=gc.getTime();
public static long time1=(d.getTime())/60000; //precision till minute of the time elapsed since 1 Jan 1970
public static long time2=time1+1;
public static byte[] store(Message message)throws Exception{
while(true)
{
if(time1<time2)
{
long preciseTime=TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis())+(System.nanoTime()-startNanotime);
map1.put(preciseTime, message);
}
else
{
b=Serializer.serialize(map1);
map1.clear();
time1=time2;
time2++;
return b;
}
}
}
}
为什么这个代码给我空指针异常突出INT LEN = b.length个;的另一个类,它被称为返回值?
public static void record(Message message){
try{
//storing the serialized message in byte[]
byte[] b =StoreMessage.store(message);
int len=b.length; //<--highlights this line for null pointer exception
即使在进行修改后(即将返回放在else块内),它也不会将控件返回给调用类。另外,没有SOP语句(添加时)打印在else块内部。为什么?
The Serializer class
public class Serializer {
//serializes an object and returns a byte array
public static byte[] serialize(Object map) throws IOException
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(b);
o.writeObject(map);
return b.toByteArray();
}
//de-serialization of the byte array and returns an object
public static Object toObject (byte[] bytes)
{
Object obj = null;
try
{
ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
ObjectInputStream ois = new ObjectInputStream (bis);
obj = ois.readObject();
}
catch (Exception ex) { }
return obj;
}
}
这是因为你的b
变量是null
。在这里看到:
您输入方法,使检查if(time1<time2)
。这是true
。因此你不会进入其他地方,也不要初始化b。 Afterwords后,你将返回b
的值。它是null
。如果你问我,你已经错误地列出了退货声明。放置在else语句返回的,所以你要确保循环被终止,还是你会在返回前初始化数组:
while(true) {
if(time1<time2) {
long preciseTime=TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis())+(System.nanoTime()-startNanotime);
map1.put(preciseTime, message);
} else {
b=Serializer.serialize(map1);
map1.clear();
time1=time2;
time2++;
return b;
}
}
}
,或将它的东西,其中'内b'已知被设置(特别是假设它是'while(true)' - 你的方法永远不会终止)。 (我不知道逻辑的意图是什么。) – 2012-04-07 13:01:20
@HotLicks啊,你是对的。纠正它。谢谢。 – 2012-04-07 13:01:47
如果我把它放在外面while循环..它给了我**无法访问的代码** – kuks 2012-04-07 13:02:09
您的意思是把'的else分支外返回B'?它仍然被初始化为'null',因为首先if-branch将被执行。 – Howard 2012-04-07 12:56:26
您的StoreMessage.store(...)返回null,因为b始终为空。对不起,但它是可怕的代码 - 为什么所有的静态垃圾? – 2012-04-07 12:56:32
呃,我想'b'是空的。 – 2012-04-07 12:56:37