简体   繁体   中英

Getter method returns null. Java MVC

I have googled for a whole day but I just can't find an answer to my problem.

I'm using the MVC pattern. Basically I have a list of songs (id, title, author, ect.) that I need to display in a JTable . The songs are stored in a MySQL database.The Connection to database works fine.

I'm currently storing all the reconds from my database in an 'ArrayList'

The code to store the songs:

SongDAO songDAO = new SongDAO();
ArrayList<Song> songs = songDAO.findAll();

When printing out the songs, I get this output:

[oop.model.entities.Song@1055e4af, oop.model.entities.Song@3caeaf62, oop.model.entities.Song@e6ea0c6, oop.model.entities.Song@6a38e57f]

This is where I play the first song:

if (songs.isEmpty()) {
    library = null; 
} else {
    library = songs.get(0);
    Mainframe.setVisible(true);
    firstController();
}

public static Library getLibrary() {
    return library; 
}

The library static factory method:

public static void firstController() {
    new libraryController(view.libraryView);
}

I'm pretty sure the problem is here. I just can't figure it out for the life of me.

public class libraryController {
   private libraryView view;

   public libraryController(libraryView view) {
       this.view = view;
        for (Song song : baseController.getLibrary().getSongs()) {
            view.addSong(song);
        }
        updateView();
    }

    public void updateView() {
        view.setSongs(baseController.getLibrary().getSongs());
    }
}

I've tried System.out.println(baseController.getLibrary().getSongs()) , but all i get is [ ] . This leads me to believe the getSongs() method is the cause.

public void setSongs(ArrayList<Song> songs) {
    for (Song song : songs) {
        addSong(Song);
    }
}

public void addSong(Song song) {
    Object[] row = new Object[]{song.getId(), song.getTitle(), song.getArtist(), song.getAlbum(), song.getGenre(), song.getYear()};
    model.addRow(row);
}

Here is my Library Class:

public class Library {
    private ArrayList<Song> songs;
    private int id;
    private Song song;

    public Library() {
        id=0;
        songs = new ArrayList<Song>(20);
    }

    public ArrayList<Song> getSongs() {
        return songs;
    }

    public Libreria setSongs(ArrayList<Song> songs) {
        this.songs = songs;
        return this;
    }
}

My song class:

public class Song extends Library {
    private int id;
    private String title;
    private String artist;
    private String album;
    private String genre;
    private int year;
    private Library library;

    public Song(){
        id = 0;
    } 

    public int getId() {return id; }

    public void setId(int id) {this.id = id;}

    public String getTitle() { return title;}

    public void setTitle(String title) {this.title = title;}

    public String getArtist() { return artist;}

    public void setArtist(String artist) {this.artist = artist;}

    public String getAlbum() {return album;}

    public void setAlbum(String album) {this.album = album; }

    public String getGenre() {return genre;}

    public void setGenre(String genre) {this.genre = genre;}

    public int getYear() {return year;}

    public void setYear(int year) {this.year = year;}

    public Library getLibrary() {return library;}

    public Song setLibrary(Library library) {
        this.library = library;
        return this;
    }
}

If anyone could help me, that would be greatly appreciated.

Basically i have to display in JTable my songs ('id','title,'author','..')

Swing components are already designed using a modified version of MVC.

If you want to display data in a JTable you just need to load the data into a TableModel.

So you may want to create a custom TableModel so hold your Song objects.

Check out Table Row Model .

It shows you step by step how to create a custom model for the Song object. Or if you want you can use the generic TableRowModel to make the coding easier.

Once you create the SongTableModel the code to use it would be something like:

SongTableModel model = new SongTableModel( songs );
JTable table = new JTable( model );

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