Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 46
Like Tree4Likes

Thread: This very simple code throws exception... and I can't understand why

  1. #21
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by Tolls View Post
    (That other reply was in reply to your previosu post).

    It wasn't intenede to fix the audio problem, but to give you the correct way to access the audio file, if that file is intended to be part of your application deployment.
    Thanks for all your help. I've learned a lot, even if I still can't make a wav file play.

  2. #22
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: This very simple code throws exception... and I can't understand why

    It has to be something with the WAV file. I was able to get your code to play a WAV file on my Computer. You can try setting the parameters through getAudioInputStream().

  3. #23
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Let's close this thread! Thanks to all who contributed, and especially the very patient Tolls. My cow is now mooing in the kids program I'm working on. "Yay!"

    The original code I posted when starting this thread works fine. The wav file (in fact all the wav files on my computer) are not recognized by javax.sound.sampled.AudioSystem. Something to do with the sample rate I'm guessing. I downloaded Switch Sound File Converter for free and opened and resaved moo.wav. I'm a happy rancher now.

  4. #24
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    OK, next problem... I am sure the path is correct because this all worked before I tried to insert the getResourceAsStream bit. I have no compiler errors. (The "try" is actually on line 416 of my code.) Line 502 is in the main method and is trying to instanciate the AlphaPlay class.

    This is the block of code that throws the following exceptions:

    Java Code:
    			try {
    				AudioInputStream audio = AudioSystem.getAudioInputStream
    						(AlphaPlay.class.getResourceAsStream
    						("files/ZZOpen/ABCSong.wav"));
    		        Clip clip = AudioSystem.getClip();
    		        clip.addLineListener(this);
    		        clip.open(audio);
    		        clip.start();   
    		        }
    		        catch(UnsupportedAudioFileException uae) {
    		            System.out.println(uae);
    		        }
    		        catch(IOException ioe) {
    		            System.out.println(ioe);
    		        }
    		        catch(LineUnavailableException lua) {
    		            System.out.println(lua);
    		        }
    				song += 1;	
    		}
    STACK TRACE

    java.lang.NullPointerException
    at com.sun.media.sound.SoftMidiAudioFileReader.getAud ioInputStream(Unknown Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
    at biz.mybiz.alpha.AlphaPlay.<init>(AlphaPlay.java:41 7)
    at biz.mybiz.alpha.AlphaPlay$28.run(AlphaPlay.java:50 2)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Last edited by NotTooOld; 04-09-2013 at 10:48 AM.

  5. #25
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: This very simple code throws exception... and I can't understand why

    Again use some tests and debug:
    Java Code:
    File f = new File("files/ZZOpen/ABCSong.wav");
    System.out.println(f.getAbsolutePath());
    if(!f.exists()){
    	System.err.println("forget it!");
    	return;
    }
    InputStream instr = AlphaPlay.class.getResourceAsStream("files/ZZOpen/ABCSong.wav");
    if(instr == null){
    	System.err.println("forget it!");
    	return;
    }
    AudioInputStream audio = AudioSystem.getAudioInputStream(instr);
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #26
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by PhHein View Post
    Again use some tests and debug:
    Java Code:
    File f = new File("files/ZZOpen/ABCSong.wav");
    System.out.println(f.getAbsolutePath());
    if(!f.exists()){
    	System.err.println("forget it!");
    	return;
    }
    InputStream instr = AlphaPlay.class.getResourceAsStream("files/ZZOpen/ABCSong.wav");
    if(instr == null){
    	System.err.println("forget it!");
    	return;
    }
    AudioInputStream audio = AudioSystem.getAudioInputStream(instr);
    I'm not getting the errors, but the wav isn't playing either...
    Last edited by NotTooOld; 04-09-2013 at 11:36 AM.

  7. #27
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Code now =
    [code]
    try {
    File f = new File("files/ZZOpen/ABCSong.wav");
    System.out.println(f.getAbsolutePath());
    if(f.exists()){
    System.err.println("It Exists!");
    return;
    }
    InputStream inStr = AlphaPlay.class.getResourceAsStream
    ("files/ZZOpen/ABCSong.wav");
    if(inStr == null){ //// NOT TRUE
    System.err.println("How can it be null?");
    return;
    }
    AudioInputStream audio = AudioSystem.getAudioInputStream(inStr);

    // AudioInputStream audio = AudioSystem.getAudioInputStream
    // (AlphaPlay.class.getResourceAsStream
    // ("files/ZZOpen/ABCSong.wav"));
    Clip clip = AudioSystem.getClip();
    clip.addLineListener(this);
    clip.open(audio);
    clip.start();
    }
    catch(UnsupportedAudioFileException uae) {
    System.out.println(uae);
    }
    catch(IOException ioe) {
    System.out.println(ioe);
    }
    catch(LineUnavailableException lua) {
    System.out.println(lua);
    }

    And output is only:
    C:\Eclipse\workspaceZero\AlphaApp\files\ZZOpen\ABC Song.wav
    It Exists!

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by PhHein View Post
    Again use some tests and debug:
    Java Code:
    File f = new File("files/ZZOpen/ABCSong.wav");
    System.out.println(f.getAbsolutePath());
    if(!f.exists()){
    	System.err.println("forget it!");
    	return;
    }
    InputStream instr = AlphaPlay.class.getResourceAsStream("files/ZZOpen/ABCSong.wav");
    if(instr == null){
    	System.err.println("forget it!");
    	return;
    }
    AudioInputStream audio = AudioSystem.getAudioInputStream(instr);
    If you're using getResourceAsStream then you need to use getResource to find out the "path" being used.
    This is because otherwise you will not see the path used inside a jar file.
    File will get you the wrong thing, ie a path on the actual disk, not internal to a jar.

    So a slight change to the above:
    Java Code:
    URL f = AlphaPlay.class.getResource("files/ZZOpen/ABCSong.wav");
    System.out.println(f);
    InputStream instr = AlphaPlay.class.getResourceAsStream("files/ZZOpen/ABCSong.wav");
    if(instr == null){
    	System.err.println("forget it!");
    	return;
    }
    AudioInputStream audio = AudioSystem.getAudioInputStream(instr);
    Then it's a case of checking in the jar file and seeing if it matches the end part of the URL.
    PhHein likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #29
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    The LineListener is not sending a START message... and this line is getting skipped???? System.out.println(clip.getLineInfo());

    Java Code:
    try {
    				File f = new File("files/ZZOpen/ABCSong.wav");
    				System.out.println(f.getAbsolutePath());
    				if(f.exists()){     
    					System.err.println("It Exists!");     
    					return; 
    				}
    				InputStream inStr = AlphaPlay.class.getResourceAsStream
    						("files/ZZOpen/ABCSong.wav"); 
    				if(inStr == null){     
    					System.err.println("How can it be null?");     
    				return; 
    				} 
    				AudioInputStream audio = AudioSystem.getAudioInputStream(inStr); 
    
    														//			AudioInputStream audio = AudioSystem.getAudioInputStream
    														//					(AlphaPlay.class.getResourceAsStream
    														//					("files/ZZOpen/ABCSong.wav"));
    				Clip clip = AudioSystem.getClip();
    		        clip.addLineListener(this);
    		        clip.open(audio);
    		        System.out.println(clip.getLineInfo());
    		        clip.start();   
    		        }
    STACK TRACE
    C:\Eclipse\workspaceZero\AlphaApp\files\ZZOpen\ABC Song.wav
    It Exists!

  10. #30
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by Tolls View Post
    If you're using getResourceAsStream then you need to use getResource to find out the "path" being used.
    This is because otherwise you will not see the path used inside a jar file.
    File will get you the wrong thing, ie a path on the actual disk, not internal to a jar.

    So a slight change to the above:
    Java Code:
    URL f = AlphaPlay.class.getResource("files/ZZOpen/ABCSong.wav");
    System.out.println(f);
    InputStream instr = AlphaPlay.class.getResourceAsStream("files/ZZOpen/ABCSong.wav");
    if(instr == null){
    	System.err.println("forget it!");
    	return;
    }
    AudioInputStream audio = AudioSystem.getAudioInputStream(instr);
    Then it's a case of checking in the jar file and seeing if it matches the end part of the URL.
    I didn't see this post before posting my last. I'll try that now. But the wav file is not in a .jar, it's just in a folder. :p At least that's what windows says.
    Last edited by NotTooOld; 04-09-2013 at 12:36 PM.

  11. #31
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    This is getting weirder for me. Now it is skipping 2 System.out.println... and still no LineEvent START.

    Java Code:
    try {
    				File fl = new File("files/ZZOpen/ABCSong.wav");
    				System.out.println(fl.getAbsolutePath());
    				if(fl.exists()){     
    					System.err.println("It Exists!");     
    					return; 
    				}
    				URL f = AlphaPlay.class.getResource("files/ZZOpen/ABCSong.wav");
    				System.out.println(f.getFile());
    				InputStream inStr = AlphaPlay.class.getResourceAsStream
    						("files/ZZOpen/ABCSong.wav"); 
    				if(inStr == null){     
    					System.err.println("How can it be null?");     
    				return; 
    				} 
    				AudioInputStream audio = AudioSystem.getAudioInputStream(inStr); 
    
    														//			AudioInputStream audio = AudioSystem.getAudioInputStream
    														//					(AlphaPlay.class.getResourceAsStream
    														//					("files/ZZOpen/ABCSong.wav"));
    				Clip clip = AudioSystem.getClip();
    		        clip.addLineListener(this);
    		        clip.open(audio);
    		        System.out.println(clip.getLineInfo());
    		        clip.start();   
    		        }
    STACK TRACE still...

    C:\Eclipse\workspaceZero\AlphaApp\files\ZZOpen\ABC Song.wav
    It Exists!

  12. #32
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    You're not printing the URL.
    Don't use getFile().

    And I thought you said in your other thread you were jarring up your code?

    Where exactly is this code running and how are you running it?
    Where are the files you are trying to load?
    Are you trying to turn this into an executable jar file?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #33
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by Tolls View Post
    You're not printing the URL.
    Don't use getFile().

    And I thought you said in your other thread you were jarring up your code?

    Where exactly is this code running and how are you running it?
    Where are the files you are trying to load?
    Are you trying to turn this into an executable jar file?
    the other thread is about a problem exporting this to a runnable jar. I have a (workspace folder), (project folder), (settings folder, files folder, and package folder, there are also 2 files in there: .classpath and .project). And I just added that println so I could see if it would fire. It didn't. when I put "f" in there it prints nothing too.
    Last edited by NotTooOld; 04-09-2013 at 01:57 PM.

  14. #34
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Just to check again, I went back to the code that was working (only it doesn't export properly) and added a check... this debugging is strange stuff. How can the sound file play when I run this code, but the System.out.println doesn't execute?
    Java Code:
    		String sound = folder + letter + ".wav";
    		File file = new File(sound);
    		ImageIcon leftIcon = new ImageIcon(left);
    		ImageIcon topIcon = new ImageIcon(top);
    		ImageIcon rightIcon = new ImageIcon(right);
    		leftIcon.getImage().flush();
    		topIcon.getImage().flush();
    		rightIcon.getImage().flush();
    
    		try {
    			ImageLeft.setIcon(leftIcon);
    			ImageTop.setIcon(topIcon);
    			ImageRight.setIcon(rightIcon);
    		}
    		catch (Exception e){
    			e.printStackTrace();
    		}
    		try {
    			if (file.exists()){
    				System.out.println("file exists");
    			}
                AudioInputStream audio = AudioSystem.getAudioInputStream(file);
                Clip clip = AudioSystem.getClip();
                clip.addLineListener(this);
                clip.open(audio);
                clip.start();
    sure seems it should exist if I'm listening to it.

  15. #35
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    If you're going to export this as an executable jar, containing your audio file, then there is no point going down the File route as that will never work as a jar file.
    That's all I'm saying.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #36
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by Tolls View Post
    If you're going to export this as an executable jar, containing your audio file, then there is no point going down the File route as that will never work as a jar file.
    That's all I'm saying.
    That is my intention, to eventually export to a Runnable jar. So where do I go from here? I don't know what else to do to get this working with getResourceAsStream(). It seems that the wav file is located, where it is supposed to be, I can get an InputStream, but when I feed that to GetAudioInputStream it does something strange. Please see my post from 6:49 today.

  17. #37
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Considering what I think you are saying... in order to get all of the files to be included in the Runnable jar, will I need to hardcode (somewhere in my class file) every image and sound file?? Because as it is now, the path is built on the fly by the PlayAll method.

  18. #38
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by Tolls View Post
    If you're going to export this as an executable jar, containing your audio file, then there is no point going down the File route as that will never work as a jar file.
    That's all I'm saying.
    It just occured to me while reading all I can find about getResourceAsStream that it wants a file from a jar. My NullPointerException could be caused by the fact that the target is just a file???

  19. #39
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: This very simple code throws exception... and I can't understand why

    It works with stuff that's not a jar.
    It just uses the runtime root, or the class location, as a basis for the path.

    So, where is the file relative to the class file.
    Is the file in a source directory?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #40
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: This very simple code throws exception... and I can't understand why

    Quote Originally Posted by Tolls View Post
    It works with stuff that's not a jar.
    It just uses the runtime root, or the class location, as a basis for the path.

    So, where is the file relative to the class file.
    Is the file in a source directory?
    The class file (AlphaPlay) is in the same folder as a folder named "files" which contains folders A-Z and one folder "ZZOpen."
    Here is a relative path I've been using with no problems. ("files/ZZOpen/ABCSong.wav")

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 03-11-2012, 04:28 PM
  2. Simple Swing throws Exception in thread "AWT-EventQueue-0"
    By sultanofswing in forum AWT / Swing
    Replies: 3
    Last Post: 10-29-2010, 09:20 PM
  3. throws exception
    By simorgh in forum New To Java
    Replies: 1
    Last Post: 07-30-2010, 12:24 AM
  4. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •