Results 1 to 4 of 4
  1. #1
    c_walker is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default null point exception in array lists

    Hi, I am really new in Java. I need help regarding NullPointerExceptions.
    The class compiled but when I riun my program, it says that I have a null point exception. What do you think are my mistakes? Thanks in advance.

    Here are my codes(all from one class):

    Instance fields

    Java Code:
        private double earnings;
        ArrayList<Copier> copierList;
        ArrayList<DepartmentInvoice> departmentList;
        String[] sizeList = {"Letter","Legal","A4","A3"};
        ArrayList<String> deptnames = new ArrayList<String>();
    Constructor:
    Java Code:
    public CopyOffice()
        {
            ArrayList<Copier> copierList = new ArrayList<Copier>();
            ArrayList<DepartmentInvoice> departmentList = new ArrayList<DepartmentInvoice>();
            earnings = 0.0;
        }
    addDepartment method
    Java Code:
     /**
       * The addDepartment method calls the findDept method to check whether a department is already in the ArrayList.
       * If it does not exist, an instance of DepartmentInvoice is created and stored in the ArrayList. 
       * A message is returned to indicate if the department is added or if the department already exists. 
       * 
       * @param  name  the unique name of the department
       * @return the message about the success or failure of adding a Department
       */
      
        public String addDepartment(String name)
        {
            String message="";
            if (deptnames.isEmpty())
            {
                deptnames.add(name);
                [COLOR="Red"]departmentList.add(new DepartmentInvoice(name));[/COLOR]----->[B]null pointer exception: null[/B]
                message = "Department added: " + name;
            }
            else
            {
                if (findDept(name)== null)
                {
                       
                       departmentList.add(new DepartmentInvoice(name));
                       message = "Department added: " + name;
                }               
                else
                    message= "Department already exists: " + name;       
            }
                       
            return message;     
        }
    findDept method
    Java Code:
    /**
      * The findDept method of class DepartmentInvoice returns the DepartmentInvoice record. 
      * The method returns null when no record is found. 
      * 
      * @ param  s   the name of the department being searched 
      * @ return the DepartmentInvoice record being searched
      */ 
     
       public DepartmentInvoice findDept( String s )
       {
           DepartmentInvoice a = null;
           for( int i = 0; i < departmentList.size(); i++ )
           {
               String j = departmentList.get( i ).getName();
               if( j.equalsIgnoreCase( s ))
               {
                   a = departmentList.get( i );
                   break;
               }
           }
           return a;

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem is here:

    Java Code:
    import java.util.ArrayList;
    
    public class Foo2 {
       ArrayList<String> list1;
       ArrayList<String> list2;
       
       public Foo2() {
          ArrayList<String> list1 = new ArrayList<String>();
          list2 = new ArrayList<String>();
       }
       
       public void useList() {
          
          // This works:
          list2.add("new String");
          
          
          // but this doesn't:
          list1.add("new String");  // know why?
       }
    }
    list2 works, but list1 doesn't. Do you see why? The question is what gets initialized in the constructor? The field that was declared in the class at the top, or a new variable that is declared in (and only visible from) the constructor?

    Make sense?

    Oh, and before I forget -- welcome to the forum!

  3. #3
    c_walker is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    It works. Thank you!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 04:25 AM
  3. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 08:22 PM
  4. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 06:56 AM
  5. please i need the code of comparing these two array lists.
    By raj reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 04-18-2008, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •