简体   繁体   中英

Getting java.lang.NullPointerException

I am attempting to store all message objects received in one minute to a tree map and after one mintue is over, serialize it and return the byte[] to another class meanwhile clear the map and start storing the messages recieved in the next min and so on.

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

            }
         }
    }       

Why is this code giving me null pointer exception highlighting int len=b.length; of another class where it is called to return the value?

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 

Even after making the amends (ie placing the return inside the else block), it does not return the control to the calling class. Also, no SOP statement (when added)is printed inside the else block. Why?

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

It is because your b variable is null . See here:

You enter the method and make the check if(time1<time2) . This is true . thus you do not go into the else and do not initialize b. Afterwords you go and return the value of b . It is null . You have placed the return statement wrongly if you ask me. Place the return in the else statement, so you make sure the cycle is terminated and still you will initialize the array before returning:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM