Page 3 of 3 FirstFirst 123
Results 41 to 46 of 46
Like Tree4Likes

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

  1. #41
    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 latest attempt (Stack Trace) has me wondering if the compiler is not attempting to use the right version of getAudioInputStream(). The error is just after I assign the "InputStream inStr" to the file, as the GetAudioInputStream(inStr); is called. The second error is in the main method attempting to create the class. (AlphaPlay frame = new AlphaPlay();)

    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:42 2)
    at biz.mybiz.alpha.AlphaPlay$28.run(AlphaPlay.java:50 8)
    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)

    I think the code is correct. Is there a way to tell "getAudioInputStream(InputStream = inStr) ???

    Here is the code:
    Java Code:
    try {
    				//myURL  = new URL("files/ZZOpen/ABCSong.wav");
    		InputStream inStr = AlphaPlay.class.getResourceAsStream(sound);
    		AudioInputStream audio = AudioSystem.getAudioInputStream(inStr); //actually line 422
    		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);
    		}
    Last edited by NotTooOld; 04-10-2013 at 10:41 AM.

  2. #42
    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 NotTooOld View Post
    This latest attempt (Stack Trace) has me wondering if the compiler is not attempting to use the right version of getAudioInputStream(). The error is just after I assign the "InputStream inStr" to the file, as the GetAudioInputStream(inStr); is called. The second error is in the main method attempting to create the class. (AlphaPlay frame = new AlphaPlay();)
    No, it is getResourceAsStream that is failing... but why? Class.getResourceAsStream() only accepts a String representing the path to the file. I'll try it with the absolute path... <fingers crossed> Can I trust the absolute path I see when I right click the file in Eclipse? It is the same as Windows.

  3. #43
    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 NotTooOld View Post
    No, it is getResourceAsStream that is failing... but why? Class.getResourceAsStream() only accepts a String representing the path to the file. I'll try it with the absolute path... <fingers crossed> Can I trust the absolute path I see when I right click the file in Eclipse? It is the same as Windows.
    / <------- That was the culprit! While getAudioInputStream("files/whatever") works, getResourceAsStream(requires "/files/whatever") Damn glad I got that worked out. The same goes for getResource.
    Last edited by NotTooOld; 04-10-2013 at 11:28 AM.

  4. #44
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,814
    Rep Power
    19

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

    As I said earlier, getResourceAsStream and getResource get the resource based either on the location of the class file, or the classpath root.
    Without a '/' it gets it based on the class.
    With one it gets it based on the "top" of the classpath (for a jar file it would be the top of the jar file).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #45
    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
    As I said earlier, getResourceAsStream and getResource get the resource based either on the location of the class file, or the classpath root.
    Without a '/' it gets it based on the class.
    With one it gets it based on the "top" of the classpath (for a jar file it would be the top of the jar file).
    thanks. Having done it once, and having so much trouble with it, I'll not forget.

  6. #46
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,814
    Rep Power
    19

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

    That tends to be the only way I ever remember these things.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 3 of 3 FirstFirst 123

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
  •