×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    17

    Null pointer exception

    Hi all,

    I have a program that send out GPS data through SMS. And there is sending error thrown. On emulator, I think the error may come from mesg= server.getMesg(); . And I post the code here. I have submit this work in few days, so could you help me solve the problem? Thanks.

    clyde

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.location.*;

    import javax.microedition.rms.*;
    import javax.microedition.io.*;
    import javax.wireless.messaging.*;
    import java.util.*;


    public class loctest extends MIDlet implements CommandListener
    {
    private Display display;
    private Form form;
    private Command cmdExit,cmdOK;
    private StringItem si;
    private TextField dest1, dest2;
    private Command sendCommand;
    Retriever server;


    public loctest()
    {

    display =Display.getDisplay(this);
    form = new Form("SMS");
    cmdExit = new Command("Exit",Command.EXIT,5);
    cmdOK = new Command("OK",Command.OK,1);
    sendCommand = new Command("SEND",
    Command.OK,2);
    si = new StringItem("Press OK to get data first", null);
    form.append(si);

    /*dest1 = new TextField("Receipient 1", "",
    100, TextField.ANY);

    dest2 = new TextField("Receipient 2", "",
    100, TextField.ANY);
    form.append(dest1);
    form.append(dest2);
    */

    form.addCommand(sendCommand);
    form.addCommand(cmdOK);
    form.addCommand(cmdExit);
    form.setCommandListener(this);

    }

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


    public void pauseApp()
    {

    }

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

    public void commandAction(Command c, Displayable d)
    {
    if (c == cmdOK){
    Retriever ret = new Retriever(this);
    ret.start();

    } else if (c == cmdExit) {
    destroyApp(false);
    }

    else if (c==sendCommand)
    {
    Timer task1 = new Timer();

    task1.schedule(new DateTask(), 0, 10000);//delay=0 sec, period =10 sec
    }

    }

    public void displayString(String string)
    {
    si.setText(string);
    }

    class DateTask extends TimerTask
    {

    public void run()
    {
    sendMesg();
    }

    public void sendMesg()
    {
    try
    {
    String mesg;
    mesg= server.getMesg();

    //String addr1 = "sms://" + dest1.getString();
    String addr1;
    addr1= "sms://+8613261800566";
    MessageConnection conn1 =
    (MessageConnection)Connector.open(addr1);
    TextMessage msg1 =
    (TextMessage)conn1.newMessage(
    MessageConnection.TEXT_MESSAGE);

    msg1.setPayloadText(mesg);
    conn1.send(msg1);
    conn1.close();
    /*

    String addr2 = "sms://" + dest2.getString();
    MessageConnection conn2 = (MessageConnection)Connector.open(addr2);
    TextMessage msg2 =
    (TextMessage)conn2.newMessage(
    MessageConnection.TEXT_MESSAGE);
    msg2.setPayloadText(mesg);
    conn2.send(msg2);
    conn2.close();
    */

    }
    catch (Exception e)
    {
    System.out.println("Error in sending");
    e.printStackTrace();
    }
    }

    }

    }



    class Retriever extends Thread {

    private loctest midlet;
    private String mesg;

    public Retriever(loctest midlet)
    {
    this.midlet = midlet;

    }

    public void run()
    {
    try {
    checkLocation();
    } catch (Exception ex)
    {
    ex.printStackTrace();
    midlet.displayString(ex.toString());
    }
    }

    public void checkLocation() throws Exception
    {
    String string;
    Location l;
    LocationProvider lp;
    Coordinates c;
    // Set criteria for selecting a location provider:
    // accurate to 500 meters horizontally
    Criteria cr= new Criteria();
    cr.setHorizontalAccuracy(500);

    // Get an instance of the provider
    lp= LocationProvider.getInstance(cr);

    // Request the location, setting a one-minute timeout
    l = lp.getLocation(60);
    c = l.getQualifiedCoordinates();

    if(c != null ) {
    // Use coordinate information
    double lat = c.getLatitude();
    double lon = c.getLongitude();
    string = "\nLatitude : " + lat + "\nLongitude : " + lon;
    mesg = string;

    } else {
    string ="Location API failed";
    }
    midlet.displayString(string);
    }
    public String getMesg()
    {
    return mesg;
    }
    }

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Null pointer exception

    I'm sure it's coming from that line. The variable "server" is null, because you never assign anything to it.

    Try adding:

    Code:
    server = new Retriever();
    somewhere.

    Graham.

  3. #3
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: Null pointer exception

    Hey you are two mistakes here,

    1.Retriever ret = new Retriever(this);you are declaring this variable as the local one and hance this will not be accessible outside the commandAction(),

    2.As graham stated,

    just create one global object and use that.Better you can initialize the object in the constructor and then can use the same.
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  4. #4
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Null pointer exception

    Also a tip on Exception handling in J2mE :

    - first of all, the printStackTrace() won't do any good
    - if you catch an Exception do something usefull with it, replace
    System.out.println("Error in sending");
    with
    System.out.println("Exception in sending : " + e.toString());

    Another very good option, especially if you are doubting about a specific part of code like now, is to debug the application and single-step through the piece of code you are not trusting to function properly, you will be able to see the value's of all variables/objects... So if something is null when it should not be you will be able to actually see that...

Similar Threads

  1. Pointer address modified after TRAP call
    By omarfr in forum Symbian
    Replies: 9
    Last Post: 2008-11-28, 16:53
  2. 6131NFC SDK1.1 Class cast exception on Custom Data Source
    By steview in forum Tools and SDK Feedback (Closed)
    Replies: 0
    Last Post: 2007-08-13, 22:07
  3. System.getProperty() results in null pointer exception
    By artemg in forum Mobile Java General
    Replies: 2
    Last Post: 2005-02-28, 17:04
  4. global root overflow(please help)
    By sushant_125 in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2004-05-14, 08:24
  5. Linux driver in RedHat 7.3
    By nsolhjoo in forum Multimodecards
    Replies: 0
    Last Post: 2003-10-24, 17:04

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×