Results 1 to 3 of 3
  1. #1
    cataschok is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default problem reading converting file to hash map

    So on my assignment I created a class to read a TXT file into a hashmap the txt contents is like this

    1<>John Kane
    2<>Jane Lyman
    3<>Peter Hansen
    4<>Cathy Harris
    5<>Rose Makki
    6<>Michael O'Connor

    what i have so far
    Java Code:
    public class Reader
    {
    	static Map<Integer, String> instructors, courses;
    	private static final String SEPARATOR = "<>";
    
    	
    	public static Map<String, String> getInstructors()
    	{
    		try
    		{
              BufferedReader in = new BufferedReader( new FileReader("instructor.txt"));
    			 
    			 instructors = new LinkedHashMap<String, String>();
    			 
    			 String line1;
    
    		 	while(((line1 = in.readLine()) != null))
    			{
    				line1 = in.readLine();
    				String[] val1 = line1.split(SEPARATOR);
    				String ID = val1[0];
    				String name = val1[1];
    				
    				instructors.put(ID, name);
    			}
    			in.close();
            }
            catch(IOException ioe)
            {
                ioe.printStackTrace();
            }
    
            return instructors;
    		}
    	public static Map<String, String> getCourses()
    	{
    		try
    		{
              BufferedReader in = new BufferedReader( new FileReader("course.txt"));
    			 
    			 courses = new LinkedHashMap<String, String>();
    			 
    			 String line2;
    
    		 	while(((line2 = in.readLine()) != null))
    			{
    				line2 = in.readLine();
    				String[] val2 = line2.split(SEPARATOR);
    				String ID = val2[0];
    				String name = val2[1];
    				
    				courses.put(ID, name);
    			}
    			
             in.close();
            }
            catch(IOException ioe)
            {
                ioe.printStackTrace();
            }
    		 
           return courses; 
    		}
    }
    I passed the instructor and course hashmap to my mainapp, but two problems occur when i try to display them onto a textarea with only the name, not ID
    my instructor textarea only appeared only 2, 6, and 4,(the actual name), and my courses throws an error refering to line 52 String[] val2 = line2.split(SEPARATOR); throwing an Exception in thread "main" java.lang.NullPointerException

    can anyone help me out on this?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: problem reading converting file to hash map

    Exception in thread "main" java.lang.NullPointerException
    What variable is null on that line? Backtrack in your code to see why that variable does not have a non-null value.

    What do you do with the value of line2 you read from the file at line 46?

    When posting error messages, do not edit off the part with the line number where the error occurred.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

Similar Threads

  1. reading a file problem
    By aianta in forum New To Java
    Replies: 3
    Last Post: 08-03-2011, 07:22 PM
  2. Reading from file problem
    By BillyB in forum New To Java
    Replies: 10
    Last Post: 03-18-2011, 04:09 AM
  3. problem reading file
    By jmoutia in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 03:19 AM
  4. Problem reading from a file
    By sarapeace in forum New To Java
    Replies: 13
    Last Post: 10-03-2010, 02:08 PM
  5. Reg: File Reading Problem
    By balaji csc in forum New To Java
    Replies: 0
    Last Post: 11-06-2009, 04:22 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
  •