J2ME中发送SMS的NullPointer异常

时间:2012-11-02 07:18:13

标签: java java-me sms send sms-gateway

我正在尝试从J2ME应用程序发送短信,但我收到了NullPointer异常。这是我的整个代码。如果您有任何想法,请帮助我。非常感谢...我正在使用MessageConnection在J2ME中发送短信。请纠正我错在哪里。

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);
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:5)

我怀疑问题是clientConn为空。

毕竟,你在之前的try块中为它分配了一个值 - 但是如果你从第一个块中捕获了一个异常,你就会继续,好像它已经工作了。不要那样做。

答案 1 :(得分:3)

这是我的代码,我可以发送SMS Perfecly。只需在Midlet中添加权限即可。 MIDlet-Permissions:javax.microedition.io.Connector.sms,javax.wireless.messaging.sms.send

我的代码是,

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);
                }
            }
        }
    }
}