×

Discussion Board

Results 1 to 13 of 13
  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    45

    "public class" is not abstract and does not override abstract method commandAction

    Hi,

    My project consist in a client MIDlet that connects with a server, and a public class written in a separate file that implements a voice recorder.
    There is no problem with the MIDlet, but the recorder cannot be compiled, because it has some errors.

    First error appears when I declare the public class "grabadora"(means recorder):
    "public class" is not abstract and does not override abstract method commandAction(javax.microedition.lcdui.Command,javax.microedition.lcdui.Displayable)in javax.microedition.lcdui.CommandListener


    One of my problems is I don't know how to build a project in different files correctly. Where can I find some info about that?

    My code is:




    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.Display.*;
    import javax.microedition.media.*;
    import javax.microedition.media.control.*;
    import javax.microedition.io.file.*;
    import javax.microedition.io.*;
    import java.io.*;

    public class grabadora implements javax.microedition.lcdui.CommandListener, PlayerListener {
    ByteArrayOutputStream outputStream =null;
    byte[] byteArray = null;
    String locator= "capture://audio";
    Player player =null;
    Form form;
    TextField tb = null;
    Command recCommand, stopRecCommand, playCommand,exitCommand;
    Display display=null;

    RecordControl rc=null;
    ChoiceGroup cg;
    /** Creates a new instance of HelloMidlet */
    public grabadora() { //constructor

    cg= new ChoiceGroup ("Record to", ChoiceGroup.EXCLUSIVE);
    cg.append("file",null);
    cg.append("byteArray", null);
    cg.setSelectedIndex(1, true);
    form = new Form("Audio Capture");

    recCommand = new Command("record", Command.OK, 1);
    stopRecCommand = new Command("stop record", Command.OK, 1);
    playCommand = new Command("Play", Command.SCREEN, 2);
    exitCommand = new Command("Exit", Command.SCREEN, 2);

    tb = new TextField("SoundCapture", "info", 256, TextField.UNEDITABLE) ;
    form.addCommand(recCommand);
    form.addCommand(playCommand);
    form.addCommand(exitCommand);
    form.setCommandListener(this);

    form.append(tb);
    form.append(cg);
    display.setCurrent(form); //Lo he puesto yo

    }
    public void commandAction(Command c, Displayable s) {

    // Making a boolean vector to get the options from the choicegroup
    boolean flags[] = new boolean[2];
    flags[0]=false;
    flags[1]=false;

    cg.getSelectedFlags(flags);
    if (c == recCommand) // start the recording
    {
    System.out.println("recCommand");
    tb.setString("Recorder started:");
    // Changing the recCommand with the stopCommand
    form.removeCommand(recCommand);
    form.addCommand(stopRecCommand);

    try{
    // creating the player from the file url enabling progressive download
    player = Manager.createPlayer(locator);
    // adding playerListenter
    player.addPlayerListener(this);
    // allocating the resources from the microphone
    player.realize();
    // Getting the ReocrdControll so we can record the output from the player
    rc = (RecordControl)player.getControl("RecordControl");
    }
    catch(Exception er){tb.setString(er.toString());}

    if(flags[0]) // If we are saving the recorde audio to file
    {
    try{
    // setting the record location to the file
    rc.setRecordLocation("file:///c:/other/audio.amr");
    // start recording
    rc.startRecord();
    // start to play the sound from the microphone
    player.start();
    }catch(Exception t){tb.setString(t.toString());}
    }
    else // If we are saving to an OutputStream
    {
    outputStream = null;
    outputStream = new ByteArrayOutputStream();

    if(rc==null)
    System.out.println("rc==null");
    // setting the record location to the outputstream
    rc.setRecordStream(outputStream);

    try{
    // start recording
    rc.startRecord();
    // start to play the sound from the microphone
    player.start();

    }
    catch(Exception e){tb.setString(e.toString());}

    }


    }
    else if (c == stopRecCommand) // Stop the recording
    {
    System.out.println("StoprecCommand");
    // Changing the stopCommand with recCommand
    form.removeCommand(stopRecCommand);
    form.addCommand(recCommand);
    try{

    // stop recording
    rc.commit();
    // and stop the playback from the microphone
    player.close();

    tb.setString("Recorder to byteArray stopped:");
    }
    catch(Exception e){tb.setString(e.toString());}
    }
    else if (c == playCommand) // start the playback of the recorded audio
    {

    if(flags[0]) // to File
    {
    System.out.println("play from File");
    // creating and starting the playback thread
    new PlayerThread("file:///c:/other/audio.amr").start();

    }
    else // to outputStream
    {
    System.out.println("play from ByteArrayInputStream");
    if(outputStream==null)
    return;
    // getting the ByteArray from the ByteArrayOutputStream
    byteArray = outputStream.toByteArray();
    // Creating and starting the playback thread
    new PlayerThread(new ByteArrayInputStream(byteArray), "audio/amr" ).start();

    }
    tb.setString("Playing ");
    }
    else if (c == exitCommand)
    {

    }

    }

    public void playerUpdate(Player player,java.lang.String event,java.lang.Object eventData)
    {
    System.out.println("playerUpdate event="+event);

    }

    }






    Any suggestions?, thanks a million
    Last edited by werter10; 2008-12-09 at 11:32.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: "public class" is not abstract and does not override abstract method commandActio

    Hmmmm... looks OK to me... my first guess is that you have something unusual in your class path.

    What is your classpath when compiling?

    What are the names of the other classes in your application?

  3. #3
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: "public class" is not abstract and does not override abstract method commandActio

    Hi,
    Just read this paragraph,

    An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signatures and constant declarations (variable declarations which are declared to be both static and final). An interface may never contain method definitions.

    As interfaces are implicitly abstract, they cannot be directly instantiated. Object references in Java may be specified to be of an interface type; in which case they must either be null, or be bound to an object which implements the interface.

    The keyword implements is used to declare that a given class implements an interface. A class which implements an interface must either implement all methods in the interface, or be an abstract class.
    One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java (other than java.lang.Object, the root class of the Java type system) must have exactly one base class; multiple inheritance of classes is not allowed. However, a Java class/interface may implement/extend any number of interfaces.

    Therefore you have to implement the CommandAction() method of the interface CommandListener

    Hope these lines can help you,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: "public class" is not abstract and does not override abstract method commandActio

    raj... he knows that... see the line:

    Code:
    public void commandAction(Command c, Displayable s) {
    in the code he posted.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    45

    Re: "public class" is not abstract and does not override abstract method commandActio

    Quote Originally Posted by grahamhughes View Post
    Hmmmm... looks OK to me... my first guess is that you have something unusual in your class path.

    What is your classpath when compiling?

    What are the names of the other classes in your application?

    Hi, thanks for your fast reply.

    I have three different files in the source packages: Echoclient.java (MIDlet), grabadora.java (voice recorder), PlayerThread.java.
    Grabadora works fine if I compile as a MIDlet, but I think I made a mistake when I modified to convert it to a public class for my application.

    thanks

  6. #6
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: "public class" is not abstract and does not override abstract method commandActio

    Hi,
    Well, the same theory applicable for the abstract classes and other interfaces..I guess he has miss some implementation..against either the abstract class or interface,

    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  7. #7
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: "public class" is not abstract and does not override abstract method commandActio

    My first thought is that there is another class called "Command" in your classpath, and that the parameter "c" in commandAction() is being seen as the wrong class (and so does not implement the interface correctly).

    Did you need to fully qualify the name of CommandListener?

    Check through the imports (get rid of import javax.microedition.lcdui.Display.* for a start).

    Check the class path... Are you compiling from an IDE, or the command line?

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    45

    Re: "public class" is not abstract and does not override abstract method commandActio

    Quote Originally Posted by grahamhughes View Post
    My first thought is that there is another class called "Command" in your classpath, and that the parameter "c" in commandAction() is being seen as the wrong class (and so does not implement the interface correctly).

    Did you need to fully qualify the name of CommandListener?

    Check through the imports (get rid of import javax.microedition.lcdui.Display.* for a start).

    Check the class path... Are you compiling from an IDE, or the command line?
    hi,
    well there is another commandAction implemented in the MIDlet,(not in grabadora class) but I thought there wasn't any problem.
    I'm compiling from NeatBeans IDE. When you say the classpath, do you mean the system classpath or the path of grabadora public class? How should I check the classpath?. Fully qualify?
    I'm new in this stuff, I'm sorry.

    I'm explaining the whole project better now. I have a MIDlet wich connects via Bluetooth to a computer, when I accomplished that, I wrote a public class in another file to record some voice and trying to figure out how to send it to the computer. I though about using another file for the recorder, with it own commands for recording and stopping.

    Any other idea to structure the project?

    Thank you very much

  9. #9
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: "public class" is not abstract and does not override abstract method commandActio

    No problem... quick Java lesson...

    A fully qualified class name ("FQN") is like:

    Code:
    javax.microedition.lcdui.Command
    So, the complete name of the class, including the package name. When you specify a simple class name (like Command), the compiler uses the import directives at the head of the source code to find the correct package. "java.lang.*" is imported automatically. The compiler must find the class in exactly one package. The order of the imports makes no difference.

    The classpath is different. This is basically a list of directories, JAR or ZIP files, which the compiler uses to find the actual .class files. For example, your code refers to:

    Code:
    javax.microedition.lcdui.Command
    so the compiler must find the file:

    Code:
    javax\microedition\lcdui\Command.class
    somewhere. It will search the directories and JAR/ZIP files in the classpath (in order) until it finds this file. If there are two such files in the path, the compiler finds the first, so the order here is important.

    Back to your code...

    I tried to compile it, and it's fine. Here's what I did.

    There is a reference in the code to PlayerThread. I don't have this, so I created a dummy class, like this:

    Code:
    import java.io.*;
    
    public class PlayerThread extends Thread {
        public PlayerThread(String s) {
        }
        public PlayerThread(InputStream in, String s) {
        }
    }
    This is just enough to make the grabadora class compile.

    Then I run the compiler, from the command line.

    Code:
    javac -classpath .;c:\WTK2.5.2\lib\cldcapi11.jar;c:\WTK2.5.2\lib\midpapi20.jar;c:\WTK2.5.2\lib\mmapi.jar;c:\WTK2.5.2\lib\jsr75.jar grabadora.java
    In the classpath, I've listed the JAR files that contain the API classes needed for this application. The ".;" at the start of the path makes sure the compiler looks in the current directory (where grabadora.java is) first.

    It compiles fine. So, there is not a fault in grabadora.java.

    Possibly, this is not the class it is complaining about. I don't use NetBeans so I'm not sure what evil it might be doing. Try compiling from the command line, so you can control exactly what is going on.

    Any help?

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    45

    Re: "public class" is not abstract and does not override abstract method commandActio

    Quote Originally Posted by grahamhughes View Post
    No problem... quick Java lesson...

    A fully qualified class name ("FQN") is like:

    Code:
    javax.microedition.lcdui.Command
    So, the complete name of the class, including the package name. When you specify a simple class name (like Command), the compiler uses the import directives at the head of the source code to find the correct package. "java.lang.*" is imported automatically. The compiler must find the class in exactly one package. The order of the imports makes no difference.

    The classpath is different. This is basically a list of directories, JAR or ZIP files, which the compiler uses to find the actual .class files. For example, your code refers to:

    Code:
    javax.microedition.lcdui.Command
    so the compiler must find the file:

    Code:
    javax\microedition\lcdui\Command.class
    somewhere. It will search the directories and JAR/ZIP files in the classpath (in order) until it finds this file. If there are two such files in the path, the compiler finds the first, so the order here is important.

    Back to your code...

    I tried to compile it, and it's fine. Here's what I did.

    There is a reference in the code to PlayerThread. I don't have this, so I created a dummy class, like this:

    Code:
    import java.io.*;
    
    public class PlayerThread extends Thread {
        public PlayerThread(String s) {
        }
        public PlayerThread(InputStream in, String s) {
        }
    }
    This is just enough to make the grabadora class compile.

    Then I run the compiler, from the command line.

    Code:
    javac -classpath .;c:\WTK2.5.2\lib\cldcapi11.jar;c:\WTK2.5.2\lib\midpapi20.jar;c:\WTK2.5.2\lib\mmapi.jar;c:\WTK2.5.2\lib\jsr75.jar grabadora.java
    In the classpath, I've listed the JAR files that contain the API classes needed for this application. The ".;" at the start of the path makes sure the compiler looks in the current directory (where grabadora.java is) first.

    It compiles fine. So, there is not a fault in grabadora.java.

    Possibly, this is not the class it is complaining about. I don't use NetBeans so I'm not sure what evil it might be doing. Try compiling from the command line, so you can control exactly what is going on.

    Any help?

    Hi, thanks.
    Well, I tried to compile setting in the path one of my JAR files, just to try but it doesn't work. I did:
    javac -classpath .;c:\Program Files\NetBeans 6.1\mobility8\WTK2.5.2\lib\jsr75.jar grabadora.java

    You can read afterwards javac :invalid flag: Files\NetBeans
    usage: javac<options> <source files>

    The code of grabadora comes from a MIDlet which I adapted to become a class, it wrks as a MIDlet but I did something wrong...

    Thanks anyway, I write the original code, just in case...

    package hello;

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.media.*;
    import javax.microedition.media.control.*;
    import javax.microedition.io.file.*;
    import javax.microedition.io.*;

    import java.io.*;
    /**
    *
    * @author 23049734
    */
    public class SoundCaptureMIdlet extends MIDlet implements javax.microedition.lcdui.CommandListener, PlayerListener {
    ByteArrayOutputStream outputStream =null;
    byte[] byteArray = null;
    String locator= "capture://audio";
    Player player =null;
    Form form;
    TextField tb = null;
    Command recCommand, stopRecCommand, playCommand,exitCommand;
    Display display=null;

    RecordControl rc=null;
    ChoiceGroup cg;
    /** Creates a new instance of HelloMidlet */
    public SoundCaptureMIdlet () {

    cg= new ChoiceGroup ("Record to", ChoiceGroup.EXCLUSIVE);
    cg.append("file",null);
    cg.append("byteArray", null);
    cg.setSelectedIndex(1, true);
    form = new Form("Audio Capture");

    recCommand = new Command("record", Command.OK, 1);
    stopRecCommand = new Command("stop record", Command.OK, 1);
    playCommand = new Command("Play", Command.SCREEN, 2);
    exitCommand = new Command("Exit", Command.SCREEN, 2);

    tb = new TextField("SoundCapture", "info", 256, TextField.UNEDITABLE) ;
    form.addCommand(recCommand);
    form.addCommand(playCommand);
    form.addCommand(exitCommand);
    form.setCommandListener(this);

    form.append(tb);
    form.append(cg);


    }

    // --- This section is auto-generated by NetBeans IDE. Do not modify or you may lose your changes.//<editor-fold id="MVDMethods" defaultstate="collapsed" desc="This section is auto-generated by NetBeans IDE.">//GEN-BEGIN:MVDMethods
    /**
    * This method initializes UI of the application.
    */
    private void initialize() {
    }

    // --- This is the end of auto-generated section.//</editor-fold>//GEN-END:MVDMethods

    public void startApp() {
    if(display==null)
    display=Display.getDisplay(this);
    display.setCurrent(form);
    initialize();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }



    public void commandAction(Command c, Displayable s) {

    // Making a boolean vector to get the options from the choicegroup
    boolean flags[] = new boolean[2];
    flags[0]=false;
    flags[1]=false;

    cg.getSelectedFlags(flags);
    if (c == recCommand) // start the recording
    {
    System.out.println("recCommand");
    tb.setString("Recorder started:");
    // Changing the recCommand with the stopCommand
    form.removeCommand(recCommand);
    form.addCommand(stopRecCommand);

    try{
    // creating the player from the file url enabling progressive download
    player = Manager.createPlayer(locator);
    // adding playerListenter
    player.addPlayerListener(this);
    // allocating the resources from the microphone
    player.realize();
    // Getting the ReocrdControll so we can record the output from the player
    rc = (RecordControl)player.getControl("RecordControl");
    }
    catch(Exception er){tb.setString(er.toString());}

    if(flags[0]) // If we are saving the recorde audio to file
    {
    try{
    // setting the record location to the file
    rc.setRecordLocation("file:///c:/other/audio.amr");
    // start recording
    rc.startRecord();
    // start to play the sound from the microphone
    player.start();
    }catch(Exception t){tb.setString(t.toString());}
    }
    else // If we are saving to an OutputStream
    {
    outputStream = null;
    outputStream = new ByteArrayOutputStream();

    if(rc==null)
    System.out.println("rc==null");
    // setting the record location to the outputstream
    rc.setRecordStream(outputStream);

    try{
    // start recording
    rc.startRecord();
    // start to play the sound from the microphone
    // player.start();

    }
    catch(Exception e){tb.setString(e.toString());}

    }


    }
    else if (c == stopRecCommand) // Stop the recording
    {
    System.out.println("StoprecCommand");
    // Changing the stopCommand with recCommand
    form.removeCommand(stopRecCommand);
    form.addCommand(recCommand);
    try{

    // stop recording
    rc.commit();
    // and stop the playback from the microphone
    player.close();

    tb.setString("Recorder to byteArray stopped:");
    }
    catch(Exception e){tb.setString(e.toString());}
    }
    else if (c == playCommand) // start the playback of the recorded audio
    {

    if(flags[0]) // to File
    {
    System.out.println("play from File");
    // creating and starting the playback thread
    new PlayerThread("file:///c:/other/audio.amr").start();

    }
    else // to outputStream
    {
    System.out.println("play from ByteArrayInputStream");
    if(outputStream==null)
    return;
    // getting the ByteArray from the ByteArrayOutputStream
    byteArray = outputStream.toByteArray();
    // Creating and starting the playback thread
    new PlayerThread(new ByteArrayInputStream(byteArray), "audio/amr" ).start();

    }
    tb.setString("Playing ");
    }
    else if (c == exitCommand)
    {
    notifyDestroyed();
    }

    }

    public void playerUpdate(Player player,java.lang.String event,java.lang.Object eventData) {
    System.out.println("playerUpdate event="+event);

    }

    }

  11. #11
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: "public class" is not abstract and does not override abstract method commandActio

    Ahhh... you have spaces in your path name.

    You can't write:

    Code:
    javac -classpath .;c:\Program Files\NetBeans 6.1\mobility8\WTK2.5.2\lib\jsr75.jar grabadora.java
    If there are spaces, it has to have quotes around the entire classpath, like:

    Code:
    javac -classpath ".;c:\Program Files\NetBeans 6.1\mobility8\WTK2.5.2\lib\jsr75.jar" grabadora.java
    I suggest you avoid installing the WTK in a folder with spaces in the path name.

    Also, you need all the JARs for all the APIs, so you need the CLDC and MIDP JARs too.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    Re: "public class" is not abstract and does not override abstract method commandActio

    It's complaining because you have declared but failed to override one of the abstract methods. I'm not that familiar with wtk, but there are several inherent commands, like OK, EXIT, and so on. If you declare them as commands, you also must override them somewhere in the code. That is all this error is about.

    For instance by declaring "static final Command ExitCommand = new Command("Exit", Command.EXIT, 0);"

    it must have a method such like:

    public void commandAction(Command c, Displayable s)
    {
    if (c.getCommandType( ) == Command.EXIT)
    {
    destroyApp(true);
    notifyDestroyed( );
    }
    }

    or you will get that exact same error message.

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    Re: "public class" is not abstract and does not override abstract method commandActio

    I had encountered same problem.

    The cause was that one of the abstract methods in abstract class hadn't got public modificator, so my subclass that extended that abstract class seemed to implement all methods, but didn't have rights to do it.
    Even when subclass was placed in same package as its parent, the compiler still throws errors (why? no modificator means package access, I think...).

    So, the solution in my case was — to add public modificator to abstract method.

Posting Permissions

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