I keep getting Java null point exception for makeMovielist
Here is the code I have done so far. I keep getting Java null point exception for makeMovielist
code:
import java.util.ArrayList;
import java.util.Arrays;
public class MovieList {
/**
* This method will take the name of a movie and delete it
* in the movies if exists and
* delete its corresponding year in years
*
* @param name movie name to delete
* @param movies List of all movies' name
* @param years List of corresponding years
*/
public static void deleteMovieByName(String name, ArrayList<String> movies,
ArrayList<Integer> years) {
if(name==null || movies == null || years == null)
return;
for(int i=0; i<movies.size(); i++){
if(movies.get(i).equals(name)){
movies.remove(i);
years.remove(i);
}
}
}
/**
* This method will delete all movies with specific year
* in the movie list and year list
*
* @param year the given year
* @param movies List of all movies' name
* @param years List of corresponding years
*/
public static void deleteMoviesByYear(int year, ArrayList<String> movies,
ArrayList<Integer> years) {
if(movies == null || years == null)
return;
for(int i=0; i<years.size(); i++){
if(years.get(i)==year){
movies.remove(i);
years.remove(i);
}
}
}
/**
* This method will return a string that contains each
* movie name and its year.
* Each line will format as follow:
* <movie name> (<year>)\n
*
* @param movies List of all movies' name
* @param years List of corresponding years
* @return string contains all movie names and years
*/
public static String makeMovieList(ArrayList<String> movies,
ArrayList<Integer> years) {
String result = "";
for(int i=0; i<movies.size(); i++){
result += movies.get(i)+" ("+years.get(i)+")\n";
}
return result;
}
/**
* test for deleteMovieByName()
* @param movies
* @param years
*/
public static void testDeleteByName(ArrayList<String> movies,
ArrayList<Integer> years) {
boolean error = false;
deleteMovieByName("Fight Club", movies, years);
ArrayList<String> expectedMovie1 = new ArrayList<>(
Arrays.asList("The Shawshank Redemption","The Godfather",
"The Godfather: Part II", "The Dark Knight",
"12 Angry Men", "Schindler's List",
"The Lord of the Rings: The Return of the King",
"Pulp Fiction", "The Good, the Bad and the Ugly"
));
ArrayList<Integer> expectedYears1 = new ArrayList<>(
Arrays.asList(1994, 1972, 1974, 2008, 1957,
1993, 2003, 1994, 1966));
if(!movies.equals(expectedMovie1) || !years.equals(expectedYears1)){
error = true;
System.out.println("Expected movie list: " + expectedMovie1);
System.out.println("Actual movie list: " + movies);
System.out.println("Expected movie list: " + expectedYears1);
System.out.println("Actual movie list: " + years);
}
//add more test cases
if(error){
System.out.println("testDeleteByName fail");
}else{
System.out.println("testDeleteByName pass");
}
}
/**
* test for deleteMoviesByYear()
* @param movies
* @param years
*/
public static void testDeleteByYear(ArrayList<String> movies,
ArrayList<Integer> years) {
}
/**
* test for makeMovieList()
* @param movies
* @param years
*/
public static void testMakeMovieList(ArrayList<String> movies,
ArrayList<Integer> years) {
}
public static void main(String[] args) {
ArrayList<String> movies = new ArrayList<>(
Arrays.asList("The Shawshank Redemption","The Godfather",
"The Godfather: Part II", "The Dark Knight",
"12 Angry Men", "Schindler's List",
"The Lord of the Rings: The Return of the King",
"Pulp Fiction", "The Good, the Bad and the Ugly",
"Fight Club"
)
);
ArrayList<Integer> years = new ArrayList<>(
Arrays.asList(1994, 1972, 1974, 2008, 1957,
1993, 2003, 1994, 1966, 1999));
testDeleteByName(movies, years);
//testDeleteByYear(movies, years);
//testMakeMovieList(movies, years);
}
}
![9.9 **zyLab: MovieList
Write this program using Eclipse or IntelliJ. Comment and style the code according to CS 200 Style Guide. Submit the source code files
(java) below. Make sure your source files are encoded in UTF-8.
The goal of this problem is to get familiar with ArrayLists. As opposed to an array, an ArrayList is dynamic; when you delete an element in
the ArrayList, the size of the ArrayList will change.
In this lab, you will create methods that simultaneously alter two parallel ArrayLists. One String ArrayList saves the name of the movie, the
other Integer ArrayList saves the corresponding year of the movie. You will implement three methods: deleteMovieByName(),
deleteMoviesByYear(), and makeMovieList(). A downloadable file named MovieList.java is available to help you get started.
1. Implement the deleteMovieByName(String name, ArrayList<String> movies, ArrayList<Integer> years)
method:
o If the given string name of the movie exists, delete the movie in the ArrayList<String> movies and the corresponding
year in the ArrayList<Integer> years
O ArrayList and String are reference types, so the method should check whether they are null first. If any of the ArrayList is
null, the method should return immediately.
/**
This method will take the name of a movie and delete it
* in the movies if exists and
delete its corresponding year in years
* @param name movie name to delete
@param movies List of all movies' name
* @param years List of corresponding years
*/
public static void deleteMovieByName (String name, ArrayList<String> movies,](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fa72dae9f-0052-41b5-80f3-bd0e25abf3cd%2F2ad37006-067e-4338-b39a-36b8f0036f42%2Fbwizdl_processed.png&w=3840&q=75)
![1: File Header Test a
1/1
2: Unit Test: deleteMovieByName() ^
3/3
3: Unit Test: deleteMovieByYear() ^
3/3
4: Unit Test: makeMovieList() ^
0/3
Test feedback
java.lang.NullPointerException](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fa72dae9f-0052-41b5-80f3-bd0e25abf3cd%2F2ad37006-067e-4338-b39a-36b8f0036f42%2Faf5572_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)