×

Discussion Board

Results 1 to 4 of 4
  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 (i am using nokia 5800xpressmusic) that is supposed to send data periodically to server. i am using timers for getting some mobile related information and sending it to the server and the interval for sending the data is set to 15 secs (which i changed aswel but no luck). the application runs fine on the emulator but when i run it in device, it throws nullpointer exception after every may be 4 or 5 successful data transfers. i have done some modifications aswel but no luck and cannot figure out what im doing wrong. the code is below

    Timer to get device related data to send to the server

    Code:
    timer.scheduleAtFixedRate(new TimerTask() {
                        public void run() {
                            // i get the battery level of device first if its under 20% then dont send the values
                            String battery = System.getProperty("com.nokia.mid.batterylevel");
    
                            int batter = Integer.parseInt(battery);
    
                            if(batter>20)
                            {
                        
                            data_to_send = "here is the data that i send to server";
                           
                            SendValuestoDB2(data_to_send);
                                                  
                            }
                        }
                    }, 15000, 15000);// i changed the interval aswel but no luck
    Function to call the thread

    Code:
    protected void SendValuestoDB2(String data)
            {
    
                    Sendvalues send;
                    
                    send = new Sendvalues(this);
                      send.start();
                      send.Sendvalues(data);
                    
    
            }

    Thread to send the values 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;// i have changed them to os="" instead of null but still no luck
                      InputStream is = null;
                      HttpConnection c = null;
                      try {
                            String url="this-is-url-to-server";
                            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 required by the server, it doesnt accept my request if i comment datalength property 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 from server is: ");
                            sb.append(responsemessage);
                            
                               
                                                 
                      } catch (Exception e) {
                            getForm().append(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);
                }
          };
    this code works for the first 4 or 5 tries and server receives the values too, but after that the application throws NullPointer exception and crashes. i dont what am i doing wrong and where, please help me.
    i am not sure if its the inputStream problem or not. is it ok if i keep sending the data without getting the response or something? but then doing this i wont be able to know whether the remote server is receiving the data or not, also wont be able to know whether the application sends data or not.
    Last edited by mbilal3; 2011-05-19 at 20:30.

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

    Re: NullPointer Exception for J2ME HTTPConnection to asp server

    Hi mbilal3,

    These situation becomes very hard to trace, when the app, initially working properly and then after a while it gets failed. I will recommend you to debug the code, use some Logger and trace the app's flow line by line. So that you can get the idea where the app was crashed. In case of the emulator, if app is working fine the there can be some issues with the memory/heap of the device, therefore make sure that you are reallocating the occupied heap at the time when it is the turn for the new request,

    Check and revert,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  3. #3
    Regular Contributor
    Join Date
    Sep 2010
    Posts
    60

    Re: NullPointer Exception for J2ME HTTPConnection to asp server

    hi raj,

    thanks for reply. do you think its because i am starting the thread every 15 secs and it doesn't get stopped until it is started next time? shall i stop the thread every time before i call it again to send the values. i was working with it last and removed the thread and simply put the httpconnection part into a method. it didnt give any exception for about 15 times then i stopped the application, i thought may be its the thread that is causing problem, i'll run the application for may be around 100 times without thread today and see if i still get the exception, i have to set the interval to 15 mins instead of 15 secs so will take long time to test but i'll test it with 15 secs before and see what the results are. thanks anyways

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

    Re: NullPointer Exception for J2ME HTTPConnection to asp server

    You need not to stop the thread, if you do this the tracing the current time will be typical for you, even you will be in trouble in to when to again re-start the thread. Therefore you just keep the thread running, however you need to manage the data that you have loaded, like the byte array and all, so that the memory can be optimized.

    Check, and try to debug the code, and revert with your findings.
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

Similar Threads

  1. NullPointer Exception for J2ME HTTPConnection to asp server
    By mbilal3 in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2011-05-30, 13:18
  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
  •