+ 2
Classes and Objects of Movie[]
So in my Program Design level two class, we're learning about classes and objects for arrays. In this specific assignment, I have to searchByTitle() for a partial search for what the user is typing. For example, the data I'm putting from is called "movielist.txt" with 10,000 movies and I want to have the user input a keyword for the Movie[] movies and result a list of movies with that keyword. Here is the code: https://code.sololearn.com/c1Tqb5A1kOLe My error is NullPointerException and I know what that is it's just how do I fix it? I have a class called Movie which has a string title and genre; and an int year.
1 Answer
+ 5
If you only want to print matching titles then the method need not to return a String, I see you only return a blank String at line 14.
Line 6:
You are concatenating original user input with a lowercase version, did you mean to switch it to lowercase and use it instead? seems more reasonable that way, something like this:
`partial_title = partial_title.toLowerCase();`
Instead of:
`partial_title += partial_title.toLowerCase();`
Line 7:
You are changing the array element into a new (uninitialized) Movie object, which probably has zero length title, this could be the cause of the error. You might wanna mark that line as comment.
// movies[cnt] = new Movie();
Line 10:
You are printing the title with the array index instead of the partial title filter string, if I understand your comment on that line, it should be:
`System.out.println(movies[cnt].title + " " + partial_title);`
Instead of:
`System.out.println(movies[cnt].title + " " + cnt);`
Hth, cmiiw