Results 1 to 6 of 6
- 03-02-2010, 11:08 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
Arraylist errors with private fields
Hi, I'm doing an assignment, and I noticed I keep getting the same error and I don't know how to fix it. I have a set of code that scans in text. I create an object based on said text. But when I try to add these objects to an arraylist of objects that is a private field of that class, it doesn't work.
put it in below. the errors always occur where i try to add to the arraylist field during the while loop. Thanks
Java Code:public class NameDatabase { // constants public static final int MAX_RANK = 1000; public static final NameRecord NONE = new NameRecord("Not_Found"); // name data private ArrayList<NameRecord> myNames; /** * Construct database from file of formatted data. */ public NameDatabase (String filename) throws FileNotFoundException { // TODO: create myNames and fill it up with data from a file FileReader reader = new FileReader(filename); Scanner in = new Scanner(reader); NameRecord nmrd=new NameRecord(in.nextLine()); System.out.println(nmrd.getName()); while (in.hasNext()){ nmrd= new NameRecord(in.nextLine()); this.myNames.add(nmrd); } } /** * Find a given name within the databasae * * @param name to find * @return data record of matching name or empty record if not found */ public NameRecord match (String name) { NameRecord result = NONE; for (int i=0; i<myNames.size();i++){ if (myNames.get(i).getName().equalsIgnoreCase(name)){ result=myNames.get(i); } } return result; } }
- 03-02-2010, 11:15 AM #2
what error are you getting?
Ramya:cool:
- 03-02-2010, 11:37 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
sorry, the error is
Exception in thread "main" java.lang.NullPointerException
at NameDatabase.<init>(NameDatabase.java:37)
at Main.main(Main.java:26)
here is the main:
Java Code:public class Main { // constants public static final Dimension SIZE = new Dimension(800, 600); public static final String TITLE = "Popular Name Surfer"; public static final String INPUT_PROMPT = "Enter Name to find "; public static final String DATA_FILE = "names-data.txt"; // main --- where the program starts public static void main (String args[]) throws FileNotFoundException { NameDatabase db = new NameDatabase(DATA_FILE); // create container to display animations Canvas display = new Canvas(SIZE); // create user interface controls LabelledInput input = new LabelledInput(INPUT_PROMPT, db, display); // create container that will work with Window manager JFrame frame = new JFrame(TITLE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // add our user interface components to Frame and show it frame.getContentPane().add(display, BorderLayout.CENTER); frame.getContentPane().add(input, BorderLayout.NORTH); frame.pack(); frame.setVisible(true); } }
- 03-02-2010, 11:47 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
You didn't initialize your variabe myNames yet so you probably got a NullPointerException thrown at you.
kind regards,
Jos
- 03-02-2010, 11:54 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
thanks, i got it. but should it be initialized in the fields or in the constructor? im curious to this thanks.
- 03-02-2010, 12:00 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Searching in several fields
By ivaneggel in forum LuceneReplies: 0Last Post: 11-11-2009, 09:24 AM -
Static fields
By Eranga in forum Advanced JavaReplies: 25Last Post: 08-29-2009, 11:06 AM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
Jsp / Java cannot get fields
By Dracos in forum New To JavaReplies: 0Last Post: 02-04-2008, 09:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks