0

I created A tictactoe (creategame class) game on my mac, uploaded it to GitHub, downloaded on windows 7. Then this error occurred while running the eclipse project. On researching, I think its a software/driver issue or it's not detecting the sound files, as a beginner, I am completely stumped. Thanks guys for your help.

> Error with playing sound.
java.lang.NullPointerException
    at javax.sound.sampled.AudioSystem.getDefaultMixer(Unknown Source)
    at javax.sound.sampled.AudioSystem.getLine(Unknown Source)
    at TicTacToe.CreateGame.playSound(CreateGame.java:144)
    at TicTacToe.CreateGame.actionPerformed(CreateGame.java:194)
    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.plaf.basic.BasicButtonListener.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.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(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)


    public void playSound() {
//       Load sounds


            try {
                AudioInputStream A = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/A3_04.wav"));
                AudioInputStream Bb = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/Bb4_01.wav"));
                AudioInputStream C = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/C5_01.wav"));
                AudioInputStream D = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/D4_01.wav"));
                AudioInputStream E = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/E4_01.wav"));
                AudioInputStream F = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/F4_01.wav"));
                AudioInputStream G = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("/G4_01.wav"));




    //          IR and IR2 = random number max 7

                AudioInputStream[] Sounds = { C, D, E, F, G, A, Bb };

                Clip clip = AudioSystem.getClip();
                Clip clip2 = AudioSystem.getClip();

                clip.open(Sounds[IR]);
                clip2.open(Sounds[IR2]);

                clip.start();
                clip2.start();

            } catch (Exception ex) {
                System.out.println("Error with playing sound.");
                ex.printStackTrace();

            }
        }

Screenshot of eclipse

Thank you very much for your help :P

  • `int IR = (int) (Math.random() * 6);` will never make 6 and it won't for **IR2** either. Use **7** instead of **6**. To make sure **IR2** will never equal **IR**: `int ir = (int) (Math.random() * 7); int ir2 = ir; while (ir == ir2) { ir2 = (int) (Math.random() * 7); }`. For the **NullPointerException**: I don't think it can find a Mixer when: `Clip clip = AudioSystem.getClip();` is called. – DevilsHnd Apr 06 '20 at 21:43
  • Thanks, @DevilsHnd, I reuploaded this as I think I had bypassed the IR + IR2 . but I will update the "7". I think your right and the Mixer cant find it, but I don't think its a coding issue and that my computer has software issues with its audio. new error in this link https://stackoverflow.com/review/suggested-edits/25791791 null pointer I can solve now. "javax.sound.sampled.LineUnavailableException" I can't find this fix – Samuel Morgan-Tyghe Apr 07 '20 at 09:17
  • It's fixed , hardware issues. – Samuel Morgan-Tyghe Apr 07 '20 at 19:36
  • Nice to hear you sorted it out. :) – DevilsHnd Apr 08 '20 at 00:00

0 Answers0