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");
}
}
java.lang.NullPointerException at CoWorkers.addJob(CoWorkers.java:46) at CoWorkers.main(CoWorkers.java:101)