开发者

why i get this error when i stop playing an audio from button action listener(the audio ran from Thread)the whole code is inside

i ran an audio file inside my thread(this thread is an alarm) this is the thread code :

public void waitForAlarm() {
    waiter = new Thread(new Runnable() {
        public void run() {
            while (Thread.currentThread() == waiter) {
                Calendar d = Calendar.getInstance();
                if (getAlarmHours() == d.get(Calendar.HOUR_OF_DAY)) {
                    if (getAlarmMinutes() == d.get(Calendar.MINUTE)) {
                        UserInterface.setAlarmText("No Alarms");
                        try {
                            AudioPlayer playSound = new AudioPlayer(
                                    UserInterface.getSoundFile());//return the sound choosen by user
                            playSound.play();//play the choosen sound
                            waiter.wait();
                        } catch (NoPlayerException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (CannotRealizeException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } finally {
                            break;
                        }

                    }

                }

            }
        }
    });

    waiter.start();
}

this is the button action listener method :

stopSound.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            AudioPlayer player = new AudioPlayer();
            player.stop();//call stop method(this method will stop & close the audio File)
        }
    });

finally this is the exception i get when i click that button :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AudioPlayer.stop(AudioPlayer.java:34)
at UserInterface$4.actionPerformed(UserInterface.java:214)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents开发者_如何学运维(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

more info:

-there's a thread for displaying the current time

-i use this code to init the program SwingUtilities.invokeLater(new Runnable()

-these are the imports used in the AudioPlayer class

import javax.media.CannotRealizeException; import javax.media.Manager; import javax.media.NoPlayerException; import javax.media.Player;


This code looks like it shouldn't do anything, and in particular shouldn't stop any playing audio:

stopSound.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        AudioPlayer player = new AudioPlayer();
        player.stop();
    }
});

The reason being you are creating a new AudioPlayer within this listener, one that appears to be completely unrelated to any other AudioPlayer object including the AudioPlayer object that is currently playing your alarm.

Here's your currently playing AudioPlayer:

public void waitForAlarm() {
    waiter = new Thread(new Runnable() {
        public void run() {
            while (Thread.currentThread() == waiter) {
                Calendar d = Calendar.getInstance();
                if (getAlarmHours() == d.get(Calendar.HOUR_OF_DAY)) {
                    if (getAlarmMinutes() == d.get(Calendar.MINUTE)) {
                        UserInterface.setAlarmText("No Alarms");
                        try {
                            // **** here ****
                            AudioPlayer playSound = new AudioPlayer(
                                    UserInterface.getSoundFile());
                            //..
                        } catch .... etc..    
                    }
                }    
            }
        }
    });   
    waiter.start();

It would make much more sense to call stop on the currently playing AudioPlayer object rather than on a dummy AudioPlayer object created within the listener. In order to get a reference to the currently playing AudioPlayer object, you need to declare it as a class field, not buried within a method and a try block. Then if you want to stop it, check that it's not null and if OK, call stop on it.


I'm going to take a stab in the dark here..

You haven't seem to have quite caught onto object oriented programming. You declare an AudioPlayer to start up your sound file, then you create a new AudioPlayer and call stop on it. So now you have AudioPlayer A playing its sound file, and AudioPlayer B which doesn't have a sound file. AudioPlayer B tries to stop playing a file that doesn't exist. This is where your NullPointerException is probably coming from.

If you want to make it work as is, change the AudioPlayer's sound file variable to be static. This is a terrible idea, do not do it.

The better idea is to save the AudioPlayer you create in the waitForAlarm method in a variable that can be accessed later in your actionPerformed method when the button is pressed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜