I keep getting Java null point exception for makeMovielist

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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,
Transcribed Image Text: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,
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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education