简体   繁体   中英

Java Object Array printing null?

I have 3 classes, Movie which is used to add Movie objects to an in MovieDatabase but it keeps printing null.

When I add 2 Movies its like the first Movie is erased and it prints null instead. Also is there a way to check if the position in the array is empty and not print if it is empty?

Here is my Movie class

public class Movie {

    private String name;  
    private String director;
    private double fileSize;               
    private int duration;
    private int moviecount;

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

    public void setName(String newName)
    {
        name = newName;
    }

    public String getName()
    {
        return name;
    }

    public void setDirector(String newDirector)
    {
        director = newDirector;
    }

    public String getDirector()
    {
        return director;
    }

    public void setfileSize(double newfileSize)
    {
        fileSize = newfileSize;
    }

    public double getfileSize()
    {
        return fileSize;
    }

    public void setDuration(int newDuration)
    {
        duration = newDuration;
    }

    public int getDuration()
    {
        return duration;
    }

and here my MovieDatabase class:

public class MovieDatabase
{
    private Movie[] mov;
    private int i;
    public int count=0;

    public MovieDatabase()
    {
        mov = new Movie[4];
        i=0;
    }

    public void addData(String name, String director, double fileSize, int duration)
    {
        for(int i=0; i<4; i++)
        mov[i] = new Movie();
        setData(mov[i],name,director,fileSize,duration);
        i++;
        count++;
    }
    private void setData(Movie m,String name, String director, double fileSize, int duration)
    {
            mov[i].setName(name);
            mov[i].setDirector(director);
            mov[i].setfileSize(fileSize);
            mov[i].setDuration(duration);
    }
    public void printNames()
    {
        for (int i = 0; i < mov.length; i++) 
        {
        System.out.println(mov[i].getName());
        }

    }
}





import java.util.*;
public class Interface {
Scanner console = new Scanner(System.in);
MovieDatabase m = new MovieDatabase();
private void run() 
{

int option;



    do{
        System.out.print("Add Movie(0), Delete Movie(2),Show Movies(3),Movie Count(4) \n");
        option = console.nextInt();

        switch(option)
        {
            case 0: addMovie();
                    break;

            case 3: printMovies();
                    break;



        }
    }        

        while(option!=9);

}
public static void main(String[] args){
 Interface intFace = new Interface();
intFace.run();
}

public void addMovie()
{
    String name, director;
    double fileSize; 
    int duration;

    System.out.println("Movie Name: ");
    name = console.next();
    System.out.println("Movie Director: ");
    director = console.next();
    System.out.println("Movie File Size: ");
    fileSize = console.nextDouble();
    System.out.println("Movie Duration: ");
    duration = console.nextInt();
    System.out.print("Movie Added!");


    m.addData(name,director,fileSize,duration);
}
public void printMovies()
{
    m.printNames();
}

}

I tried to include only the relevant parts but majority of what I have done so far is relevant.

The problem is in these lines

....
public void addData(String name, String director, double fileSize, int duration)
{
    for(int i=0; i<4; i++)
    mov[i] = new Movie();
    ...

Each and every time you're adding a new data, you're erasing all previous records by assigning new movie object to every element in array. This erases all previous data.

You should instead move these 2 lines in MovieDatabase constructor. Or a better option would be to initialize them when you're setting data.

...
public void addData(String name, String director, double fileSize, int duration)
{
    setData(mov[i],name,director,fileSize,duration);
    i++;
    count++;
}
private void setData(Movie m,String name, String director, double fileSize, int duration)
{ 
        mov[i] = new Movie(); //++ edit
        mov[i].setName(name);
        mov[i].setDirector(director);
        mov[i].setfileSize(fileSize);
        mov[i].setDuration(duration);
}
...

Also is there a way to check if the position in the array is empty and not print if it is empty?

You can create a method in Movie class which checks whether this movie object is empty or not and returns appropriate result.

public class Movie {
    ...
    ...
    public boolean isEmpty() {
        if(
            this.name.isEmpty() &&  
            this.director &&
            this.fileSize == 0 &&
            this.duration == 0 &&
            this.moviecount == 0
        )
            return true;
        else
            return false;
    }
    ...
    ...
}

Now you can check whether this movie object is empty or not using:

if(mov[i].isEmpty()) {
    //empty movie object
    ...
}

In setData you always set the value of mov[0] . The class member i will never change (loop variable hides it). You do not use the parameter m to set the data.

Change your setData to

m.setName(name);
m.setDirector(director);
m.setfileSize(fileSize);
m.setDuration(duration);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM