Hello,
I'm learning Java ME and when i complete the development of an application that uses RecordStore, it's simple a contact book, but when i completed the code i see that my program have an error and i use Eclipse, then it marks wrong codes in the left of the line with an X. Here is the error description:

The type simplecontacts must implement the inherited abstract method CommandListener.commandAction(Command, Displayable)

Here is the code of my program:

package contacts;
import java.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.*;
import javax.microedition.lcdui.*;

public class simplecontacts extends MIDlet implements CommandListener, ItemStateListener {
	private Display display;
	private Alert alerta;
	private Form telainicial, adicionaReg;
	private TextField nome, telefone;
	private Command sair, adicionar, voltar, salvar, zerar;
	private RecordStore rs = null;
	String tipo, mensagem;
	boolean gravacaoOK = false;
	private ChoiceGroup tipo_contato;
	private String[] listaTipo = {"Amigos", "Família", "Trabalho"};
	
	public simplecontacts() {
		display = Display.getDisplay(this);
		sair = new Command("Sair", Command.EXIT, 0);
		adicionar = new Command("Adicionar Registros", Command.SCREEN, 1);
		zerar = new Command("Zerar Registros", Command.SCREEN, 1);
		voltar = new Command("Voltar", Command.SCREEN, 1);
		salvar = new Command("Salvar Registro", Command.SCREEN, 1);
		nome = new TextField("Nome:", "", 20, TextField.ANY);
		telefone = new TextField("Telefone:", "", 10, TextField.PHONENUMBER);
		
		telainicial = new Form("Simple Contacts");
		telainicial.addCommand(sair);
		telainicial.addCommand(adicionar);
		telainicial.addCommand(zerar);
		telainicial.setCommandListener(this);
		adicionaReg = new Form("Adiciona Contato");
		tipo_contato = new ChoiceGroup("Tipo do Contato", Choice.POPUP, listaTipo, null);
		adicionaReg.append(nome);
		adicionaReg.append(telefone);
		adicionaReg.append(tipo_contato);
		adicionaReg.addCommand(salvar);
		adicionaReg.addCommand(voltar);
		adicionaReg.setItemStateListener(this);
		adicionaReg.setCommandListener(this);
	}

	protected void destroyApp(boolean arg0) {
		notifyDestroyed();

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		try {
			rs = RecordStore.openRecordStore("SimpleContacts", true);
		} catch (Exception exc) {
			mostrarAlerta("Erro ao abrir o RecordStore -", exc.toString());
		}
		display.setCurrent(telainicial);
	}
	
	private void mostrarAlerta(String tipoAlerta, String msg) {
		alerta = new Alert(tipoAlerta, msg, null, AlertType.WARNING);
		alerta.setTimeout(Alert.FOREVER);
		display.setCurrent(alerta);
	}
	
	private void adicionarRegistro() {
		try {
			ByteArrayOutputStream BAOS = new ByteArrayOutputStream();
			DataOutputStream DOS = new DataOutputStream(BAOS);
			DOS.writeUTF(nome.getString());
			DOS.writeUTF(telefone.getString());
			DOS.writeInt(tipo_contato.getSelectedIndex());
			byte [] bRec = BAOS.toByteArray();
			rs.addRecord(bRec, 0, bRec.length);
			DOS.close();
			BAOS.close();
			display.setCurrent(telainicial);
		} catch (Exception exc) {
			mostrarAlerta("Erro ao adicionar -", exc.toString());
		}
	}
	
	private void zerarRegistros(){
		try {
			rs.closeRecordStore();
			RecordStore.deleteRecordStore("SimpleContacts");
			rs = RecordStore.openRecordStore("SimpleContacts", true);
		} catch (Exception exc) {
			mostrarAlerta("Erro ao recriar o RecordStore -", exc.toString());
		}
		display.setCurrent(telainicial);
	}
	
	public void itemStateChanged (Item item) {
		if (item instanceof TextField) {
			String teste = ((TextField)item).getString();
			if (teste != "") {
				gravacaoOK = true;
			}
		}
	}
	
	public void CommandAction(Command command, Displayable displayable) {
		if (command == sair) {
			try {
				rs.closeRecordStore();
			} catch (Exception exc) {
				mostrarAlerta("Erro fechando o RedcorStore -", exc.toString());
			}
			destroyApp(true);
		} else if (command == adicionar) {
			nome.setString("");
			telefone.setString("");
			display.setCurrent(adicionaReg);
		} else if (command == salvar) {
			if (gravacaoOK == false) {
				mostrarAlerta("Registro vazio não será salvo", "Tente Novamente");
			} else {
				adicionarRegistro();
			}
		} else if (command == zerar) {
			zerarRegistros();
		} else if (command == voltar) {
			display.setCurrent(telainicial);
		}
	}
}

What i have to do?

Thanks,
Nathan Paulino Campos

The method you must implement has the method header commandAction(Command, Displayable) whereas the method you wrote has the method header CommandAction(Command, Displayable). These are considered two different methods since Java is case sensitive, which is why you're getting the error. Also, keep in mind that commandAction follows the recommended syntax whereas your method, CommandAction, does not.

Thanks very much!!!!!!!!
Now i can continue developing my project!!!!!!!!!!!!
Thanks very very much my friend!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.