• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting a Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm trying to get '?' button to play a random sound effect.  All other buttons work correctly but I'm lost as to why I'm getting this exception.  Any help is greatly appreciated.  

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.teamtreehouse.GUI$SoundEffectArray.play(GUI.java:131)
at com.teamtreehouse.GUI.actionPerformedF(GUI.java:172)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.teamtreehouse.GUI$SoundEffectArray.play(GUI.java:131)


Look at line 131 and find where the null value is.  It is hard for us to see line 131 in the posted code.

Please edit the post and wrap the code in code tags:  Select the code and press the Code button.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm is right that it would be a lot easier for us if you posted the code inside code tags, so we can see the line numbering.

However, there is only one line in that play() method.

Consequently clip is null.

That implies that the setFile() method of SoundEffectArray isn't working correctly.

A look in that method and you are eating exceptions.
If there was an issue with reading the sound file then you would never know as the program would happily continue running (well, until you hit the null pointer exception).

At the very least add an e.printStackTrace() call in there.
I can see what the issue is, but you need to see it for yourself.
 
Joe Duncan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about that.  The post has been edited.  I'll do some more digging.  Thanks for the help.
 
Joe Duncan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After some digging, I'm still lost.  I don't understand why clip is null.  Especially if the other buttons are working.  
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is clip given a value?  Add a print statement next to that  statement that prints out a message saying clip has been given a value.
Then execute the program to see if the message is printed before the program gets the exception.
If there is no message printed, then clip will still be null and trying to use it will give the exception.
The solution is to make sure clip is given a value before it is used.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you added the bit of code I suggested into the catch block in setFile() in SoundEffectArray?
That would then tell you exactly what's wrong.

All the other buttons work because they call SoundEffect, which is not the same.
 
Joe Duncan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep I added it.  And I understand that the other buttons work because the call SoundEffect. I guess what I'm missing is why that one works and SoundEffectArray doesn't.  But...that is the issue.  lol.  With the added code I am now getting a
"java.io.FileNotFoundException: [Ljava.lang.String;@172ebcd8 (The system cannot find the file specified)"

it points to the sea.setFileA(soundF);.  So why is se.setFile working and sea.setFileA isn't?  That's what I'm trying to figure out now.  Hopefully I'm on the right track.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[Ljava.lang.String;@172ebcd8


That String is created by the toString method for a String array.  Why is  an array being passed to the File class?

Line 119 looks weird.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Norm says, you are passing a String[] to the File class.
It is expecting a single String  referencing a file.

If you want to deal with multiple files then you'll need to handle them all individually.
 
Joe Duncan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally got it with some added help from my daughter.  This is a working solution.  Thanks for the help.

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 92 is empty.  There should be a call to the printStackTrace method in ALL catch blocks to show you when an exception has happened.
See line 124 in previous post for an example.

Why are the paths to the sound files defined in two separate places: lines  67-72 and 132-136.  There should only be one definition of each path that is used in both places.
 
Joe Duncan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep.  Thanks.  e.printStackTrace() added.  
The reason the sound files are defined in two separate places is because they are being used by different buttons.  Is it better to have the variables added in the array?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

files are defined in two separate places


The reason to define Strings in separate places is because there needs to be separate sets of values.  If the intention is to have only one set of Strings, it is better to define the Strings in one place and use those variables in all the places they are needed.
 
Joe Duncan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah.  Ok.  That definitely has a cleaner functionality and look to it.  The post has been updated.  Thanks
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kindly don't edit posts to add “corrections” like that after the event. I have reverted the changes.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic