×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Sep 2010
    Posts
    60

    NullPointer Exception for J2ME HTTPConnection to asp server

    hi developers,

    i have developed a J2ME application (im using nokia 5800xpressmusic) which is supposed to periodically send data to server using HTTPConnection. i am stuck in it as it gives me the NullPointer Exception after every may be 5 tries. the first 2 or 3 tries are successful but after that the application throws nullpointer exception and crashes. i am using timer for periodically sending the data to server and i have set the interval of 15 secs in the timer to repeat the sending process every 15 secs. i have tried different tricks with it but still cannot fix it. please help me. the code im using is below

    periodically data retrieval using timer

    Code:
    timer1.scheduleAtFixedRate(new TimerTask() {
                        public void run() {
                            // Task here ...
                            String battery = System.getProperty("com.nokia.mid.batterylevel");
    
                            int batter = Integer.parseInt(battery);
    
                            if(batter>20)
                            {
                            
                            data_to_send = "Here is the data to send to the server. it includes mobile information";
                            
                            SendValuestoDB2(data_to_send);// this is method which calls a thread that actually creates httpconnection with the server and sends the data. 
                           //the thread that sends the data is given below
                            
                            }
                        }
                    }, 15000, 15000);// i have changed the period to 30 secs and more aswel but no luck
    function to call thread

    Code:
    protected void SendValuestoDB2(String data)
            {
    
                    Sendvalues send;
                    
                    send = new Sendvalues(this);
                      send.start();
                      send.Sendvalues(data);
                      //String authmesg=auth.message.toString();
    
            }
    Thread to send the data to server

    Code:
    public class Sendvalues implements Runnable {
                MainMidlet midlet;
                private Display display;
                String data;
    
                String messagefromDB;
    
                public Sendvalues(MainMidlet midlet) {
                      this.midlet = midlet;
                      display = Display.getDisplay(midlet);
                }
    
                public void start() {
                      Thread t = new Thread(this);
                      t.start();
                }
    
                public void run() {
                      StringBuffer sb = new StringBuffer();
                      OutputStream os = null;
                      InputStream is = null;
                      HttpConnection c = null;
                      try {
                            String url="server-url-here";
                            c = (HttpConnection) Connector.open(url);
                            c.setRequestProperty("User-Agent","Profile/MIDP-1.0, Configuration/CLDC-1.0");
                            c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                           
                            c.setRequestProperty("Content-Language","en-US");
                            
                            c.setRequestProperty("Content-Length", ""+ data.length());
                            c.setRequestMethod(HttpConnection.POST);
                            // the above properties are the required properties. the server doesnt receive my request if i comment datalength or post property.
                            
                            os = c.openOutputStream();
    
                            os.write(data.getBytes());
    
                            is =c.openInputStream();
    
                            
                            int ch;
                           while((ch = is.read()) != -1)
                            {
                              sb.append((char) ch);
                            }
    
                            String responsemessage=c.getResponseMessage();// i have commented these three lines aswel but no luck
                            sb.append("\nThe response is ");
                            sb.append(responsemessage);
                            
                          
                                           
                          showAlert(sb.toString());
    
                           
                      } catch (Exception e) {
                            showAlert(e.getMessage());
                      }finally {
                          try {
                            if (os != null) os.close();
                            if (is != null) is.close();
                            if (c != null) c.close();
                          }
                          catch (IOException ioe) {}
                        }
                }
                
                public void Sendvalues(String data) {
                      this.data = data;
    
                }
    
                /* Display Error On screen*/
                private void showAlert(String err) {
                      Alert a = new Alert("");
                      a.setString(err);
                      a.setTimeout(Alert.FOREVER);
                      display.setCurrent(a);
                }
                private void showErrorMesg(String err) {
                      Alert a = new Alert("");
                      a.setString(err);
                      a.setTimeout(3000);
                      display.setCurrent(a);
                }
          };
    i have tried the above with different periods to send the data but the application throws nullpointer exception and crashes after every few successful data transfers. please help me i cant figure out where im doing wrong.

  2. #2
    Registered User
    Join Date
    May 2011
    Posts
    2

    Re: NullPointer Exception for J2ME HTTPConnection to asp server

    Hello!!

    Im a developer from Argentina, so, sorry about my english.
    I have deployed a j2me application that send data to a server using httpconnection (POST method).
    The user complete 4 textbox and press the botton "send". Then, the midlet take this information and send it to a servlet which is running in www.eatj.com. The servlet take this information and store it in a mysql database (stored in the same server).
    The application works fine in the emulator, but when i try to run it in my nokia 5800 dont works (without an exception). And i dont know why.

    This is the Code:

    ------------------------------------------------------------------------------
    package gpsMobileCellPhone;


    import java.io.*;
    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.location.*;


    public class main extends MIDlet implements CommandListener, Runnable
    {
    private Display display;
    private TextField textFieldMobileID;
    private TextField textFieldLatitud;
    private TextField textFieldLongitud;
    private TextField textFieldDescripcion;
    private TextField textFieldInfo;
    private Form form;
    private Command cancel;
    private Command send;

    //private Command volver;

    public main()
    {
    textFieldMobileID = new TextField("MobileID:", "", 10, TextField.ANY);
    textFieldLatitud = new TextField("Latitud:", "", 10, TextField.ANY);
    textFieldLongitud = new TextField("Longitud:", "", 10, TextField.ANY);
    textFieldDescripcion = new TextField("Descrip:", "", 10, TextField.ANY);
    textFieldInfo = new TextField("Info:", "", 10, TextField.ANY);
    form = new Form("Mobile");
    cancel = new Command("Cancel", Command.CANCEL, 2);
    send = new Command("Enviar", Command.OK, 2);

    display = Display.getDisplay(this);
    form.append(textFieldMobileID);
    form.append(textFieldLatitud);
    form.append(textFieldLongitud);
    form.append(textFieldDescripcion);
    form.append(textFieldInfo);
    form.addCommand(cancel);
    form.addCommand(send);
    form.setCommandListener(this);
    display.setCurrent(form);

    //volver= new Command("Volver", Command.BACK, 2);
    }

    public void startApp()
    {/*
    display = Display.getDisplay(this);
    form.append(textFieldMobileID);
    form.append(textFieldLatitud);
    form.append(textFieldLongitud);
    form.append(textFieldDescripcion);
    form.append(textFieldInfo);
    form.addCommand(cancel);
    form.addCommand(send);
    form.setCommandListener(this);
    display.setCurrent(form);
    */
    if (display!=null)
    display= Display.getDisplay(this);
    display.setCurrent(form);
    }


    public void pauseApp()
    {

    }

    public void destroyApp(boolean unconditional)
    {
    notifyDestroyed();
    }
    public void SendData(String id, String Lat, String Long, String desc) throws IOException
    {
    HttpConnection httpConn = null;
    String url = "urlserver";
    DataOutputStream os = null;

    try
    {
    // Open an HTTP Connection object
    httpConn = (HttpConnection)Connector.open(url);

    //Creo los datos a enviar
    String params;
    params =
    "mobileId=" + textFieldMobileID.getString() +
    "&latitude=" + textFieldLatitud.getString() +
    "&longitude=" + textFieldLongitud.getString() +
    "&description=" + textFieldDescripcion.getString();

    //Propiedades del HTTP Connection
    httpConn.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-1.0");
    httpConn.setRequestProperty("Content-Language","en-US");
    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    httpConn.setRequestProperty("Content-Length", ""+ params.length());
    httpConn.setRequestMethod(HttpConnection.POST);

    // If you experience connection/IO problems, try
    // removing the comment from the following line
    // httpConn.setRequestProperty("Connection", "close");


    //os = httpConn.openOutputStream();
    os=httpConn.openDataOutputStream();

    os.write(params.getBytes());

    /* Caution: os.flush() is controversial. It may create unexpected behavior
    on certain mobile devices. Try it out for your mobile device */
    //os.flush();
    if (os!=null)
    os.close();
    if (httpConn!=null)
    httpConn.close();
    }
    catch (Exception ex)
    {
    textFieldInfo.setString(ex.getMessage());
    }

    }

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

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

    // Request the location, setting a one-minute timeout
    l = lp.getLocation(1800);
    c = l.getQualifiedCoordinates();
    textFieldLatitud.setString(Double.toString(c.getLatitude()));
    textFieldLongitud.setString(Double.toString(c.getLongitude()));
    }
    catch(Exception e)
    {

    }

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

    public void commandAction(Command c, Displayable d)
    {
    String label = c.getLabel();
    if(label.equals("Cancel"))
    {
    destroyApp(true);
    }
    else if(label.equals("Enviar"))
    {
    try
    {//use variables, do not use textfields as variables
    //getPosition();

    //SendData(textFieldMobileID.getString(), textFieldLatitud.getString(), textFieldLongitud.getString(), textFieldDescripcion.getString());
    Thread t= new Thread(this);
    t.start();

    }
    catch(Exception e)
    {

    }
    }
    }

    public void run() {
    try {
    SendData(textFieldMobileID.getString(), textFieldLatitud.getString(), textFieldLongitud.getString(), textFieldDescripcion.getString());
    }
    catch (Exception ex){

    }
    }
    }
    -----------------------------------------------------------------------------

    Thank you for your time.
    This is my email: volpefrancisco@hotmail.com

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105

    Re: NullPointer Exception for J2ME HTTPConnection to asp server

    FraN_ArG, please, let us continue in your other thread …
    By the way, the software of this discussion board offers a code tag.

Similar Threads

  1. NullPointer Exception for J2ME HTTPConnection to asp server
    By mbilal3 in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2011-05-20, 11:22
  2. Send Image from J2ME Mobile to ASP Server
    By kutax87 in forum Mobile Java Networking & Messaging & Security
    Replies: 5
    Last Post: 2011-02-14, 13:51
  3. Application Error Nullpointer Exception
    By laksmntr in forum Symbian
    Replies: 1
    Last Post: 2006-04-26, 06:04
  4. JAR nullpointer exception help
    By seewan22 in forum Mobile Java Tools & SDKs
    Replies: 5
    Last Post: 2006-02-03, 17:48
  5. httpconnection nullpointer
    By rcaboni in forum Mobile Java General
    Replies: 0
    Last Post: 2002-09-06, 14:19

Posting Permissions

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