2

I have a code to in controller and I am trying to get the value from the map. Here is the code

public Map<String, Integer> daysOfTheWeekMap = new Map<String, Integer>();
daysOfTheWeekMap=assignDaysOfWeekMap();
if (daysOfTheWeekMap.get(dayOfWeek) - daysOfTheWeekMap.get(recurringTaskLPP.daysOfWeek[i]) <= 0) {
                days.add(startDate);
                startDate = startDate + (this.daysOfTheWeekMap.get(dayOfWeek) - this.daysOfTheWeekMap.get(recurringTaskLPP.daysOfWeek[i]));
            }
 public Map<String, Integer> assignDaysOfWeekMap() {
        Map<String,Integer> weekMap = new Map<String, Integer>();
        this.daysOfTheWeekMap.put('Sunday',0);
        this.daysOfTheWeekMap.put('Monday',1);
        this.daysOfTheWeekMap.put('Tuesday',2);
        this.daysOfTheWeekMap.put('Wednesday',3);
        this.daysOfTheWeekMap.put('Thursday',4);
        this.daysOfTheWeekMap.put('Friday',5);
        this.daysOfTheWeekMap.put('Saturday',6);
        return weekMap;
    }

Whenever i try and run I get the error in the If statement that I am trying to dereference the null object. The problem is that when dayOfWeek is passed in the map to get the value the map is returning null. When I log dayOfWeek it shows the correct value which means that the problem is in the get from the map. Can anyone solve and tell me why is it giving a null pointer exception in get from the map?? Thanks

4
  • Whats in recurringTaskLPP? Jul 1, 2016 at 18:57
  • It's highly unlikely that the .get() method is at fault. More likely is that your recurringTaskLPP.daysOfWeek[i] is returning a value not in your map's keyset. Have you tried system.debug on that part of your expression?
    – Derek F
    Jul 1, 2016 at 18:57
  • Yes I tried to do the system .debug actually [i] has an array of string from the other class which also only has weekdays so it is not an issue. The problem is that get method is not returning the correct value. I am passing the key correctly that I checked in the logs Jul 1, 2016 at 18:59
  • @Abhinav Pandey, please refer my answer Jul 1, 2016 at 19:07

2 Answers 2

3

Just a note that you can get the Day Of Week value by using Datetime.format('u'). It's not in the SimpleDateFormat docs, but it works. It's probably missing because it was added in Java 7 and they're outdated. Anyway, try running this snippet in Execute Anonymous:

static Integer getDayOfWeek(Datetime input)
{
    return Math.mod(Integer.valueOf(input.format('u')), 7);
}
static void debugFormat(Datetime input)
{
    system.debug(input.format('EEEE') + ' - ' + getDayOfWeek(input));
}

Datetime sunday = Datetime.newInstanceGmt(
    Date.newInstance(2016, 6, 26), Time.newInstance(12, 0, 0, 0)
);
debugFormat(sunday);
debugFormat(sunday.addDays(1));
debugFormat(sunday.addDays(2));
debugFormat(sunday.addDays(3));
debugFormat(sunday.addDays(4));
debugFormat(sunday.addDays(5));
debugFormat(sunday.addDays(6));

Output:

Sunday - 0
Monday - 1
Tuesday - 2
Wednesday - 3
Thursday - 4
Friday - 5
Saturday - 6

3
1

In this method assignDaysOfWeekMap() replace daysOfTheWeekMap with weekMap as you are returning weekMap from this method which is empty. Updated method will be as follows:

public Map<String, Integer> assignDaysOfWeekMap() {
    Map<String,Integer> weekMap = new Map<String, Integer>();
    weekMap.put('Sunday',0);
    weekMap.put('Monday',1);
    weekMap.put('Tuesday',2);
    weekMap.put('Wednesday',3);
    weekMap.put('Thursday',4);
    weekMap.put('Friday',5);
    weekMap.put('Saturday',6);
    return weekMap;
} 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .