-2

Hi I am trying to make a movie database. Have decided to use arraylist as we cannot use arrays. It will have 4 classes, Movie, MovieDatabase, Playlist and Interface. I want the to add Movie objects from my Movie class into an Arraylist in the Moviedatabase and then be able to add and remove them from the interface. I am getting lots of errors saying it can't find the variables. I used another similar question as a template and can't figure out why it won't compile. Here is my Movie Class

    public class Movie 
 {
  private String name, director;
  private int fileSize, duration;

  public Movie()
  {  
     name = "";
     director = "";
     fileSize = 0;
     duration = 0;


  }   

  Movie(String newName, String newDirector, int newfileSize, int newDuration)
  {   
     this.name = newName;
     this.director = newDirector;
     this.fileSize = newfileSize;
     this.duration = newDuration;

  }   
  public void setName(String newName)
  {
      name = newName;
  }
  public void setDirector(String newDirector)
  {
      director = newDirector;
  }
  public void setfileSize(int newfileSize)
  {
      fileSize = newfileSize;
  }
  public void setDuration(int newDuration)
  {
      duration = newDuration;
  }
  public String getName()
  {
      return name;
  }
  public String getDirector()
  {
      return director;
  } 
  public int getfileSize()
  {
      return fileSize;
  }
  public int getDuration()
  {
      return duration;
  }




}

And the MovieDatabase Class

 import java.util.*;

    public class MovieDatabase
      {   //start class
      private ArrayList<Movie> list = new ArrayList<Movie>(4);   //creates ArrayList
      Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration);   //instantiates Movie class

     public void add(Movie newMovie)   //method adds a new Movie object to the array list
  {   //start add
     list.add(newMovie);

  }   //end add  


    public void add(String newName, String newDirector, int newfileSize, int newDuration)   //accepts parameters from main method to add to new object
  {   //start add
     newName = name;
     newDirector = director;
     newfileSize = fileSize;
     newDuration = duration;

     newMovie.setName(newName);
     newMovie.setDirector(newDirector);
     newMovie.setfileSize(newfileSize);
     newMovie.setDuration(newDuration);

     list.add(newName,newDirector,newfileSize,newDuration);




  }   //end add
 } 

//end class

Edit: Error messages

    MovieDatabase.java:6: error: cannot find symbol
      Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration);   //instantiates Movie class
                                 ^
  symbol:   variable newName
  location: class MovieDatabase
MovieDatabase.java:6: error: cannot find symbol
      Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration);   //instantiates Movie class
                                          ^
  symbol:   variable newDirector
  location: class MovieDatabase
MovieDatabase.java:6: error: cannot find symbol
      Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration);   //instantiates Movie class
                                                       ^
  symbol:   variable newfileSize
  location: class MovieDatabase
MovieDatabase.java:6: error: cannot find symbol
      Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration);   //instantiates Movie class
                                                                    ^
  symbol:   variable newDuration
  location: class MovieDatabase
MovieDatabase.java:17: error: cannot find symbol
     newName = name;
               ^
  symbol:   variable name
  location: class MovieDatabase
MovieDatabase.java:18: error: cannot find symbol
     newDirector = director;
                   ^
  symbol:   variable director
  location: class MovieDatabase
MovieDatabase.java:19: error: cannot find symbol
     newfileSize = fileSize;
                   ^
  symbol:   variable fileSize
  location: class MovieDatabase
MovieDatabase.java:20: error: cannot find symbol
     newDuration = duration;
                   ^
  symbol:   variable duration
  location: class MovieDatabase
MovieDatabase.java:27: error: no suitable method found for add(String,String,int,int)
     list.add(newName,newDirector,newfileSize,newDuration);
         ^
    method Collection.add(Movie) is not applicable
      (actual and formal argument lists differ in length)
    method List.add(Movie) is not applicable
      (actual and formal argument lists differ in length)
    method List.add(int,Movie) is not applicable
      (actual and formal argument lists differ in length)
    method AbstractCollection.add(Movie) is not applicable
      (actual and formal argument lists differ in length)
    method AbstractList.add(Movie) is not applicable
      (actual and formal argument lists differ in length)
    method AbstractList.add(int,Movie) is not applicable
      (actual and formal argument lists differ in length)
    method ArrayList.add(Movie) is not applicable
      (actual and formal argument lists differ in length)
    method ArrayList.add(int,Movie) is not applicable
      (actual and formal argument lists differ in length)
Jack
  • 1
  • 3
  • Edit your question and add some of the error messages. And gibt: run the compiler more often. Don't wait until you wrote 50 lines of code to run the compiler – GhostCat Apr 19 '17 at 03:14
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Apr 19 '17 at 03:15
  • @KenWhite How is it a duplicate? – Jack Apr 19 '17 at 03:31
  • Because it's a duplicate of one of the dozens of *undefined reference/unresolved external symbol error* posts here, and we can't possibly have one for every single project someone on the planet builds that encounters the same issue with different classes. The answer to *In C, how to add the integer variables `a` and `b`?* doesn't change when the variables are named differently, so *In C, how to add the integer variables `c` and `d`?* is a duplicate. The same applies in this case. – Ken White Apr 19 '17 at 12:26

3 Answers3

0

Movie newMovie = new Movie(newName, newDirector, newfileSize, newDuration);

You need to define newName, newDirector, newfileSize, and newDuration, as these variables don't currently exist in MovieDatabase.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
  • But shouldn't it be able to get the variables from the Movie class? – Jack Apr 19 '17 at 03:32
  • @Jack No, you've made them private, and it's not like the MovieDatabase extends the Movie class (which it shouldn't). I recommend you learn about initializing objects and setting their variables. – Jacob G. Apr 19 '17 at 03:33
0

You don't need to specify the length of the array list as 4. The purposes of an ArrayList is to have array like performance with list properties, such as add and remove items by name OR position, size checking, etc.

Also, the method add(Movie newMvoie) adds the movie to the class variable list, but you try create the list inside the class. Realistically you should declare a list member variable without the new constructor.

public class MovieDataBase {
    private ArrayList<Movie> list;
}  

You Can't just write code in a class outside of a method or constructor. You would need to create a MovieDatabase constructor, and possibly have another class to drive the application.

rhysvo
  • 38
  • 6
0

following code is using variables not declared in the MovieDatabase class

public void add(String newName, String newDirector, int newfileSize, int newDuration)   //accepts parameters from main method to add to new object
  {   //start add
     newName = name;
     newDirector = director;
     newfileSize = fileSize;
     newDuration = duration;
     newMovie.setName(newName);
     newMovie.setDirector(newDirector);
     newMovie.setfileSize(newfileSize);
     newMovie.setDuration(newDuration);
     list.add(newName,newDirector,newfileSize,newDuration);
  }    

what you mean is maybe:

public void add(String newName, String newDirector, int newfileSize, int newDuration)   //accepts parameters from main method to add to new object
  {   
     list.add(new Movie(newName,newDirector,newfileSize,newDuration));
  }    
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97