Object-Oriented Programming Classes | SoloLearn: Learn to code for FREE!


Object-Oriented Programming Classes

Hey there, For this specific lab assignment, I am in the Driver.java removingthe existing code and replace it with a statement that creates a new instance of the MovieContainer class. Then call the newly created display() method. Of course, there are no movies in the container so you will only see a header. In MovieContainer.java I'm heavily modifying the methods especially the one I'm struggling with right now which is the display() method. For this I need to make the display() work for the Driver.java class to make an instance for the number of entries the movies are so that I can test it. I'm getting an error of NullPointerException and I know why but it's confusing to why it's not there. I drew for a reference on what's causing and I'm trying to figure out how to EMPLOY the number of entries into display() for the sake of testing an instance in the Driver class. Here are my codes: MovieContainer: https://code.sololearn.com/cGPEvNU3Zi04 Driver: https://code.sololearn.com/cl0tPkW8GSk2 Movie class: https://code.sololearn.com/cIz7Cg5IKH3v UPDATE: So I tried doing inside the loop for display() method for(){ movies[I] = new Movie("","",0); System.out.printf("%-30s %-20s %4d\n", movies[I].title, movies[I].genre, movies[I].year); } It gave me an OutOfBoundsException error of 10,000 UPDATE#2: I think the problem with my Driver was creating a variable that has null of MovieContainer and I traced it back to the constructor method in MovieContainer.java. I'm getting an error saying it doesn't recognize "FileInputStream" in there even though I have it imported.

2/6/2019 11:42:27 PM

Spartan Noah- 458

7 Answers

New Answer


I'm definitely not a java expert by any means, so I may not have this right. It looks like to me, the way you have these files now, is that you're just trying to display the title, genre, and year for a default Movie instance. In your Movie.java, those variables aren't being defined. In the default Movie constructor you set those variables to the class variables, which aren't defined. In java, is it allowed to say: String title = "default"; String genre = "default"; int year = 0; If that's only allowed if they are static (like c++), then just set them to literal values in the constructor: public Movie(){ title = "default"; genre = "default"; year = 0;// or this. for each of these; I'm not sure how that works for Java } Once again, I could be wrong.


@ZekeWilliams That didn't work. I'm not allowed at the moment to change anything in the Movie class. The error's still NullPointerException and I'm not sure if it's in my display() method or my Driver class of testing the instance of MovieContainer.


Oh okay, so you can only change what's in the Driver or MovieContainer Spartan Noah- 458 ? You have to call the other Movie constructor in your MovieContainer class then. So instead of saying Movie[] movies = new Movie[...], try looping through and constructing each instance using the other Movie constructor: for loop movies[i] = new Movie ("", "", 0);


Spartan Noah- 458 And you should use the toString Movie function in your display function. movies[i].toString();


Zeke Williams I tested what you gave me advice and I got an OutOfBoundsException error. Also, I have another error in the MovieContainer constructor.



Hmmm. Honestly, I would use a vector instead of the default array. That way, you can create an empty container first, without using the default Movie constructor. I can't remember how vectors are used in Java, but I used them in a code I wrote for a contest, just to see if I could write the same code in Java as I did in C++. Take a look at the vector stuff: https://code.sololearn.com/cLQrwu2k48OA/?ref=app Then, in your loop, it would be: movies.add(new Movie("", "", 0)); If you make movies a vector, that is.