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;
}
}
あなたは '他の分岐外B'返す入れてもしかして?最初にif-branchが実行されるので、それは 'null'に初期化されます。 – Howard
bが常にnullであるため、StoreMessage.store(...)はnullを返します。申し訳ありませんが、それはひどいコードです - なぜすべての静的な迷惑メール? –
ええと、私は 'b'がnullだと思います。 –