0

I'm making a Project manager program (a very simple one). Basically there are CoWorkers and their tasks. Now, I made the following structure: I'm using the HashMap keys as the name of the CoWorkers and the value is always the CoWorkers ArryList which contains his or her jobs of course.

My issue is I can create this structure but when I try to add a task one of the ArrayLists I always getting null as a return value.

Here are my code snippets:

HashMap<String, ArrayList<String>> jobs = new HashMap<String, ArrayList<String>>();

public void createJobs(){
    nameReader();
    for(String name:names){
        File f = new File(name+"_job.txt");
        if(f.exists()){
            jobs.put(name, jobReader(name));
        }
    }
    Set set = jobs.entrySet(); 
    Iterator i = set.iterator(); 
    while(i.hasNext()) { 
        Map.Entry me = (Map.Entry)i.next(); 
        System.out.print(me.getKey() + ": "); 
        System.out.println(me.getValue()); 
    } 
}

After this method on the console i see the following result:

George: [do the laundry]

So i assume that i did everything all right.

But then:

public void addJob(String name, String job){
    try{
        List<String> itemsList = jobs.get(name);
    itemsList.add(job);
    }catch(Exception ex){
         System.err.println(ex.getMessage() + jobs.get(name) +name);
    }
}

and I'm calling this method:

addjob("George","Clean up your room!");

the result on the console will be:

nullnullGeorge

UPDATE

public ArrayList jobReader(String name){
    ArrayList<String> tasks = new ArrayList<String>();
    try{
        Scanner s = new Scanner(new File(name+"_job.txt"));
        while (s.hasNext()){
            tasks.add(s.next());
        }
        s.close();
    }catch(IOException ioe){
        System.err.println("IOException: " + ioe.getMessage());
    }
    return tasks;
}

UPDATE 2

This is my hole code:

public class CoWorkers {
    HashMap<String, ArrayList<String>> jobs = new HashMap<String, ArrayList<String>>();
    ArrayList<String> names = new ArrayList<String>();

    public void createJobs(){
        nameReader();
        for(String name:names){
            File f = new File(name+"_job.txt");
            if(f.exists()){
                jobs.put(name, jobReader(name));
            }
        }
        Set set = jobs.entrySet(); 
        Iterator i = set.iterator(); 
        while(i.hasNext()) { 
            Map.Entry me = (Map.Entry)i.next(); 
            System.out.print(me.getKey() + ": "); 
            System.out.println(me.getValue()); 
        } 
    }

    public void addName(String name){
        try{
            String filename= "Names.txt";
            FileWriter fw = new FileWriter(filename,true); 
            fw.write(name+"\n");
            fw.close();
        }
        catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
    }

    public void addJob(String name, String job){
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        try{
            List<String> itemsList = jobs.get(name);
            itemsList.add(job);
        }catch(Exception ex){
             ex.printStackTrace();
        }
    }

    public ArrayList jobReader(String name){
        ArrayList<String> tasks = new ArrayList<String>();
        try{
            Scanner s = new Scanner(new File(name+"_job.txt"));
            while (s.hasNext()){
                tasks.add(s.next());
            }
            s.close();
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
        return tasks;
    }

    public void nameReader(){
        names.clear();
        try{
            Scanner s = new Scanner(new File("Names.txt"));
            while (s.hasNext()){
                names.add(s.next());
            }
            s.close();
        }catch(IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
    }
    public static void main(String[] args) {
        CoWorkers project = new CoWorkers();
        project.createJobs();
        project.addJob("George","Do the laundry");
    }
}
18
  • Where are your methods being called from? Commented Apr 16, 2013 at 17:26
  • can you print the contents of jobs in the addJob method and verify that the contents still are there when you are adding a new job? Commented Apr 16, 2013 at 17:26
  • Also, instead of printing ex.getMessage(), call ex.printStackTrace(). It's often pretty helpful Commented Apr 16, 2013 at 17:29
  • Thank you for your comments i tried ex.printStackTrace() and this is what i get: java.lang.NullPointerException at CoWorkers.addJob(CoWorkers.java:46) at CoWorkers.main(CoWorkers.java:101) Commented Apr 16, 2013 at 17:34
  • Is addJob() called after createJobs() in the same execution? Commented Apr 16, 2013 at 17:35

3 Answers 3

1

So I created my own little test class and proved that your code works.

I don't think you're showing us everything here.

I think somewhere you have two definitions of the variable "jobs", one is hiding the other.

------updated------

OK, so I run your code with hard-coded names and jobs and it works just fine. My suggestion: try doing the same.

       String names[] = {"George", "Bill"};
        for(String name:names){          
             jobs.put(name, jobReader(name));              
        }

       public ArrayList jobReader(String name){
        ArrayList<String> tasks = new ArrayList<String>();
                tasks.add("Tidy");
            return tasks;
    }

I suspect that what you read from the File is not matching "George", even though we can't as yet see why. Once we have confidence in the list handling then we can think about the data.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you i'm trying this now.
So the issue is with the files then? How should i clean the read lines?
In a debugger compare what you are reading with what you expect. Do you have a line termination character perhaps? Tell us what you get and we can figure it out.
A whitespace after the name... Thank you for your help really appreciated :)
1

Looking upon the code ex.getMessage() + jobs.get(name) +name , u must be getting null pointer exception as your console prints nullnullGeorge It means List itemsList = jobs.get(name);//This is null and hence is the list . Adding null to List will give u null pointer Exception.That means while adding George in Map , didn't happen.

1 Comment

Thank you very much for your help. I assume you are right but where is the issue then?
1

In general, it's a good idea to check if your map contains a key yet if you're going to be manipulating the value. For example:

public void addJob(String name, String job){

    if (!jobs.containsKey(name)) {
        jobs.put(name, new ArrayList<String>());
    }
    List<String> itemsList = jobs.get(name);
    itemsList.add(job);
}

One other issue you have is that you fail silently when you can't find the file with someone's jobs. Consider adding this into createJobs()

    for(String name:names){
        File f = new File(name+"_job.txt");
        if(f.exists()){
            jobs.put(name, jobReader(name));
        } else { // This is new
            jobs.put(name, new ArrayList<String>());
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.