2012-04-07 6 views
0

1分以内に受け取ったすべてのメッセージオブジェクトをツリーマップに格納しようとしていますが、1つのミントが終了した後にシリアル化し、別のクラスにバイト[]を戻しながら、メッセージは次の分で受信されました。なぜ、このコードは私にint型のlen =のb.lengthを強調nullポインタ例外を与えている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; 
      } 

      } 
     } 
    }  

。他のクラスのは値を返すために呼び出されますか?

補正を行った後(すなわち、戻り値を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; 
     } 
    } 
+0

あなたは '他の分岐外B'返す入れてもしかして?最初にif-branchが実行されるので、それは 'null'に初期化されます。 – Howard

+0

bが常にnullであるため、StoreMessage.store(...)はnullを返します。申し訳ありませんが、それはひどいコードです - なぜすべての静的な迷惑メール? –

+0

ええと、私は 'b'がnullだと思います。 –

答えて

0

あなたb変数がnullからです。ここをクリックしてください:

お客様は、この方法を入力してチェックを行います。if(time1<time2)。これはtrueです。したがって、あなたは他者に行かず、初期化しないでください。その後、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; 
     } 
} 
} 
+1

またはelseの中に配置します。 bが設定​​されていることが分かっています(特にwhile(true) - あなたのアプローチは決して終了しません)。 (私は論理の意図が何であるか分かりません) –

+0

@HotLicksああ、そうです。それを訂正する。ありがとうございました。 –

+0

cycle.itが私に到達できないコード** – kuks

関連する問題