Results 1 to 4 of 4
- 10-17-2009, 05:02 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
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
Constructor:Java Code:private double earnings; ArrayList<Copier> copierList; ArrayList<DepartmentInvoice> departmentList; String[] sizeList = {"Letter","Legal","A4","A3"}; ArrayList<String> deptnames = new ArrayList<String>();
addDepartment methodJava Code:public CopyOffice() { ArrayList<Copier> copierList = new ArrayList<Copier>(); ArrayList<DepartmentInvoice> departmentList = new ArrayList<DepartmentInvoice>(); earnings = 0.0; }
findDept methodJava 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; }
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;
-
Your problem is here:
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?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? } }
Make sense?
Oh, and before I forget -- welcome to the forum!
- 10-17-2009, 05:24 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
It works. Thank you!
-
Similar Threads
-
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
null pointer exception
By anthonym2121 in forum New To JavaReplies: 7Last Post: 04-06-2009, 03:25 AM -
null pointer exception
By cityguy503@yahoo.com in forum New To JavaReplies: 4Last Post: 08-22-2008, 07:22 PM -
getting a null pointer exception
By Rjava in forum XMLReplies: 4Last Post: 07-16-2008, 05:56 AM -
please i need the code of comparing these two array lists.
By raj reddy in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 04-18-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks