0

我正在尝试将一分钟内收到的所有消息对象存储到树形图中,一分钟结束后,将其序列化并将字节 [] 返回到另一个类,同时清除地图并开始存储下一分钟收到的消息等上。

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 

即使在进行了修改(即,将 return 放在 else 块中)之后,它也不会将控制权返回给调用类。此外,在 else 块内没有打印任何SOP语句(添加时)。为什么?

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;
        }
    }
4

1 回答 1

0

这是因为你的b变量是null. 看这里:

您输入方法并进行检查if(time1<time2)。这是true. 因此你不会进入 else 也不会初始化 b。之后你去返回b. 它是null。如果你问我,你把退货声明放错了。将 return 放在 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;
      }
 }
 }
于 2012-04-07T12:57:06.633 回答