0

The problem with my program is that once the "getMP3Tags" method is removed from the main class which is called ""musicManagerUI", and placed within it's own class, the method does not send anything over to the appropriate methods in the "musicManagerUI" class. However, if the "getMP3Tags" method is placed within the "musicManagerUI" class then everything works perfectly. I want to know why this happens and if anyone can help me to resolve this issue, as I don't want to cluster up the "musicManagerUI" class with loads of methods.

Here is the constructor for the main class "musicManagerUI":

public musicManagerUI() {

    super ("Music Manager");
    initComponents();

    handlerClass handler = new handlerClass();

jMenuItem5.addMouseListener(handler);
}

Here is one the four methods which prints out data to the jTable:

public void printArtistName(ArrayList arrayOfArtistNames) 
{
DefaultTableModel model = (DefaultTableModel) jTable2.getModel();


if (arrayOfArtistNames == null) {model.setValueAt("No Artist Names Found", 0, 0);}


else {   
Object [] rowData;
rowData = new Object[3];
for (Object x : arrayOfArtistNames)
  {


  model.addRow(rowData);


  model.setValueAt(x, counterArtist, 0);
  counterArtist++; 
  }

 }

 }

Here is the code for the mousepressed handler method:

public void mousePressed(MouseEvent e) {

DefaultTableModel model = (DefaultTableModel) jTable2.getModel();

if (counterArtist == 0){}
else if (counterArtist > 0) {

   model.setRowCount(0);

 counterArtist = 0; counterTitle = 0; counterAlbum = 0; genre = 0;}




 String FILE_DIR = "C:\\Users\\Hazzy\\Desktop\\test-data\\";
 String FILE_TEXT_EXT = ".mp3";


 findCertainExtension fw = new findCertainExtension();
 try {
    fw.getMP3Tags(FILE_DIR);
  } catch (IOException ex) {
    System.err.println(" Could Not read file in ");
  } catch (TagException ex) {

    System.err.println(" Could Not find tag");
  }
  }

And finally the "getMP3Tags" method:

public void getMP3Tags( String path ) throws IOException, TagException,             
try{
    File root = new File( path );
    File[] list = root.listFiles();

ArrayList addArtistTitle = new ArrayList();
ArrayList addTrackTitle = new ArrayList();
ArrayList addAlbumTitle = new ArrayList();
ArrayList addGenre = new ArrayList();



    if (list == null) return;

    for ( File f : list ) {
        if ( f.isDirectory()) {
            getMP3Tags( f.getAbsolutePath() );



        }


        else if (!f.getAbsoluteFile().toString().toLowerCase().endsWith("mp3"))

        {
        getMP3Tags(f.getAbsolutePath());
        }



        else{

       musicManagerUI setFields = new musicManagerUI ();
        //System.out.println(f.getAbsoluteFile());  


        for (Object x : list) {}
        MP3File mp3file = new MP3File(f.getAbsoluteFile());
      ID3v2_2 name = (ID3v2_2) mp3file.getID3v2Tag();


      if (name.getLeadArtist().isEmpty() == true){ name.setLeadArtist("Unknown");}
      else if (name.getSongTitle().isEmpty() == true) {name.setSongTitle("Unknown");}
      else if (name.getSongGenre().isEmpty() == true) {name.setSongGenre("Unknown");}
      else if (name.getAlbumTitle().isEmpty() == true) {name.setAlbumTitle("Unknown");}


      addArtistTitle.add(name.getLeadArtist());
      addTrackTitle.add(name.getSongTitle());
      addAlbumTitle.add(name.getAlbumTitle());
      addGenre.add(name.getSongGenre());

      }

    }



  musicManagerUI sendTrackInfo = new musicManagerUI();
 sendTrackInfo.printArtistName(addArtistTitle); 
  sendTrackInfo.printTrackTitle(addTrackTitle);
  sendTrackInfo.printAlbumTitle(addAlbumTitle);
  sendTrackInfo.printGenre(addGenre);


   }   







   catch(NullPointerException e){

   System.err.println("Unknown Artist");

   //return;




   }

}

}

For this program I am using the ID3 Tagging Library/API. If you need the full code then please email me. Thanks in advance.

Some other sources which may be useful:

JTable does not show anything? Loading Java JTable: Why does it not work?

Community
  • 1
  • 1
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Dec 23 '13 at 01:13
  • Ok thanks for the tips. I will remember to do it next time. – HarryBlaise Dec 23 '13 at 05:25

1 Answers1

2

My first thought is that in your getMP3Tags method you are calling

musicManagerUI setFields = new musicManagerUI ();

Which is creating a new instance of the musicManagerUI which has no relationship to the one that is on the screen...

What you might consider doing is passing a reference of the current instance of musicManagerUI to the method instead...

fw.getMP3Tags(FILE_DIR, this);

Assuming this is an instance of musicManagerUI which is actually been displayed...

ps- I should mention that you are actually create another instance of musicManagerUI in the getMP3Tags...

musicManagerUI sendTrackInfo = new musicManagerUI();
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for the quick reply. I'm not too familiar with using this. How would I pass "this" in to the method parameter of getMP3Tags without getting an error? – HarryBlaise Dec 23 '13 at 01:40
  • You need to change `getMP3Tags` method to accept an instance of `musicManagerUI` and then pass it the reference on the screen, which I assume the mouse listener is implemented within... – MadProgrammer Dec 23 '13 at 01:50