Results 1 to 6 of 6
  1. #1
    DJRome is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

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

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    what error are you getting?
    Ramya:cool:

  3. #3
    DJRome is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    You didn't initialize your variabe myNames yet so you probably got a NullPointerException thrown at you.

    kind regards,

    Jos

  5. #5
    DJRome is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    thanks, i got it. but should it be initialized in the fields or in the constructor? im curious to this thanks.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by DJRome View Post
    thanks, i got it. but should it be initialized in the fields or in the constructor? im curious to this thanks.
    Well, you should give it a value before you try to use it; you're trying to use it in your constructor so why not define and initialize it in one step? Declare it as follows:

    Java Code:
    private List<NameRecord> myNames= new ArrayList<NameRecord>();
    kind regards,

    Jos

Similar Threads

  1. Searching in several fields
    By ivaneggel in forum Lucene
    Replies: 0
    Last Post: 11-11-2009, 09:24 AM
  2. Static fields
    By Eranga in forum Advanced Java
    Replies: 25
    Last Post: 08-29-2009, 11:06 AM
  3. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. Jsp / Java cannot get fields
    By Dracos in forum New To Java
    Replies: 0
    Last Post: 02-04-2008, 09:39 PM

Posting Permissions

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