Reputation: 1049
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");
}
}
Upvotes: 0
Views: 3370
Reputation: 55957
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.
Upvotes: 1
Reputation: 7146
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>());
}
}
Upvotes: 1
Reputation: 57
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.
Upvotes: 1