I was compiling some code in netbeans and I got this error on the actual emulator.
java.lang.IllegalArgumentException:Port format
Can anybody give any advice on what do to do?
I was compiling some code in netbeans and I got this error on the actual emulator.
java.lang.IllegalArgumentException:Port format
Can anybody give any advice on what do to do?
Yes, use the Java forum for posting Java-related issues![]()
Hi cisa,
which method throws that Exception? Try posting related code (otherwise it'll be really hard to spot out any possible issues)
Pit
Hi Jappit,
Thanks for responding. Below is my code. As well, I think the code is waiting for an SMS. For now I am just doing this on the emulator. Do you know anyway to send an SMS to the emulator? Do I have to just put in some more code to do this.
package example;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
public class SMSReceive extends MIDlet implements CommandListener,Runnable,MessageListener{
Command exitCommand =new Command("Exit",Command.EXIT,2);
Command replyCommand =new Command("Reply", Command.OK,1);
Alert content;
Display display;
Thread thread;
String[] connections;
boolean done;
String smsPort;
MessageConnection smsconn=null;
Message msg;
String senderAddress;
Alert sendingMessageAlert;
SMSSender sender;
Displayable resumeScreen;
// private Command exitCommand;
private Form form;
private StringItem stringItem;
public SMSReceive() {
smsPort=getAppProperty("SMS-Port");
display=Display.getDisplay(this);
content= new Alert("SMS Receive");
content.setTimeout(Alert.FOREVER);
content.addCommand(exitCommand);
content.setCommandListener(this);
content.setString("Receiving...");
sendingMessageAlert=new Alert("SMS",null,null,AlertType.INFO);
sendingMessageAlert.setTimeout(5000);
sendingMessageAlert.setCommandListener(this);
sender=new SMSSender(smsPort, display, content, sendingMessageAlert);
resumeScreen=content;
}
private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
// write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
// write post-initialize user code here
}//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
// write pre-action user code here
switchDisplayable(null, getForm());//GEN-LINE:|3-startMIDlet|1|3-postAction
// write post-action user code here
}//GEN-BEGIN:|3-startMIDlet|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
// write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
// write post-action user code here
}//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
/**
* Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
* @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
// write pre-switch user code here
Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}//GEN-END:|5-switchDisplayable|1|5-postSwitch
// write post-switch user code here
}//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
/**
* Called by a system to indicated that a command has been invoked on a particular displayable.
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
try{
if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|19-preAction
if (command == exitCommand) {//GEN-END:|7-commandAction|1|19-preAction
// write pre-action user code here
exitMIDlet();//GEN-LINE:|7-commandAction|2|19-postAction
// write post-action user code here
}//GEN-BEGIN:|7-commandAction|3|7-postCommandAction
}//GEN-END:|7-commandAction|3|7-postCommandAction
}catch(Exception ex){ex.printStackTrace();}
}//GEN-BEGIN:|7-commandAction|4|
//</editor-fold>//GEN-END:|7-commandAction|4|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
/**
* Returns an initiliazed instance of exitCommand component.
* @return the initialized component instance
*/
public Command getExitCommand() {
if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
// write pre-init user code here
exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
// write post-init user code here
}//GEN-BEGIN:|18-getter|2|
return exitCommand;
}
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit
/**
* Returns an initiliazed instance of form component.
* @return the initialized component instance
*/
public Form getForm() {
if (form == null) {//GEN-END:|14-getter|0|14-preInit
// write pre-init user code here
form = new Form("Welcome", new Item[] { getStringItem() });//GEN-BEGIN:|14-getter|1|14-postInit
form.addCommand(getExitCommand());
form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
// write post-init user code here
}//GEN-BEGIN:|14-getter|2|
return form;
}
public StringItem getStringItem() {
if (stringItem == null) {//GEN-END:|16-getter|0|16-preInit
// write pre-init user code here
stringItem = new StringItem("Hello", "Hello, World!");//GEN-LINE:|16-getter|1|16-postInit
// write post-init user code here
}//GEN-BEGIN:|16-getter|2|
return stringItem;
}
//</editor-fold>//GEN-END:|16-getter|2|
public Display getDisplay () {
return Display.getDisplay(this);
}
public void exitMIDlet() throws MIDletStateChangeException {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
public void startApp() {
// SMS connection to be read.
String smsConnection = "sms://:" + smsPort;
content.setString(smsConnection);
// Open the message connection.
// if (smsconn == null) {
try {
smsconn = (MessageConnection)Connector.open(smsConnection, Connector.READ);
// smsconn.setMessageListener(this);
} catch (Throwable t) {
content.setString(t.toString());
}
// } catch (IOException ioe) {
// content.setString(ioe.toString());
// ioe.printStackTrace();
// }
// }
/*
// Initialize the text if we were started manually.
connections = PushRegistry.listConnections(true);
if (connections == null || connections.length == 0) {
content.setString("Waiting for SMS on port " + smsPort + "...");
}
done = false;
thread = new Thread(this);
thread.start();
*/
display.setCurrent(resumeScreen);
}
public void pauseApp() {
done=true;
thread=null;
resumeScreen=display.getCurrent();
}
public void destroyApp(boolean unconditional) throws MIDletStateChangeException {
done=true;
thread=null;
if(smsconn!=null)
{try{smsconn.close();}
catch(IOException e){}}}
public void run() { try{
msg=smsconn.receive();
if(msg!=null)
{senderAddress=msg.getAddress();
content.setTitle("From: " + senderAddress);
if(msg instanceof TextMessage)
{content.setString(((TextMessage)msg).getPayloadText());
}
else{
StringBuffer buf = new StringBuffer();
byte[] data= ((BinaryMessage)msg).getPayloadData();
for(int i=0;i<data.length;i++){
int intData= (int)data[i] & 0xFF;
if(intData<0x10)
{buf.append("0");
}
buf.append(Integer.toHexString(intData));
buf.append(' ');
}
content.setString(buf.toString());}
content.addCommand(replyCommand);
display.setCurrent(content);
}
}catch(IOException e){}
}
public void notifyIncomingMessage(MessageConnection conn) {
if(thread==null)
{done=false;
thread = new Thread(this);
thread.start();
}
}
/* private void reply(){
String address=senderAddress.substring(6);
String statusMessage="Sending message to" +address+"...";
sendingMessageAlert.setString(statusMessage);
sender.promptAndSend(senderAddress);
}*/
}
Hi,
I've just tested your code with port 5000 and it seems to execute without Exceptions on WTK emulator. So, you're probably getting a wrong (or null?) port number with the getAppProperty() call. Try loggin it (with a simple System.out.println) to check if its value is correct.
Hope it helps,
Pit
What do you mean exactly that you tested the code with port 5000? Is that a setting that I have to put somewhere. Is that the port where the code is suppose to accept the SMS?
In your code there's this line:
that assumes that SMS-Port property is defined somewhere in the descriptor file or manifest. I simply tried hard-coding "5000" as port number, and then started your application, without any Exception being thrown.Code:smsPort=getAppProperty("SMS-Port");
You can also try with different port numbers: the important thing is that you're listening on the same port that you're using to send SMS messages.
Pit
How do you hard code a port number because smsPort is a string and is expecting a string? Do I have to change the name of the variable type?
Last edited by cisa; 2008-06-23 at 14:57.
Hi cisa,
I've tried changing this line:
toCode:smsPort=getAppProperty("SMS-Port");
You can try it and see if the Exception has gone.Code:smsPort = "5000";
Pit
Yes, I also changed this. But if I do this, does it actually listen to port 5000? From what I see, it just changes the output to display 5000 because it's a string....Please tell me if I am wrong.
Assuming you are right, I guess now I need to send a message on port 5000.
Yes, it listens on that port since it is used to initialize the MessageConnection, in this code lines:
PitCode:String smsConnection = "sms://:" + smsPort; ... smsconn = (MessageConnection)Connector.open(smsConnection, Connector.READ);