Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Jave : Adding File to directory

Options
  • 09-04-2008 4:07pm
    #1
    Registered Users Posts: 269 ✭✭


    I have a project to develop a Jukebox in java i have it playing sound fast forwarding everything and i have a directory where all my sound are stored. Part of the project is to allow a user add music or other sounds to the system

    How do i do that. How do i allow a user add sounds to my system to the directory where my other sounds are stored . I have looked a JFileChooser but there is not method that allow me to save a file to my sound directory can anyone help.


Comments

  • Registered Users Posts: 1,996 ✭✭✭lynchie


    Call getSelectedFile() on the JFileChooser.

    Then copy the file to the appropriate directory. Quick way is to Use Apache commons IO to do the file copy. Alternative is to do it yourself.
    File source = chooser.getSelectedFile();
    
    File dest = new File("c:\\my directory\\"+source.getName());
    copy(source,dest);
    

    Copy Method
    ** Fast & simple file copy. */
    public static void copy(File source, File dest) throws IOException {
         FileChannel in = null, out = null;
         try {          
              in = new FileInputStream(source).getChannel();
              out = new FileOutputStream(dest).getChannel();
     
              long size = in.size();
              MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
     
              out.write(buf);
     
         } finally {
              if (in != null)          in.close();
              if (out != null)     out.close();
         }
    }
    


  • Registered Users Posts: 269 ✭✭cyberwit


    Thanks i really appreciate this


  • Registered Users Posts: 269 ✭✭cyberwit


    musicItem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    //addMusic();
    saveMusic = new JFileChooser();
    saveMusic.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    File source = saveMusic.getSelectedFile();
    File dest = new File("audio/" + source.getName());
    Save(source,dest);

    }
    }
    );

    public void Save(File source, File dest) throws IOException{
    FileChannel in = null;
    FileChannel out = null;

    try{
    in = new FileInputStream(source).getChannel();
    out = new FileOutputStream(dest).getChannel();

    long size = in.size();
    MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY,0,size);
    out.write(buf);
    }
    finally{
    if(in!=null)
    {
    in.close();
    }
    if(out!=null)
    {
    out.close();
    }
    }

    }

    What is the following error i am getting for the following method call Save(source,dest);

    Unhandled exception type IOException


  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    import java.io.IOException;
    try{
    What you had here
    }
    catch(IOException e){
    blahblah
    }


  • Registered Users Posts: 269 ✭✭cyberwit


    Is there any other java code that will allow me add a sound file to a directory as the above code is not functioning for me.


  • Advertisement
  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    "Not functioning" how? Whats your code?


  • Registered Users Posts: 269 ✭✭cyberwit


    import javax.swing.Box;
    import javax.swing.AbstractButton;
    import javax.swing.JFrame;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import javax.swing.JFileChooser;
    import java.awt.Container;
    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import javax.swing.JToolBar;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JScrollPane;
    import java.io.*;
    import java.io.IOException;
    import javax.swing.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.nio.channels.FileChannel;
    import java.nio.MappedByteBuffer;

    public class MediaMenu {

    private JFrame mediaFrame;
    private JMenuBar menuBar;
    private JMenu menuFile;
    private JMenu menuHelp;
    private JMenu menuInsert;
    private JMenuItem musicGalleryItem,imageGalleryItem,musicItem,pictureItem,exitItem;
    private JLabel musiclabel;
    private ImageIcon jukebox;
    private ImageIcon imageGallery;
    private ImageIcon musicGallery;
    private JToolBar options;
    private JButton music;
    private JButton images;
    private BorderLayout layout;
    private JFileChooser saveMusic;
    private File musicFile;


    public MediaMenu()
    {
    setFrame();
    setMenu();
    setToolBar();
    }

    public void setFrame()
    {
    mediaFrame = new JFrame("Media Menu");//Creating a frame
    Container getContentPane = mediaFrame.getContentPane();//Setting up frames contentPane
    //Layout Manager
    layout = new BorderLayout(10,10);
    //musicFrame.setSize(570,472);
    mediaFrame.setSize(567,510);
    mediaFrame.setResizable(false);
    mediaFrame.setLayout(layout);//Assign Layout manager to frame
    musiclabel=new JLabel();//creating label and assigning an image to it
    musiclabel.setToolTipText("JukeBox");//Using the setToolTip Text of the JLabel class to and a too tip
    jukebox= new ImageIcon(("pictures/jukeboxnew.jpg"));//Creating Image Icon and storing image in it
    musiclabel.setIcon(jukebox);
    getContentPane.add(musiclabel, BorderLayout.NORTH);//Adding label to contentPane of frame
    mediaFrame.setIconImage(new ImageIcon("pictures/musicsymbol.jpg").getImage());//Puts Custom Icon on Frame Window
    mediaFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mediaFrame.setVisible(true);
    }

    public void setMenu()throws IOException{

    menuBar = new JMenuBar();//Create Menu Bar
    menuFile = new JMenu("File");//Create File Menu Option
    menuInsert = new JMenu("Insert");
    menuHelp = new JMenu("Help");//Create Help Menu Option

    musicGalleryItem = new JMenuItem("Music Gallery");
    imageGalleryItem = new JMenuItem("Image Gallery");
    exitItem = new JMenuItem("Exit");
    musicItem = new JMenuItem("Add Music");//Create subMenu Option
    pictureItem = new JMenuItem("Add Pictures");

    musicGalleryItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    SoundPlayerGUI musicplayer = new SoundPlayerGUI();
    }
    }
    );

    imageGalleryItem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    ImagePlayerGUI imageplayer = new ImagePlayerGUI();
    }
    }
    );
    exitItem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    }
    );

    musicItem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {

    // addMusic();
    saveMusic = new JFileChooser();
    saveMusic.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    File source = saveMusic.getSelectedFile();
    File dest = new File("audio/" + source.getName());
    Save(source,dest);


    }
    }
    );

    mediaFrame.setJMenuBar(menuBar);//Adding Menu Bar to Frame

    menuBar.add(menuFile);//Add File Menu Option to Menu Bar
    menuBar.add(menuInsert);
    menuBar.add(Box.createHorizontalGlue());//Using the BoxLayout Manager to create a space between File and Help
    menuBar.add(menuHelp);//Add Help Menu Option to Menu Bar
    menuInsert.add(musicItem);//Add sub Menu Add Music to File Menu Option
    menuInsert.add(pictureItem);
    menuFile.add(musicGalleryItem);
    menuFile.add(imageGalleryItem);
    menuFile.add(exitItem);
    }

    //private class

    public void setToolBar()
    {
    options = new JToolBar("Media Options");//create ToolBar
    options.setSize(562,81);

    imageGallery = new ImageIcon("pictures/image.jpg");//Create ImageIcon
    musicGallery = new ImageIcon("pictures/imageIpod.jpg");

    music = new JButton("Music Gallery",musicGallery); //creating button
    music.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    SoundPlayerGUI musicplayer = new SoundPlayerGUI();
    }
    }
    );

    images = new JButton("Image Gallery",imageGallery);
    images.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    ImagePlayerGUI imageplayer = new ImagePlayerGUI();
    }
    }
    );

    music.setVerticalTextPosition(AbstractButton.BOTTOM);
    images.setVerticalTextPosition(AbstractButton.BOTTOM);//Aligning Image and Text on Button
    music.setHorizontalTextPosition(AbstractButton.CENTER);
    images.setHorizontalTextPosition(AbstractButton.CENTER);
    options.add(music);
    options.add(Box.createHorizontalGlue());
    options.add(images);
    mediaFrame.add(options,BorderLayout.CENTER);
    }

    //public void addMusic()throws IOException
    //{
    //saveMusic = new JFileChooser();
    //saveMusic.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    //saveMusic.addActionListener(new ActionListener(){
    //public void actionPerformed(ActionEvent e)
    // {
    //saveMusic.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    // File source = saveMusic.getSelectedFile();
    //File dest = new File("audio/" + source.getName());
    //Save(source,dest);
    //}
    // }
    // );


    //}

    public void Save(File source,File dest) throws IOException{
    FileChannel in = null;
    FileChannel out = null;

    try{
    in = new FileInputStream(source).getChannel();
    out = new FileOutputStream(dest).getChannel();

    long size = in.size();
    MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY,0,size);
    out.write(buf);
    }
    catch(IOException e){
    if(in!=null)
    {
    in.close();
    }
    if(out!=null)
    {
    out.close();
    }
    }


    }
    }





    Error with Save(source,dest);
    Error Message Unhandled exception type IOException"]


  • Closed Accounts Posts: 2,267 ✭✭✭h57xiucj2z946q


    possibly try:
    public void Save(File source,File dest) {
    	FileChannel in = null;
    	FileChannel out = null;
    
    	try {
    	in = new FileInputStream(source).getChannel();
    	out = new FileOutputStream(dest).getChannel();
    	
    	long size = in.size();
    	MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY,0,size);
    	out.write(buf);
    	}
    	catch(IOException e){
    		e.printStackTrace();
    	} finally {
    		if(in!=null)
    			in.close();
    		if(out!=null)
    			out.close();
    	}
    }
    


  • Registered Users Posts: 269 ✭✭cyberwit


    public void Save(File source,File dest) {
    FileChannel in = null;
    FileChannel out = null;

    try {
    in = new FileInputStream(source).getChannel();
    out = new FileOutputStream(dest).getChannel();

    long size = in.size();
    MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY,0,size);
    out.write(buf);
    }
    catch(IOException e){
    e.printStackTrace();
    }
    finally {
    if(in!=null){
    in.close();
    }
    if(out!=null){
    out.close();
    }
    }
    }
    }

    I am now getting the exception error with whats in red
    Unhandled exception type IOException


  • Closed Accounts Posts: 2,267 ✭✭✭h57xiucj2z946q


    When calling the "Save" function:
    try {
    	Save(source,dest);
    } catch (IOException e) {
    	e.printStackStrace();
    }
    


    Sorry forgot to leave in throws IOException
    public void Save(File source,File dest) throws IOException {
    	FileChannel in = null;
    	FileChannel out = null;
    
    	try {
    	in = new FileInputStream(source).getChannel();
    	out = new FileOutputStream(dest).getChannel();
    	
    	long size = in.size();
    	MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY,0,size);
    	out.write(buf);
    	}
    	catch(IOException e){
    		e.printStackTrace();
    	} finally {
    		if(in!=null)
    			in.close();
    		if(out!=null)
    			out.close();
    	}
    }
    


  • Advertisement
  • Registered Users Posts: 269 ✭✭cyberwit


    import javax.swing.Box;
    import javax.swing.AbstractButton;
    import javax.swing.JFrame;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import javax.swing.JFileChooser;
    import java.awt.Container;
    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import javax.swing.JToolBar;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JScrollPane;
    import java.io.*;
    import java.io.IOException;
    import javax.swing.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.nio.channels.FileChannel;
    import java.nio.MappedByteBuffer;

    public class MediaMenu {

    private JFrame mediaFrame;
    private JMenuBar menuBar;
    private JMenu menuFile;
    private JMenu menuHelp;
    private JMenu menuInsert;
    private JMenuItem musicGalleryItem,imageGalleryItem,musicItem,pictureItem,exitItem;
    private JLabel musiclabel;
    private ImageIcon jukebox;
    private ImageIcon imageGallery;
    private ImageIcon musicGallery;
    private JToolBar options;
    private JButton music;
    private JButton images;
    private BorderLayout layout;
    private JFileChooser saveMusic;
    private File musicFile;


    public MediaMenu()
    {
    setFrame();
    setMenu();
    setToolBar();
    }

    public void setFrame()
    {
    mediaFrame = new JFrame("Media Menu");//Creating a frame
    Container getContentPane = mediaFrame.getContentPane();//Setting up frames contentPane
    //Layout Manager
    layout = new BorderLayout(10,10);
    //musicFrame.setSize(570,472);
    mediaFrame.setSize(567,510);
    mediaFrame.setResizable(false);
    mediaFrame.setLayout(layout);//Assign Layout manager to frame
    musiclabel=new JLabel();//creating label and assigning an image to it
    musiclabel.setToolTipText("JukeBox");//Using the setToolTip Text of the JLabel class to and a too tip
    jukebox= new ImageIcon(("pictures/jukeboxnew.jpg"));//Creating Image Icon and storing image in it
    musiclabel.setIcon(jukebox);
    getContentPane.add(musiclabel, BorderLayout.NORTH);//Adding label to contentPane of frame
    mediaFrame.setIconImage(new ImageIcon("pictures/musicsymbol.jpg").getImage());//Puts Custom Icon on Frame Window
    mediaFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mediaFrame.setVisible(true);
    }

    public void setMenu()throws IOException{

    menuBar = new JMenuBar();//Create Menu Bar
    menuFile = new JMenu("File");//Create File Menu Option
    menuInsert = new JMenu("Insert");
    menuHelp = new JMenu("Help");//Create Help Menu Option

    musicGalleryItem = new JMenuItem("Music Gallery");
    imageGalleryItem = new JMenuItem("Image Gallery");
    exitItem = new JMenuItem("Exit");
    musicItem = new JMenuItem("Add Music");//Create subMenu Option
    pictureItem = new JMenuItem("Add Pictures");

    musicGalleryItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    SoundPlayerGUI musicplayer = new SoundPlayerGUI();
    }
    }
    );

    imageGalleryItem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    ImagePlayerGUI imageplayer = new ImagePlayerGUI();
    }
    }
    );
    exitItem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    }
    );

    musicItem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {

    // addMusic();
    saveMusic = new JFileChooser();
    saveMusic.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    File source = saveMusic.getSelectedFile();
    File dest = new File("audio/" + source.getName());
    try {
    Save(source,dest);
    } catch (IOException event) {
    System.out.println("Help");
    }


    }
    }
    );

    mediaFrame.setJMenuBar(menuBar);//Adding Menu Bar to Frame

    menuBar.add(menuFile);//Add File Menu Option to Menu Bar
    menuBar.add(menuInsert);
    menuBar.add(Box.createHorizontalGlue());//Using the BoxLayout Manager to create a space between File and Help
    menuBar.add(menuHelp);//Add Help Menu Option to Menu Bar
    menuInsert.add(musicItem);//Add sub Menu Add Music to File Menu Option
    menuInsert.add(pictureItem);
    menuFile.add(musicGalleryItem);
    menuFile.add(imageGalleryItem);
    menuFile.add(exitItem);
    }


    //private class

    public void setToolBar()
    {
    options = new JToolBar("Media Options");//create ToolBar
    options.setSize(562,81);

    imageGallery = new ImageIcon("pictures/image.jpg");//Create ImageIcon
    musicGallery = new ImageIcon("pictures/imageIpod.jpg");

    music = new JButton("Music Gallery",musicGallery); //creating button
    music.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
    SoundPlayerGUI musicplayer = new SoundPlayerGUI();
    }
    }
    );

    images = new JButton("Image Gallery",imageGallery);
    images.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    ImagePlayerGUI imageplayer = new ImagePlayerGUI();
    }
    }
    );

    music.setVerticalTextPosition(AbstractButton.BOTTOM);
    images.setVerticalTextPosition(AbstractButton.BOTTOM);//Aligning Image and Text on Button
    music.setHorizontalTextPosition(AbstractButton.CENTER);
    images.setHorizontalTextPosition(AbstractButton.CENTER);
    options.add(music);
    options.add(Box.createHorizontalGlue());
    options.add(images);
    mediaFrame.add(options,BorderLayout.CENTER);
    }

    //public void addMusic()throws IOException
    //{
    //saveMusic = new JFileChooser();
    //saveMusic.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    //saveMusic.addActionListener(new ActionListener(){
    //public void actionPerformed(ActionEvent e)
    // {
    //saveMusic.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    // File source = saveMusic.getSelectedFile();
    //File dest = new File("audio/" + source.getName());
    //Save(source,dest);
    //}
    // }
    // );


    //}

    public void Save(File source,File dest) throws IOException {
    FileChannel in = null;
    FileChannel out = null;

    try {
    in = new FileInputStream(source).getChannel();
    out = new FileOutputStream(dest).getChannel();

    long size = in.size();
    MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY,0,size);
    out.write(buf);
    }
    catch(IOException e){
    e.printStackTrace();
    } finally {
    if(in!=null)
    in.close();
    if(out!=null)
    out.close();
    }
    }
    }
    Please look at attachment


  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    Damo told you what to do


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Using the [noparse]
    
    [/noparse] tags will make you posts a lot more readable too. 


  • Registered Users Posts: 269 ✭✭cyberwit


    
    import javax.swing.Box;
    import javax.swing.AbstractButton;
    import javax.swing.JFrame;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    import javax.swing.JFileChooser;
    import java.awt.Container;
    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import javax.swing.JToolBar;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JScrollPane;
    import java.io.*;
    import java.io.IOException;
    import javax.swing.event.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.nio.channels.FileChannel;
    import java.nio.MappedByteBuffer;
    
    public class MediaMenu {
    	
    	private JFrame mediaFrame;
        private JMenuBar menuBar;
        private JMenu menuFile;
        private JMenu menuHelp;
        private JMenu menuInsert;
        private JMenuItem musicGalleryItem,imageGalleryItem,musicItem,pictureItem,exitItem;
        private JLabel musiclabel;
        private ImageIcon jukebox;
        private ImageIcon imageGallery;
        private ImageIcon musicGallery;
        private JToolBar options;
        private JButton music;
        private JButton images;
        private BorderLayout layout;
        private JFileChooser saveMusic;
        private File musicFile;
       
        
    public MediaMenu()
    {
    	setFrame();
    	setMenu();
    	setToolBar();  
    }
    
    public void setFrame()
    {
    	mediaFrame = new JFrame("Media Menu");//Creating a frame
    	Container getContentPane = mediaFrame.getContentPane();//Setting up frames contentPane
    	//Layout Manager
    	layout = new BorderLayout(10,10);
    	//musicFrame.setSize(570,472);
    	mediaFrame.setSize(567,510);
    	mediaFrame.setResizable(false);
    	mediaFrame.setLayout(layout);//Assign Layout manager to frame
    	musiclabel=new JLabel();//creating label and assigning an image to it
    	musiclabel.setToolTipText("JukeBox");//Using the setToolTip Text of the JLabel class to and a too tip
    	jukebox= new ImageIcon(("pictures/jukeboxnew.jpg"));//Creating Image Icon and storing image in it
        musiclabel.setIcon(jukebox);
    	getContentPane.add(musiclabel, BorderLayout.NORTH);//Adding label to contentPane of frame
    	mediaFrame.setIconImage(new ImageIcon("pictures/musicsymbol.jpg").getImage());//Puts Custom Icon on Frame Window
    	mediaFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	mediaFrame.setVisible(true);
    }
    
    public void setMenu()
    {
    	menuBar = new JMenuBar();//Create Menu Bar
    	menuFile = new JMenu("File");//Create File Menu Option
    	menuInsert = new JMenu("Insert");
    	menuHelp = new JMenu("Help");//Create Help Menu Option
    	musicGalleryItem = new JMenuItem("Music Gallery");
    	imageGalleryItem = new JMenuItem("Image Gallery");
    	exitItem = new JMenuItem("Exit");
    	musicItem = new JMenuItem("Add Music");//Create subMenu Option 
    	pictureItem = new JMenuItem("Add Pictures");
    	
    	musicGalleryItem.addActionListener(new ActionListener() {
    	        public void actionPerformed(ActionEvent e)
    	            { 
    	        	SoundPlayerGUI musicplayer = new SoundPlayerGUI();} });
    	
    	imageGalleryItem.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e)
    			  { 
    		      ImagePlayerGUI imageplayer = new ImagePlayerGUI();}});
    	
    	exitItem.addActionListener(new ActionListener(){
    		    public void actionPerformed(ActionEvent e){System.exit(0);}});
    	
    	musicItem.addActionListener(new ActionListener(){ 
    		public void actionPerformed(ActionEvent e){ // addMusic();
    				saveMusic = new JFileChooser();
    				saveMusic.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    				File source = saveMusic.getSelectedFile();
    				File dest = new File("/simplesound/audio/"+source.getName());
    					try {
    						Save(source,dest);
    						} catch (IOException event) {
    	    			event.printStackTrace();
    						}
    	    	  	    } });
    
    	mediaFrame.setJMenuBar(menuBar);//Adding Menu Bar to Frame
    	menuBar.add(menuFile);//Add File Menu Option to Menu Bar
    	menuBar.add(menuInsert);
    	menuBar.add(Box.createHorizontalGlue());//Using the BoxLayout Manager to create a space between File and Help
    	menuBar.add(menuHelp);//Add Help Menu Option to Menu Bar
        menuInsert.add(musicItem);//Add sub Menu Add Music to File Menu Option
        menuInsert.add(pictureItem);
        menuFile.add(musicGalleryItem);
        menuFile.add(imageGalleryItem);
        menuFile.add(exitItem);
    }//end of setFrame();
    
    
    //private class
    
    public void setToolBar()
    {
    	options = new JToolBar("Media Options");//create ToolBar
    	options.setSize(562,81);
    	
    	imageGallery = new ImageIcon("pictures/image.jpg");//Create ImageIcon
        musicGallery = new ImageIcon("pictures/imageIpod.jpg");
        
        music = new JButton("Music Gallery",musicGallery); //creating button
        music.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
                { 
            	SoundPlayerGUI musicplayer = new SoundPlayerGUI();
                }
               }
              );
    
        images = new JButton("Image Gallery",imageGallery);
        images.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e)
              { 
    	      ImagePlayerGUI imageplayer = new ImagePlayerGUI();
    	      }
             }
            );
        
        music.setVerticalTextPosition(AbstractButton.BOTTOM);
        images.setVerticalTextPosition(AbstractButton.BOTTOM);//Aligning Image and Text on Button
        music.setHorizontalTextPosition(AbstractButton.CENTER);
        images.setHorizontalTextPosition(AbstractButton.CENTER);
        options.add(music);
        options.add(Box.createHorizontalGlue());
        options.add(images);
        mediaFrame.add(options,BorderLayout.CENTER);
    }
    
    //public void addMusic()throws IOException
    //{
    	//saveMusic = new JFileChooser();
    	//saveMusic.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    	//saveMusic.addActionListener(new ActionListener(){
    	      //public void actionPerformed(ActionEvent e)
    	        // { 
    	    	   //saveMusic.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    	    	  // File source = saveMusic.getSelectedFile();
    	    		//File dest = new File("audio/" + source.getName());
    	    		//Save(source,dest);
    		     //}
    	       // }
    	      // );
    	
    	
    //}
    
    public void Save(File source,File dest) throws IOException {
    	FileChannel in = null;
    	FileChannel out = null;
    
    	try {
    	in = new FileInputStream(source).getChannel();
    	out = new FileOutputStream(dest).getChannel();
    	
    	long size = in.size();
    	MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY,0,size);
    	out.write(buf);
    	}
    	catch(IOException e){
    		e.printStackTrace();
    	} finally {
    		if(in!=null)
    			in.close();
    		if(out!=null)
    			out.close();
    	}
    }
    }
    
    

    The program runs but when i click on the Add Music in the Menu to add a sound file all i get is the following:


    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MediaMenu$4.actionPerformed(MediaMenu.java:103)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)



  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    You should learn how to read a stack trace. It's line 103
    Save(source,dest);
    

    Either source and/or dest is null.


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    You should also read up on what Exceptions are, how to declare them, catch them, throw them as below it is plainly obvious that you weren't catching or throwing the IOException.
    Error with Save(source,dest);
    Error Message Unhandled exception type IOException"]
    


  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    Evil Phil wrote: »
    You should learn how to read a stack trace. It's line 103
    Save(source,dest);
    

    Either source and/or dest is null.

    Hes using eclipse, click on the error and it brings you to the line


  • Registered Users Posts: 269 ✭✭cyberwit


    I am trying to export the project as a JAR i have gone through the process in eclipse except all that is showing is only two of my windows with nothing on them and my music gallery is not showing at all can anyone help


Advertisement