获取java.lang.NullPointerException

时间:2012-04-07 12:52:39

标签: java treemap

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

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块中)之后,它也不会将控件返回给调用类。此外,在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;
        }
    }

1 个答案:

答案 0 :(得分:0)

这是因为您的b变量为null。见这里:

您输入方法并进行检查if(time1<time2)。这是true。因此你不进入其他并且不初始化b。之后你会去返回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;
      }
 }
 }