Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8797039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:45:50+00:00 2026-06-13T23:45:50+00:00

I am Trying to send SMS from J2ME Application, But I am getting NullPointer

  • 0

I am Trying to send SMS from J2ME Application, But I am getting NullPointer Exception. Here is my entire code. Please help me if you have any idea. Thanks a lot…I am using MessageConnection for sending sms in J2ME. please correct me where I am wrong.

import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.wireless.messaging.*;

public class SendSms extends MIDlet implements CommandListener {

    Display display;
    private TextField toWhom;
    private TextField message;
    private Alert alert;
    private Command send, exit;
    MessageConnection clientConn;
    private Form compose;

    public SendSms() {
        display = Display.getDisplay(this);
        compose = new Form("Compose Message");
        toWhom = new TextField("To", "", 10, TextField.PHONENUMBER);
        message = new TextField("Message", "", 600, TextField.ANY);
        send = new Command("Send", Command.BACK, 0);
        exit = new Command("Exit", Command.SCREEN, 5);
        compose.append(toWhom);
        compose.append(message);
        compose.addCommand(send);
        compose.addCommand(exit);
        compose.setCommandListener(this);

    }

    public void startApp() {
        display.setCurrent(compose);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void commandAction(Command cmd, Displayable disp) {
        if (cmd == exit) {
            destroyApp(false);
        }
        if (cmd == send) {
            String mno = toWhom.getString();
            String msg = message.getString();
            if (mno.equals("")) {
                alert = new Alert("Alert");
                alert.setString("Enter Mobile Number!!!");
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            } else {
                try {
                    clientConn = (MessageConnection) Connector.open("sms://" + mno);
                } catch (Exception e) {
                    alert = new Alert("Alert");
//                    alert.setString("Unable to connect to Station because of network problem");
                    alert.setString(e.getMessage()+"|"+e.toString());
                    System.out.println(e.getMessage()+"|"+e.toString());
                    alert.setTimeout(2000);
                    display.setCurrent(alert);

                }
                try {
                    TextMessage textmessage = (TextMessage) clientConn.newMessage(MessageConnection.TEXT_MESSAGE);
                    textmessage.setAddress("sms://" + mno);
                    textmessage.setPayloadText(msg);
                    clientConn.send(textmessage);
                } catch (Exception e) {
                    Alert alert = new Alert("Alert", "", null, AlertType.INFO);
                    alert.setTimeout(Alert.FOREVER);
                    alert.setString(e.getMessage()+"|"+e.toString());
                    System.out.println(e.getMessage()+"|"+e.toString());
                    display.setCurrent(alert);
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:45:51+00:00Added an answer on June 13, 2026 at 11:45 pm

    This is my code from which, I can send SMS Perfecly. Just Add Permissions in Midlet.
    MIDlet-Permissions: javax.microedition.io.Connector.sms,javax.wireless.messaging.sms.send

    And my code is,

    import javax.microedition.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.MIDlet;
    import javax.wireless.messaging.*;
    
    public class SendSms extends MIDlet implements CommandListener {
    
        Display display;
        private TextField toWhom;
        private TextField message;
        private Alert alert;
        private Command send, exit;
        MessageConnection clientConn;
        private Form compose;
    
        public SendSms() {
            display = Display.getDisplay(this);
            compose = new Form("Compose Message");
            toWhom = new TextField("To", "", 10, TextField.PHONENUMBER);
            message = new TextField("Message", "", 600, TextField.ANY);
            send = new Command("Send", Command.BACK, 0);
            exit = new Command("Exit", Command.SCREEN, 5);
            compose.append(toWhom);
            compose.append(message);
            compose.addCommand(send);
            compose.addCommand(exit);
            compose.setCommandListener(this);
    
        }
    
        public void startApp() {
            display.setCurrent(compose);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
            notifyDestroyed();
        }
    
        public void commandAction(Command cmd, Displayable disp) {
            if (cmd == exit) {
                destroyApp(false);
            }
            if (cmd == send) {
                String mno = toWhom.getString();
                String msg = message.getString();
                if (mno.equals("")) {
                    alert = new Alert("Alert");
                    alert.setString("Enter Mobile Number!!!");
                    alert.setTimeout(Alert.FOREVER);
                    display.setCurrent(alert);
                } else {
                    try {
                        clientConn = (MessageConnection) Connector.open("sms://" + mno);
                    } catch (Exception e) {
                        alert = new Alert("Alert");
    //                    alert.setString("Unable to connect to Station because of network problem");
                        alert.setString(e.getMessage()+"|"+e.toString());
                        System.out.println(e.getMessage()+"|"+e.toString());
                        alert.setTimeout(2000);
                        display.setCurrent(alert);
    
                    }
                    try {
                        TextMessage textmessage = (TextMessage) clientConn.newMessage(MessageConnection.TEXT_MESSAGE);
                        textmessage.setAddress("sms://" + mno);
                        textmessage.setPayloadText(msg);
                        clientConn.send(textmessage);
                    } catch (Exception e) {
                        Alert alert = new Alert("Alert", "", null, AlertType.INFO);
                        alert.setTimeout(Alert.FOREVER);
                        alert.setString(e.getMessage()+"|"+e.toString());
                        System.out.println(e.getMessage()+"|"+e.toString());
                        display.setCurrent(alert);
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send sms from java application using SMPPSim as SMS gateway
I am trying to send sms from my app but it is crashing. LOGCAT:
I am trying to figure out how to send sms from my application. I
I am trying to send sms through device. But it crashes on the line
I am trying to send SMS using a webservice call. But am having errors
I'm trying to send and receive binary data sms from my app (sdk 2.1-update
I am trying to send sms programmatically from one android emulator to another on
I am trying to send a sms with smslib but It did not send
I'm trying to use python and mechanize to send sms from my mobile provider
I was trying to send SMS from within my app. I wrote this piece

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.