0
    public void rentMovie(){
           System.out.println("ENter a customer ID: ");
           int ID =In.nextInt();
           System.out.print("Enter a movie: ");
           String name=In.nextInt();
           for(Customer customer:customers){
           Optional<Movie> movie = catalogue.getMoviesAvailable().stream().filter(b -> b.getTitle().equals(movieName)).findFirst();
           if(movie.isPresent()) {
              catalogue.getMoviesAvailable().remove(movie.get());
              catalogue.getMoviesRented().add(movie.get());
              customer.getCurrentlyRented().add(movie); 
              customer.getRentingHistory().add(movie.get());

         }
      }}

When I run this code I get a null pointer exception on customer.getCurrentlyRented().add(movie);

and on customer.getRentingHistory().add(movie.get());

Can you tell me how to rectify this problem?

Customer Class

public class customer{
     private List<Movie> currentlyRented;
     private List<Movie> rentingHistory;
   public Customer(){
     }
    }

Catalogue Class

public class Catalogue{
    private List<Movie> moviesAvailable;
    private List<Movie> moviesRented;
      public Catalogue(){
      moviesAvailable.add(new Movie("Matrix",1999,new Genre("SciFi"),3));
      moviesAvailable.add(new Movie("Jurassic Park",1993, new Genre("SciFi"),4));
      }
     }

While renting a movie it should also update the currently renting and renting history also on the rent movie class.

2 Answers2

3

Your list is not initialized.

You should give an empty list inside your constructor

 public Customer(){
   currentlyRented = new ArrayList<>();
 }
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
0

Are you sure you get a Movie from your stream? It might be better to declare a Movie instead of an optional. I would do: Movie movie = catalogue.getMoviesAvailable().stream().filter(b -> b.getTitle()toLowerCase().equals(movieName.toLowerCase())).findFirst(); Note both toLowerCase(). Also is the method isPresent() a custom class? You might want to try if(movie != null)

I hope this works. Else I would use the debugger to see manually if you have a match in your filter

Stan Fieuws
  • 162
  • 1
  • 2
  • 13
  • I used your method I got an error I have to put the optional key word. –  May 06 '18 at 07:01