簡體   English   中英

獲取 java.lang.NullPointerException

[英]Getting java.lang.NullPointerException

我試圖將一分鍾內收到的所有消息對象存儲到一棵樹 map 中,一分鍾結束后,將其序列化並將字節 [] 返回給另一個 class 同時清除 map 並開始存儲下一分鍾收到的消息等等在。

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

            }
         }
    }       

為什么這段代碼給我 null 指針異常突出顯示int len=b.length; 另一個 class 在哪里被調用返回值?

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 塊內)之后,它也不會將控制權返回給調用方 class。此外,在 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;
        }
    }

這是因為你的b變量是null 看這里:

您輸入方法並檢查if(time1<time2) 這是true 因此,您不會將 go 放入 else 中,也不會初始化 b。 之后你 go 並返回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;
      }
 }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM