Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Sulist
Posts: 48
Registered: ‎06-17-2011
My Device: Blackberry 9800
My Carrier: Personal

Where is my mistake

I'm making an application that can send sms with the code below:

MenuItem send= new MenuItem("Send SMS " ,3,3);
        lokasi.setCommand(new Command(new CommandHandler(){
             public void execute(ReadOnlyCommandMetadata metadata, Object context)
             {   
                    MessageConnection kirimkan = null;
                    try {
                        kirimkan = (MessageConnection)Connector.open("sms://");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    TextMessage pesanya = (TextMessage)kirimkan.newMessage(MessageConnection.TEXT_MESSAGE);
                    pesanya.setPayloadText(text.getText());
                    pesanya.setAddress("sms://" +sing[0]);
                    try {
                        kirimkan.send(pesanya);
                        kirimkan.close();
                    } catch (InterruptedIOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
             }
        }));

 I got a runtime exception come some body give me some correction for my code thanks before

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Where is my mistake

What is the text of the RuntimeException that you get - use the toString method or debug and get the detailMessage. 

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Where is my mistake

Where is "sing[0]" declared?

 

Please use plain text.
Contributor
Sulist
Posts: 48
Registered: ‎06-17-2011
My Device: Blackberry 9800
My Carrier: Personal

Re: Where is my mistake

When i'm debuging that is a stuck in here :

kirimkan.send(pesanya);

 

please give a corection

Please use plain text.
Contributor
Sulist
Posts: 48
Registered: ‎06-17-2011
My Device: Blackberry 9800
My Carrier: Personal

Re: Where is my mistake

To rex doug sing [0]  some persistent store and the contain is the mobile number

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Where is my mistake

In general things don't get stuck, I suspect you are missing an Exception. 

 

I would change your exception handling:

 

                    } catch (InterruptedIOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

 

In fact you will get nothing from these because there is no stacktrace kept by Exceptions.  I suggest that you replace these, for the meantime, with something like

 

                    } catch (Throwable t) {
                        // TODO Auto-generated catch block
                        t.printStackTrace();

                        System.out.println("Exception: " + t.toString());
                    }

Let us know if this tells you anything useful while debugging. 

Please use plain text.
Developer
kamal_nigam
Posts: 434
Registered: ‎07-23-2012
My Device: Blackberry 10
My Carrier: Orange

Re: Where is my mistake

  1. this will be non blocking call so use thread

Find below code. This might help you

public void run() {

                                try{

                                                _conn = Connector.open("sms://:" + _port);

                                               

                                                if(isCDMA()){

                                                                DatagramConnectionBase dcb = (DatagramConnectionBase) _conn;

                                                                dcb.send(message.toDatagram(dcb));

                                                } else{

                                                                MessageConnection mc = (MessageConnection) _conn;

                                                                mc.send(message.toMessage(mc));

                                                }

                                                if(null != _conn){

                                                                _conn.close();

                                                }

                                } catch(Exception ioe){

                                } finally {

                 }

}

 

 

 

    public Message toMessage(MessageConnection mc) {

                                               String addressString = "//" + _address

                                                                                + (_port.equals(NON_ZERO_PORT_NUMBER) ? ":" + _port : "");

 

                                                TextMessage m = (TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE, addressString);

                                                m.setPayloadText(_msg);

 

                                                return m;

                                }

 

                                public Datagram toDatagram(DatagramConnectionBase datagramConnectionBase) throws IOException {

                                                DatagramBase datagram = null;

                                                byte[] data = _msg.getBytes("ISO-8859-1");

                                                datagram = (DatagramBase) datagramConnectionBase.newDatagram();

                                                SmsAddress smsAddress = new SmsAddress("//" + _address);

                                                SMSPacketHeader smsPacketHeader = smsAddress.getHeader();

                                                smsPacketHeader.setMessageCoding(SMSPacketHeader.MESSAGE_CODING_ISO8859_1);

                                                datagram.setAddressBase(smsAddress);

                                                datagram.write(data, 0, data.length);

 

                                                return datagram;

                                }

 

Thanks
-------------------------------------------------------------------------------------
Press the Accept as solution Button when u got the Solution
Press Kudo to say thank to developer.
-------------------------------------------------------------------------------------.
Please use plain text.