×

Discussion Board

Results 1 to 14 of 14
  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    30

    recordcontrol.setStream(output) throws NullPointerException

    I wrote this code and I get NullPointerException when using recordcontrol.setStream I understand that recordcontrol is null but i've checked this doc and it's exactly what they did, what am I doing wrong ? btw using JAVA ME SDK 3.4 and running this on emulator
    http://developer.nokia.com/Resources...rdControl.html
    Code:
    package audiosurf;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.microedition.media.Manager;
    import javax.microedition.media.MediaException;
    import javax.microedition.media.Player;
    import javax.microedition.media.control.RecordControl;
    
    /**
     *
     * @author Roy
     */
    public class AudioAnalyzer implements Runnable{
        
        private Player audioPlayer;
        private RecordControl recordControl;
        private InputStream inputStream;
        private ByteArrayOutputStream recordOutput;
        private byte recordData [];
        private Thread thread;
        private boolean recordFinished;
        
        /**
         * Constructor.
         */
        public AudioAnalyzer()
        {
            try {
                thread = new Thread(this);
                recordFinished = false;
                //inputStream = getClass().getResourceAsStream("res/getlucky.mpg");
                //audioPlayer = Manager.createPlayer(inputStream, "audio/mpg");
                audioPlayer = Manager.createPlayer("http://puu.sh/35YTG.mp3");
                //audioPlayer = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
                audioPlayer.realize();
                recordControl = (RecordControl)audioPlayer.getControl("RecordControl");
                if(recordControl == null){
                    System.out.println("not supported");
                }
                recordOutput = new ByteArrayOutputStream();
                recordControl.setRecordStream(recordOutput);
                recordControl.startRecord();
                audioPlayer.start();
                //thread.start();
            } catch (MediaException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        /**
         * 
         * @return the recordData; 
         */
        public byte [] getRecordData()
        {
            return recordData;
        }
        
        /**
         * 
         * @return the recordFinished; 
         */
        public boolean getRecordFinished()
        {
            return recordFinished;
        }
    
        /**
         * Checking when the player has finished playing the audio and copy the audio's recorded data into recordData.
         */
        public void run()
        {
            try {
                Thread.sleep(audioPlayer.getDuration() / 1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            try {
                recordControl.commit();
                recordData = recordOutput.toByteArray();
                recordOutput.close();
                audioPlayer.close();
                recordFinished = true;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
    }

  2. #2
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    602

    Re: recordcontrol.setStream(output) throws NullPointerException

    RecordControl is not supported for the locator http:// that's why you get a NPE
    if you want to record something you should use the file:// locator and also note that the recording will be done with AMR format.
    Heard of DVLUP? Join here

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    30

    Re: recordcontrol.setStream(output) throws NullPointerException

    Quote Originally Posted by shai.i View Post
    RecordControl is not supported for the locator http:// that's why you get a NPE
    if you want to record something you should use the file:// locator and also note that the recording will be done with AMR format.
    hey shai thanks for letting me know this, but I'm not sure how to work with the file:// locater, this is what I did
    audioPlayer = Manager.createPlayer("file:///D:/getlucky.amr"); (btw the file has to be in amr format or you ment that the record will be converted to amr ?)
    and I get IO exception "java.io.IOException: Root is not accessible"

    I'm new to J2ME it'll be helpful if you could tell me what I did wrong

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,567

    Re: recordcontrol.setStream(output) throws NullPointerException

    d: is not necessarily an existing location. Consider checking FileSystemRegistry class and its listRoots method.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    30

    Re: recordcontrol.setStream(output) throws NullPointerException

    Quote Originally Posted by wizard_hu_ View Post
    d: is not necessarily an existing location. Consider checking FileSystemRegistry class and its listRoots method.
    trying to print the list of roots just to see
    and Im getting i/o exception root is not accessible
    my main goal is to show on a list every audio file (songs) that are stored on the device how can I do that ?
    also while building my app im running it on sdk 3.4 from oracle emulator so I probably wont be able to test the app until later on real device so what can I do about it ? say I want to check the funcuallity of the method above (of obtaining all audios from device) on an emulator ? how can I do it since im testing on an emulator there're no audio files I can store on an actual device, hope you understand me, thanks in advance!

  6. #6
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    602

    Re: recordcontrol.setStream(output) throws NullPointerException

    You should never count on the root name being hardcode in your app code ("D:"),
    to obtain the list of roots available on the phone use
    FileSystemRegistry.listRoots();
    (note that some roots maybe read only and some writeable, you should check that via code)

    The file format will be AMR, also the file you are trying to write into you should create it first.

    If you target the new Asha devices (501,500,502,503) then you can use the build in File Dialog to allow the user to select a file, if not then you simply have to traverse the root and its inner directories to search for your desired media files , you can also try to use the System.property("fileconn.dir.tones") to get the path for the "tones" dir and perhaps use "fileconn.dir.memorycard" to get the memory card root if its available.

    The oracle and nokia emulators both have real paths in your pc to emulate the phone memory card so if you place a file there before you start your emulator it will be accessible from the emulator either via code or UI
    Heard of DVLUP? Join here

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    30

    Re: recordcontrol.setStream(output) throws NullPointerException

    Sorry for double posting, forgot that my posts need to be checked by moderators so I though it was a problem at first so I tried to repost, anyway waiting for answer, thanks for all the help !
    Last edited by kululu123; 2014-01-23 at 17:23.

  8. #8
    Registered User
    Join Date
    Jan 2014
    Posts
    30

    Re: recordcontrol.setStream(output) throws NullPointerException

    Quote Originally Posted by shai.i View Post
    You should never count on the root name being hardcode in your app code ("D:"),
    to obtain the list of roots available on the phone use
    FileSystemRegistry.listRoots();
    (note that some roots maybe read only and some writeable, you should check that via code)

    The file format will be AMR, also the file you are trying to write into you should create it first.

    If you target the new Asha devices (501,500,502,503) then you can use the build in File Dialog to allow the user to select a file, if not then you simply have to traverse the root and its inner directories to search for your desired media files , you can also try to use the System.property("fileconn.dir.tones") to get the path for the "tones" dir and perhaps use "fileconn.dir.memorycard" to get the memory card root if its available.

    The oracle and nokia emulators both have real paths in your pc to emulate the phone memory card so if you place a file there before you start your emulator it will be accessible from the emulator either via code or UI
    Ok used System.getProperty("fileconn.dir.tones") like you suggested now I have couple of questions and problems :
    1. ("fileconn.dir.tones") can contain folders in it ? because I want to scan the files in it to get their names if they're audio files and if it cant contain any other directories in it then i wont have to check for file extension so it'll be helpful if you could tell me !
    2. What Im trying to do is to scan every audio file in the ("fileconn.dir.tones") directory and get its name and display it on the list but since I'm running my app on emulator there wont be any files in the ("fileconn.dir.tones") directory so what im doing is im trying to write an audio file from my /resources/ folder of my project to this location and check if it works so this is what I did :

    Code:
    if (System.getProperty("microedition.io.file.FileConnection.version") != null)
            {
                try {
                    FileConnection fileConnection = (FileConnection)Connector.open(System.getProperty("fileconn.dir.tones"), Connector.READ_WRITE);
                    if (fileConnection.exists() && fileConnection.canRead() && fileConnection.canWrite())
                    {
                        System.out.println("exists");
                        fileConnection = (FileConnection)Connector.open(System.getProperty("fileconn.dir.tones") + "getlucky.amr", Connector.READ_WRITE);
                        if (!fileConnection.exists())
                        {
                            fileConnection.create();
                        }
                        DataOutputStream dos = fileConnection.openDataOutputStream();
                        DataInputStream dis = (DataInputStream)getClass().getResourceAsStream("resources/getlucky.amr");
                        dos.write(dis.read());
                        dos.flush();
                        dos.close();
                        dis.close();
    
                        fileConnection = (FileConnection)Connector.open(System.getProperty("fileconn.dir.tones"), Connector.READ_WRITE);
                        
                        Enumeration e2 = fileConnection.list();
    
                        while  (e2.hasMoreElements())
                        {
                            if (e2.toString().endsWith(".amr"))
                            {
                                append(e2.toString(), null);
                            }
                            e2.nextElement();
                        }
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    but Im getting 2 errors and NPE :
    ERROR - fileconnection - Error: javacall_fileconnection_is_directory(), cannot get file attributes

    ERROR - fileconnection - Error: javacall_fileconnection_dir_exists(), cannot access directory

    TRACE: <at java.lang.NullPointerException: 0>, startApp threw an Exception
    java.lang.NullPointerException: 0
    in this line :
    dos.write(dis.read());
    I'm sure I placed the file getlucky.amr under the /resources directory .. so whats the problem ?

  9. #9
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    602

    Re: recordcontrol.setStream(output) throws NullPointerException

    As for #1 well i'm not 100% sure but i think a user can create folders there though the default maybe it comes with out folders
    as for #2 Why arent you just copying the files directly to the emulator folder via windows explorer as i suggested before?
    Heard of DVLUP? Join here

  10. #10
    Registered User
    Join Date
    Jan 2014
    Posts
    30

    Re: recordcontrol.setStream(output) throws NullPointerException

    Quote Originally Posted by shai.i View Post
    As for #1 well i'm not 100% sure but i think a user can create folders there though the default maybe it comes with out folders
    as for #2 Why arent you just copying the files directly to the emulator folder via windows explorer as i suggested before?
    I'll have to deal with copying files to other locations later anyway so I prefer to know what I did wrong here instead of asking again later, so any idea why im getting the error ?

  11. #11
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    602

    Re: recordcontrol.setStream(output) throws NullPointerException

    Well its hard to just KNOW what is wrong by looking at your code but I think that this line
    DataInputStream dis = (DataInputStream)getClass().getResourceAsStream("resources/getlucky.amr"); returns null to you.
    first the stream should start with "/" so your string should be "/resources/getlucky.amr"
    and secondly I'm not sure u have a directory "/resource" in your project folder unless you specifically created it.
    if you just store that amr file in the res directory in eclipse then it should be "/getlucky.amr"
    that throws the NPE when you try to call the dos.write(dis.read()); (the dis object is null)
    Heard of DVLUP? Join here

  12. #12
    Registered User
    Join Date
    Jan 2014
    Posts
    30

    Re: recordcontrol.setStream(output) throws NullPointerException

    Quote Originally Posted by shai.i View Post
    edited the quote to much chars in this post
    im using netbeans and I read about someone having the same problem and someone suggested using YOURCLASS.class.getResourceAsStream(locator) instead of getClass.getResourceAsStream(locator) and it worked, but now Im faving another problem I made the list of songs scan through all songs in the notes directory and appen the name of the songs to the list, now Im trying to copy all .amr files from the /resource directory in my project to the notes directory so I made 2 FileConnection objects 1 specify the notes directory path and the second is this :
    Code:
    FileConnection resourcesSongsDirectory = (FileConnection)Connector.open(SongsList.class.getResourceAsStream("/resources/songs").toString(), Connector.READ);
    and then I use enumeration to scan through the .amr files in the resource directory and copy them all to the notes directory problem is that line (the line I initiailize the resourceSongsDirectory) return NPE and Im 100% sure the path is correct, also I tried to do something else because I experienced this problem (instead of using FileConnector for the songs in the resource folder I just passed hard coded path for the datainputstream to check if it works [the copying of the file to the notes directory]) so after I did it, it worked and then I tried to create a player and play this song :
    Code:
    inputStream = AudioAnalyzer.class.getClass().getResourceAsStream("/resources/songs/getlucky.amr");
    but even this gave me NPE

    Here are the 2 classes that I'm having problems with :
    Code:
    package core;
    
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.util.Enumeration;
    import javax.microedition.io.Connector;
    import javax.microedition.io.file.FileConnection;
    import javax.microedition.io.file.FileSystemRegistry;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.List;
    
    /**
     *
     * @author Roy
     */
    public class SongsList extends List {
        
        private Command selectCommand;
        private Command exitCommand;
        
        /**
         * Constructor.
         * @param midlet 
         */
        public SongsList(Midlet midlet)
        {
            super("Songs", List.IMPLICIT);
            //Enumeration e1 = FileSystemRegistry.listRoots();
            
            // Checking if the device is supporting the FileConnection API.
            if (System.getProperty("microedition.io.file.FileConnection.version") != null)
            {
                try {
                    FileConnection tonesDirectory = (FileConnection)Connector.open(System.getProperty("fileconn.dir.tones"), Connector.READ_WRITE);
                    FileConnection resourcesSongsDirectory = (FileConnection)Connector.open(SongsList.class.getResourceAsStream("/resources/songs").toString(), Connector.READ);
                    if (tonesDirectory.exists())
                    {
                        if (!(tonesDirectory.canRead() && tonesDirectory.canWrite()))
                        {
                            tonesDirectory.canRead();
                            tonesDirectory.canWrite();
                        }
                        Enumeration e1 = resourcesSongsDirectory.list("*.amr", false);
                        DataOutputStream dos = null;
                        DataInputStream dis = null;
                        String songName;
                        while (e1.hasMoreElements())
                        {
                            songName = e1.nextElement().toString();
                            tonesDirectory = (FileConnection)Connector.open(System.getProperty("fileconn.dir.tones") + songName, Connector.READ_WRITE);
                            if (!tonesDirectory.exists())
                            {
                                tonesDirectory.create();
                            }
                            dos = tonesDirectory.openDataOutputStream();
                            dis = new DataInputStream(SongsList.class.getResourceAsStream("/resources/songs" + songName));
                            dos.write(dis.read());
                        }
                        if (dos != null)
                        {
                            dos.flush();
                            dos.close();
                        }
                        if (dis != null)
                        {
                            dis.close();
                        }
                        resourcesSongsDirectory.close();
    
    //                    DataOutputStream dos;
    //                    DataInputStream dis;
    //                    tonesDirectory = (FileConnection)Connector.open(System.getProperty("fileconn.dir.tones") + "getlucky.amr", Connector.READ_WRITE);
    //                    if (!tonesDirectory.exists())
    //                    {
    //                        tonesDirectory.create();
    //                        dos = tonesDirectory.openDataOutputStream();
    //                        dis = new DataInputStream(SongsList.class.getResourceAsStream("/resources/getlucky.amr"));
    //                        dos.write(dis.read());
    //                        dos.flush();
    //                        dos.close();
    //                        dis.close();
    //                    }
    
                        tonesDirectory = (FileConnection)Connector.open(System.getProperty("fileconn.dir.tones"), Connector.READ_WRITE);
                        
                        e1 = tonesDirectory.list("*.amr", false);
                        tonesDirectory.close();
    
                        while  (e1.hasMoreElements())
                        {
                            String str = e1.nextElement().toString();
                            str = str.substring(0, str.indexOf(".amr"));
                            append(str, null);
                        }
                    }
                    else // what to do if notes directory doesnt exists
                    {
                        
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            selectCommand = new Command("Select", Command.OK, 0);
            exitCommand = new Command("Exit", Command.EXIT, 0);
            addCommand(selectCommand);
            addCommand(exitCommand);
            setSelectCommand(selectCommand);
            setCommandListener(midlet);
        }
    
        /**
         * @return the selectCommand.
         */
        public Command getSelectCommand()
        {
            return selectCommand;
        }
    
        /**
         * @param selectCommand the selectCommand to set.
         */
        public void setSelectCommand(Command selectCommand)
        {
            this.selectCommand = selectCommand;
        }
    
        /**
         * @return the exitCommand.
         */
        public Command getExitCommand() {
            return exitCommand;
        }
    
        /**
         * @param exitCommand the exitCommand to set.
         */
        public void setExitCommand(Command exitCommand)
        {
            this.exitCommand = exitCommand;
        }
        
    }
    Code:
    package core;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.microedition.io.file.FileSystemRegistry;
    import javax.microedition.media.Manager;
    import javax.microedition.media.MediaException;
    import javax.microedition.media.Player;
    import javax.microedition.media.control.RecordControl;
    
    /**
     *
     * @author Roy
     */
    public class AudioAnalyzer implements Runnable{
        
        private Player audioPlayer;
        private RecordControl recordControl;
        private InputStream inputStream;
        private ByteArrayOutputStream recordOutput;
        private byte recordData [];
        private Thread thread;
        private boolean recordFinished;
        
        /**
         * Constructor.
         */
        public AudioAnalyzer()
        {
            try {
                thread = new Thread(this);
                recordFinished = false;
               
                inputStream = AudioAnalyzer.class.getClass().getResourceAsStream("/resources/songs/getlucky.amr");
                audioPlayer = Manager.createPlayer(inputStream, "audio/amr");
                //audioPlayer = Manager.createPlayer("http://puu.sh/35YTG.mp3");
                //audioPlayer = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
                audioPlayer.realize();
                recordControl = (RecordControl)audioPlayer.getControl("RecordControl");
                recordOutput = new ByteArrayOutputStream();
                recordControl.setRecordStream(recordOutput);
                recordControl.startRecord();
                audioPlayer.start();
                //thread.start();
            } catch (MediaException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        /**
         * 
         * @return the recordData; 
         */
        public byte [] getRecordData()
        {
            return recordData;
        }
        
        /**
         * 
         * @return the recordFinished; 
         */
        public boolean getRecordFinished()
        {
            return recordFinished;
        }
    
        /**
         * Checking when the player has finished playing the audio and copy the audio's recorded data into recordData.
         */
        public void run()
        {
            try {
                Thread.sleep(audioPlayer.getDuration() / 1000 + 5000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            try {
                recordControl.commit();
                recordData = recordOutput.toByteArray();
                recordOutput.close();
                audioPlayer.close();
                recordFinished = true;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
    }

  13. #13
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    602

    Re: recordcontrol.setStream(output) throws NullPointerException

    FileConnection is NOT used to open local files inside your jar but only to work with files on the device file system.
    You should use inputStream = AudioAnalyzer.class.getClass().getResourceAsStream("/resources/songs/getlucky.amr"); (BUT AGAIN I don't think you need the resources/ directory since I don't think you have that in your final jar but only /songs/getlucky.amr but I cannot be sure, try posting your jar so I can look at it and help with you with the pathing)
    and then you can just read the inputstream and write it to the file outputstream.

    Once again just to fully iterate what I already said this line
    FileConnection resourcesSongsDirectory = (FileConnection)Connector.open(SongsList.class.getResourceAsStream("/resources/songs").toString(), Connector.READ);
    is completely wrong since
    A. its a local jar resource
    B. you try to get the inputstream toString() description which is just bad.
    Heard of DVLUP? Join here

  14. #14
    Registered User
    Join Date
    Jan 2014
    Posts
    30

    Re: recordcontrol.setStream(output) throws NullPointerException

    Quote Originally Posted by shai.i View Post
    FileConnection is NOT used to open local files inside your jar but only to work with files on the device file system.
    You should use inputStream = AudioAnalyzer.class.getClass().getResourceAsStream("/resources/songs/getlucky.amr"); (BUT AGAIN I don't think you need the resources/ directory since I don't think you have that in your final jar but only /songs/getlucky.amr but I cannot be sure, try posting your jar so I can look at it and help with you with the pathing)
    and then you can just read the inputstream and write it to the file outputstream.

    Once again just to fully iterate what I already said this line
    FileConnection resourcesSongsDirectory = (FileConnection)Connector.open(SongsList.class.getResourceAsStream("/resources/songs").toString(), Connector.READ);
    is completely wrong since
    A. its a local jar resource
    B. you try to get the inputstream toString() description which is just bad.
    EDIT : I abandoned this method of recording the audio because I realized I don't need it, sorry for all the trouble I made
    my original intentions were to analyse an audio file to obtain its frequency and amplitude so I didnt need to record the audio file to get its binary data (my fault .. ), I read some about it and the only examples I saw were being done with javax.sound library, problem is you cant use this library with j2me any suggestion on how to get frequency and amplitude of an audio file then ?
    I stored its binary data in byte array now what


    ok thanks ! so now about the player problem Im getting another problem not NPE I got mediaexception unable to create native player when trying to realize the player so I thought maybe the content type Im using (amr) isnt supported with the protocol im using so I tried to print the supported protocoles for the content types and this was the output :
    video/3gpp: http
    video/3gpp: https
    audio/mp3: http
    audio/mp3: https
    video/mpeg4: http
    video/mpeg4: https
    audio/x-midi: http
    audio/x-midi: https
    audio/x-tone-seq: http
    audio/x-tone-seq: file
    audio/x-tone-seq: device
    audio/x-tone-seq: https
    image/gif: http
    image/gif: file
    image/gif: https
    audio/midi: http
    audio/midi: file
    audio/midi: device
    audio/midi: https
    audio/mid: http
    audio/mid: https
    audio/x-wav: capture
    audio/x-wav: http
    audio/x-wav: file
    audio/x-wav: https
    audio/wav: http
    audio/wav: https
    audio/tone: http
    audio/tone: https
    audio/mpeg: http
    audio/mpeg: file
    audio/mpeg: https
    image/jpeg: capture
    audio/amr: http
    audio/amr: file
    audio/amr: https
    video/3gp: capture
    video/3gp: http
    video/3gp: file
    video/3gp: https
    video/mp4: http
    video/mp4: file
    video/mp4: https
    audio/sp-midi: http
    audio/sp-midi: file
    audio/sp-midi: https
    now Im not sure what protocol I'm looking for exactly (I need to record the played audio file so what am I looking for exactly?)
    Last edited by kululu123; 2014-02-03 at 18:09.

Similar Threads

  1. getProductData throws nullPointerException
    By preeti.s.patil in forum Monetization APIs
    Replies: 1
    Last Post: 2012-04-13, 09:02
  2. *Sometimes* getSnapshot throws MediaException, RecordControl Error
    By olan75 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2011-04-03, 16:11
  3. getLocation always throws NullPointerException
    By Wurstgesicht in forum Geolocation and Navigation
    Replies: 5
    Last Post: 2009-03-20, 13:08
  4. Using RecordStore.AUTHMODE_ANY throws NullPointerException
    By sgaber in forum Mobile Java General
    Replies: 10
    Last Post: 2007-12-18, 18:03
  5. recordcontrol.setStream(out) throws NullPointerException
    By bharatdevada in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2006-11-10, 01:16

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •