1

when I'm running this part of code I get the IndexOutOfBoundException, because lessons hashmap is empty. I checked it in Debugger. Could you please explain why it is empty? I'm populating it with data, but it doesn't work properly.

public class ScheduleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schedule_activity);

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new SchedulePagerAdapter(getFragmentManager()));

    }

}


class SchedulePagerAdapter extends FragmentPagerAdapter {

    public SchedulePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return DayFragment.newInstance();
    }

    @Override
    public CharSequence getPageTitle(int position) {

        ArrayList<String> arrayList = new ArrayList<>();
        for (String key : LessonsMap.lessons.keySet()) {
            arrayList.add(key);
        }
        return arrayList.get(position);

    }

    @Override
    public int getCount() {
        return 6;
    }

}


class LessonsMap {

    public static Map<String, String> lessons = new HashMap<String, String>();

    public LessonsMap() {

        lessons.put("Monday", "Test_Data");
        lessons.put("Tuesday", "Test_Data");
        lessons.put("Wednesday", "Test_Data");
        lessons.put("Thursday", "Test_Data");
        lessons.put("Friday", "Test_Data");
        lessons.put("Saturday", "Test_Data");


    }

}

Thanks!

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Konstantin
  • 29
  • 7
  • `lessons` is not yet filled with data when you do `LessonsMap.lessons.keySet()`, because it is a static field, and you are populating it in constructor. To make it work, you should initialize `lessons` map in `static` block inside `LessonsMap` class. @ArolaAb answer is also valid. – Michal Fudala Mar 19 '17 at 17:37
  • Side noze: don't use static in the first place. Instantiate real objects;and call methods on those. – GhostCat Mar 19 '17 at 17:42

4 Answers4

1

As You see the exception , the hashmap is empty not null !

Because in your code public static Map<String, String> lessons = new HashMap<String, String>();

Since you are calling the lessons in a static way from the Class LessonMap, the lessons will always be empty because the constructor of your class is never called ! you can make an instance of your LessonMap like this : LessonMap lessonMap = new LessonMap()

The new will make an object of the class and call the constructor.

You cannot make a static constructor in java

ArolaAb
  • 297
  • 3
  • 12
1

Your Constructor in LessonMap class is not running. Because you reach a static variable in class. You must fill the Hashmap in a static initialize block. Try this:

 public static Map<String, String> lessons = new HashMap<String, String>();

static {

lessons.put("Monday", "Test_Data");
lessons.put("Tuesday", "Test_Data");
lessons.put("Wednesday", "Test_Data");
lessons.put("Thursday", "Test_Data");
lessons.put("Friday", "Test_Data");
lessons.put("Saturday", "Test_Data");


}
Mustafa Çil
  • 766
  • 8
  • 15
0

Because you are accessing to the static method without initializing LessonsMap so you never call the block relative to the put of key/values.

You can solve it adding a static init method like this:

public static Map<String, String> lessons = initLessons();

private static Map<String, String> initLessons() {
    Map<String, String> lessons = new HashMap<>();
    lessons.put("Monday", "Test_Data");
    lessons.put("Tuesday", "Test_Data");
    lessons.put("Wednesday", "Test_Data");
    lessons.put("Thursday", "Test_Data");
    lessons.put("Friday", "Test_Data");
    lessons.put("Saturday", "Test_Data");
    return lessons;
}
Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70
0

You are never calling the constructor of LessonsMap which is where you are inserting the data in the map.

You could make lessons not static and create a LessonsMap object like this:

LessonsMap lessonsMap = new LessonsMap();

and then lessons would be filled and accessible using lessonsMap.lessons.

Otherwise, if you want to maintain lessons static you should initialize it using one of the methods discussed here: How can I initialise a static Map?

Community
  • 1
  • 1
Loris Securo
  • 7,538
  • 2
  • 17
  • 28