So am doing a mail server kind of program for my java course, (I would call myself a beginner in Java) and while I have completed all the required parts, there are some problems with the program that while the assignment doesn't require it, I would like to know why this is behaving the way and how to fix it. So to summarize it I have two methods that check how many mail items a specific user has and another one that returns the mail item that is first in the users mailbox, (by LIFO rule).
So while if a user both A.Has a mailbox and B.Has at least 1 mail in said mailbox there isn't a problem, this doesn't really work when a and/or b isn't true. And that is what I tried to fix. I thought it would be as simple as checking both if the mailbox.containsKey(who)==false and/or mailbox.get(who).get(0) == null would be enough but when I do this i get java.lang.IndexOutOfBoundsException: Index: 0, Size: 0.
I've tried multiple types of either checking if it's ==true/false null etc.. I've tried to just throw the exception all together but nothing seems to work and while maybe throwing the exception could work in some way I would want to solve it in another way as the throw command seems a bit lazy
Here is my method in which I check both A and B:
private boolean errorHandling(String who){
if (mailbox.containsKey(who)==false || mailbox.get(who).get(0) == null ){
return true;
}
else{
return false;
}
}
and this method is utilized by these two methods:
public MailItem getNextMailItem(String who)
{
if (!errorHandling(who) ){
MailItem item2 = mailbox.get(who).get(0);
mailbox.get(who).remove(0);
return item2;
} else{
System.out.println("that user dosent have a mailbox");
return null;
}
}
public int howManyMailItems(String who)
{
if (!errorHandling(who) ){ //To check if user has a mailbox
return mailbox.get(who).size();
}
else{
System.out.println("that user dosent have a mailbox");
return 0;
}
}
I am not sure if you need more of my code to help me or not. Tell me if that is the case but to clarify, the "mailbox" is the hashmap and the who parameter is the person you want to check his or her mailbox. Thank you.
MailItem mail = mailbox.get(who);, followed byif (mail != null). That's in general, for the specific case of #howManyMailItems:mailbox.getOrDefault(who, 0);will return 0 if there isn't a key mapped to a value. You can also make use of Map#computeIfAbsent so that you can always retrieve aMailboxand the values are nevernull.mailbox.get(who).get(0) == nullwould only be true if you addnullto the mail list and I hope you don't do that. I guess you have a LinkedList there, so you can check the JavaDoc if that class provides something for you to check if the list is empty or contains something: docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html (also check the inherited methods).List#isEmptyis useful here. Lists/Sets don't just returnnullfor a non-existant value, you need to check that the index you request (0in this case) is actually contained in the collection by comparing#sizeto the index you want.!mailbox.containsKey.