Uncaught exception java/lang/NullPointerException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • josephx
    New Member
    • May 2008
    • 1

    Uncaught exception java/lang/NullPointerException

    Hello,

    I got some of these errors listed below after executing an HTTP Post MIDlet on CLDC/MIDP platform, "Nokia S40 DP 2.0 SDK 1.1" and "S40 5th Edition SDK Feature Pack 1" and even for S60's SDK.. the application stops.

    However, it does work only on "Sun Java(TM) Wireless Toolkit 2.5.1 for CLDC" with the same errors displayed, but the application was able to execute successfully.

    Errors received are:
    Using Untrusted simulated domain
    Uncaught exception java/lang/NullPointerExce ption.
    WARNING: Traffic View: Listing of TCP/UDP Sent traffic is set to off (see Monitor)
    WARNING: Traffic View: Listing of TCP/UDP received traffic is set to off (see Monitor)

    The codes are pasted below. I have another Java class file but there seem to be no errors in it.

    It would be helpful if someone here could point out which part is causing the null exception error. Thanks.

    Code:
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    // Package used to execute Record Storing
    import javax.microedition.rms.RecordStoreException;
    
    public class HttpPostMIDlet extends MIDlet implements CommandListener, Runnable {
            
        private static final String kUsername = "Username";
        private static final String kLatitude = "Latitude";
        private static final String kLongitude = "Longitude";
        
        private Form rsForm;
        private Form hpForm;
        
        private Command okRsFrm;
        private rsPreferences rPreferences;
        private TextField mUsername, mLatitude, mLongitude;
        
        public HttpPostMIDlet() {
            try {
                rPreferences = new rsPreferences("preferences");
            }
            catch (RecordStoreException rse) {
                rsForm = new Form("Exception");
                rsForm.append(new StringItem(null, rse.toString()));
                rsForm.addCommand(new Command("Exit", Command.EXIT, 0));
                rsForm.setCommandListener(this);
                return;
            }
            
            // Record Store Form: Input User name, latitude, and longitude.
            rsForm = new Form("Record Store Form");
            
            mUsername = new TextField("Username", rPreferences.get(kUsername), 32, 0);
            mLatitude = new TextField("Latitude", rPreferences.get(kLatitude), 32, 0);
            mLongitude = new TextField("Longitude", rPreferences.get(kLongitude), 32, 0);
            
            rsForm.append(mUsername);
            rsForm.append(mLatitude);
            rsForm.append(mLongitude);
            
            // Exit Command: Allows the mobile phone user to exit the application
            rsForm.addCommand(new Command("Exit", Command.EXIT, 0));
            rsForm.addCommand(getOkRsFrm()); 
            rsForm.setCommandListener(this);
        }
        
        public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
            // Switch current display when called
            Display display = getDisplay();
            if (alert == null) {
                display.setCurrent(nextDisplayable);
            } else {
                display.setCurrent(alert, nextDisplayable);
            }
        }
        
        public Display getDisplay () {
            return Display.getDisplay(this);
        }
    
        
        public void startApp() {
            Display.getDisplay(this).setCurrent(rsForm);
            Thread t = new Thread(this);
            t.start();
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
            rPreferences.put(kUsername, mUsername.getString());
            rPreferences.put(kLatitude, mLatitude.getString());
            rPreferences.put(kLongitude, mLongitude.getString());
            try { 
                rPreferences.save();
            }
            catch (RecordStoreException rse) {
            }
        }
        
        public void run() {
            HttpConnection hc = null;
            InputStream in = null;
            OutputStream out = null;
            try {
                String message = "username=" + mUsername.getString() + "%20Latitude=" + mLatitude.getString() + "%20Longitude=" + mLongitude.getString();
                String url = getAppProperty("PostMIDlet-URL");
                
                hc = (HttpConnection)Connector.open(url);
                hc.setRequestMethod(HttpConnection.POST);
                hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                hc.setRequestProperty("Content-Length", Integer.toString(message.length()));
                
                out = hc.openOutputStream();
                out.write(message.getBytes());
                in = hc.openInputStream();
                int length = (int)hc.getLength();
                byte[] data = new byte[length];
                in.read(data);
                String response = new String(data);
                StringItem stringItem = new StringItem(null, response);
                hpForm.append(stringItem);
                hpForm.setTitle("Send and Retrieve data: Done.");
            }
            catch (IOException ioe) {
                StringItem stringItem = new StringItem(null, ioe.toString());
                hpForm.append(stringItem);
                hpForm.setTitle("Send and Retrieve data: Done.");
            }
            finally {
                try {
                    if (out != null) out.close();
                    if (in != null) in.close();
                    if (hc != null) hc.close();
                }
                catch (IOException ioe) {}
            }
        }
        
        public void commandAction(Command c, Displayable s) {
            if(c.getCommandType() == Command.EXIT) {
                destroyApp(true);
                notifyDestroyed();
            }
            if (s == rsForm) {
                if (c == okRsFrm) {
                    // If Ok command is executed, then show hpForm
                    destroyApp(true);
                    switchDisplayable(null, getHpForm());
                }
            }
        }
        
        public Command getOkRsFrm() {
            if (okRsFrm == null) {
                okRsFrm = new Command("Ok", Command.OK, 0);
            }
            return okRsFrm;
        }
        
        public Form getHpForm() {
            if (hpForm == null) {
                // Http Post Form: Send data to server and returns the result if
                // data is stored successfully on the server's positionData.txt
                hpForm = new Form("HTTP Post Form");
                hpForm.addCommand(new Command("Exit", Command.EXIT, 0));
                hpForm.setCommandListener(this);
                run();
            }
            return hpForm;
        }
    }
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    What do the folks @ Nokia have on this... Did a quick search in Google and found you're not the only one ahving this issue...

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by josephx
      It would be helpful if someone here could point out which part is causing the null exception error. Thanks.
      It would also be helpful if you had copied the complete stack trace of the error; line numbers and method names can be a big help.

      kind regards,

      Jos

      Comment

      Working...