0

I have 3 classes for a project that I am working on. The first class I have no issues with. I am deriving a class from this method. I am in turn calling a method from this derived class into my main method. Specifically, I am getting an exception in thread "main", and I cannot invoke my hash map. If someone could help me, I would really appreciate it.

This is the code for the derived class:

import java.util.HashMap;

public class movieSchedule extends movieEvent {

    private HashMap<String, String[]>movieTimes;

    public movieSchedule() {

        this.movieTimes = movieTimes;
    }

    public void AddMovieSchedule(String name, String[] times) {

        movieTimes(name, times);
    }

    private void movieTimes(String name, String[] times) {
        movieTimes.put(name, times);

    }
}

This is the code for the relevant parts of the main method:

movieSchedule athensSchedule = new movieSchedule();
    
String[] guardiansTimes = {"5:30 P.M.", "7:20 P.M.", "9:00 P.M."};
athensSchedule.AddMovieSchedule("Guardians 4", guardiansTimes);
    
String[] airTimes = {"6:30 P.M.", "8:45 P.M.", "9:10 P.M."};
athensSchedule.AddMovieSchedule("AIR", airTimes);
    
String[] marioTimes = {"3:30 P.M.", "4:20 P.M.", "5:00 P.M."};
athensSchedule.AddMovieSchedule("Mario", marioTimes);
    
String[] oppenheimerTimes = {"7:30 P.M.", "9:20 P.M.", "10:00 P.M."};
athensSchedule.AddMovieSchedule("Oppenheimer", oppenheimerTimes);
    
String[] insidiousTimes = {"9:30 P.M.", "10:20 P.M.", "11:00 P.M."};
athensSchedule.AddMovieSchedule("Insidious", insidiousTimes); 

This is the error I am getting:

 Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.HashMap.put(Object,    Object)" because "this.movieTimes" is null
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
  • 4
    `this.movieTimes = movieTimes;` to `this.movieTimes = new HashMap();` – dangerousmanleesanghyeon Apr 20 '23 at 05:13
  • Use different names for your fields and methods. `movieTimes` isn't a good name for something that adds/sets info, it sounds like a "getter". – Mat Apr 20 '23 at 05:20
  • 1
    Either initialize `movieTimes` during declaration with a new HashMap object or add an argument to your constructor as the same type that of `movieTimes`, so that one will be forced to supply the Map when the object is created. – Arun Sudhakaran Apr 20 '23 at 05:23

2 Answers2

0

The problem here is that when you invoke movieSchedule athensSchedule = new movieSchedule();, java goes to the constructor and in the constructor, the hashmap that you have equals itself if you notice, and because you didn't inicialize it, the hashmap is null. So what you need to to do is this.movieTimes = new HashMap<String, String[]>();

Sercos10
  • 3
  • 3
-1

Basically, Variable 'movieTimes' is assigned to itself in the constructor.

So first, as @Arun said, pass hashmap as constructor argument

public MovieSchedule(HashMap<String, String[]> movieTimes) {
        this.movieTimes = movieTimes;
    }
MovieSchedule athensSchedule = new MovieSchedule(new HashMap<>());
String[] guardiansTimes = {"5:30 P.M.", "7:20 P.M.", "9:00 P.M."};
athensSchedule.AddMovieSchedule("Guardians 4", guardiansTimes);

OR

initialise the map at the time of declaration

private HashMap<String, String[]> movieTimes = new HashMap<>();

OR modify the default constructor code and add it as initialization as hashmap

private HashMap<String, String[]> movieTimes ;

public MovieSchedule() {
  this.movieTimes = new HashMap<>();
}

Also, please note, according to naming convention, the first letter of each word should be capitalised check oracle documentation here

Teletubbies
  • 43
  • 10