2 Replies Latest reply: Feb 25, 2009 3:07 AM by 843785 RSS

    Exception in thread "main" Null Pointer exception.

    843785
      Hi
      I am Having a problem creating objects and storing them in an arraylist. I have no problem doing this with regular arrays, but whenever I try to use an arraylist I get the error:
      Thiis is part of my code . any help is appreciated.

      import java.util.*;
      public class MonthlySchedule
      {
           private ArrayList <Request> monthlyschedule;

           Request req = new Request(null, null, null, 0, 0);
           
           
           
           /*
           * Constructor which initialises the arraylist
           */
           public MonthlySchedule()
           {
                ArrayList<Request> monthlyschedule= new ArrayList<Request>() ;
           }
           
           /*Adds a name to the arraylist
           * @param request- the name to be added to the arraylist
           */     
           public boolean add(Request request)
           {     
                int time1=GregorianCalendar.HOUR;
                for(Request request1:monthlyschedule)
                {
                     int time2=GregorianCalendar.HOUR;
                     {
                          if(time1 !=time2)
                          {
                               monthlyschedule.add(request1);
                               return true;
                          }
                          else
                          {
                               return false;
                          }
                     }
                }
                return true;

           }
      /*****************************************************************************/     
           /*
           * The method to cancel a name
           * @param name--the name to be removed from the array list
           */
           
           public boolean cancel (String name)
           {
                for(int i=0;i<monthlyschedule.size();i++)
                {
                     String element =req.getName();
                          if((name)==element)
                          {
                               monthlyschedule.remove(name);
                               return true;
                          }
                          else
                          {
                               return false;
                          }
                }
                     return false;// just to compile
           }

      in the main method(which is not included yet, i'll add it if u request) when i try to add or cancel any request am getting a null pointer exception. can u please help me out. Thanks in advance