Solved

AudioClip : NullPointerException

Posted on 1997-05-09
4
362 Views
Last Modified: 2008-03-17
I use jdk 1.0.2 under win95. I use a File object from an application. I use then AudioClip with the File as parameter. The compilation runs well, but when the running, a NullPointerException comes. My File is not null, and the file exists really on my hard disk.  What is the problem ??
Thanks to answer
0
Comment
Question by:breizh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220310
Are you sing code that looks like:

FileInputStream Myfile;
...
play(getCodeBAse(), MyFile);


If you are, then your problem is that you are sending a File class to the play command and not a String containing the name of your file.

If not, could you post the relevant parts of your code?
0
 

Author Comment

by:breizh
ID: 1220311
Yes, ok for an applet. But the method getCodeBase() does not work in an application ! How can I do this ? My code is :

URL son = new URL ("file:///C:/musique.au");
Audioclip clip = getAudioClip(son);

It is at this last line that the error appears:
java.Lang.NullPointerException at ...getAppletContext.

Thanks to answer me.

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220312
from comp.lang.java, Bill Tschumy posted

In order to create a valid Applet when running standalone you need to implement AppletStub and AppletContext. I have not see any code that does this completely enough to get a valid AudioClip. To my knowledge there is no way to play sound from an application without usingthe undocumented (and semi-forbidden) sun.* classes.
 
Now there is a possible (though unsatisfactoy) work arround.

You could create your application as an applet and then convert it to an application.

0
 

Accepted Solution

by:
jpzwarte earned 100 total points
ID: 1220313
It isn't possible to play audio from a 'normal' application but you CAN do it, like this:

import java.io.*;
import sun.audio.*;

class TestAudio
{
    public static void main(String args[])
    {
        FileInputStream fis_sound;
        int i;

        try
        {
            fis_sound = new FileInputStream(args[0]);
            System.out.println("Playing sound...");
            AudioPlayer.player.start(fis_sound);
            while (true)
            {
            }
        }
        catch (Exception e)
           System.out.println(e);
    }
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question