×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    11

    Thumbs down JME MIDLet returning exception

    Hello members,

    I'm writing a bluetooth mobile advertisement system as my dissertation. In fact I need to retrieve the phone's information and then return it to a PC using sockets. First of all I'm using an Object Oriented approach to solve this problem. When I try to instantiate and object of the class and launch a method of the latter e.g BluetoothServer.Start() this returns null. Bolded characters indicate where exceptions are usually returned.an exception returned by NetBeans is:

    TRACE: <at java.lang.NullPointerException: 0>, startApp threw an Exception
    java.lang.NullPointerException: 0
    at BluetoothServer.sendMessage(BluetoothServer.java:75)
    at PhoneInfo.startApp(PhoneInfo.java:63)
    at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(), bci=1
    at com.sun.midp.midlet.MIDletPeer.startApp(), bci=7
    at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=269
    at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52
    at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8
    at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161
    at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
    java.lang.NullPointerException: 0
    at BluetoothServer.sendMessage(BluetoothServer.java:75)
    at PhoneInfo.startApp(PhoneInfo.java:63)
    at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(), bci=1
    at com.sun.midp.midlet.MIDletPeer.startApp(), bci=7
    at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=269
    at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52
    at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8
    at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161
    at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26

    My Source code:

    BluetoothServer.java:

    import java.io.DataOutputStream;
    import java.io.IOException;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
    import javax.microedition.io.StreamConnectionNotifier;
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    public class BluetoothServer
    {
    private String UUID;
    private String conProtocol;
    private String conTarget;
    private String serviceName;

    private String serverConnection;

    private StreamConnectionNotifier scn;
    private StreamConnection sc;

    private Thread t1;

    private Alert alert;
    private DataOutputStream out;
    public BluetoothServer()
    {
    UUID = "12345678901234567890123456789012";
    conProtocol = "btspp"; //service profile protocol
    conTarget = "localost"; //since this is the server, localhost is our best bet
    serviceName = "TopSecret";
    serverConnection = conProtocol+"://"+conTarget+":"+UUID+";"+"name="+serviceName;
    }

    public void startServer()
    {
    try
    {
    scn = (StreamConnectionNotifier) Connector.open(serverConnection);
    sc = (StreamConnection) scn.acceptAndOpen();
    t1 = new Thread((Runnable) this);
    t1.start();
    }
    catch (IllegalThreadStateException threadEx)
    {
    showAlert("Thread exception");
    }
    catch (Exception e)
    {
    showAlert("StreamConnectionNotifier failed!");
    }
    }
    public void stopServer()
    {
    try
    {
    if (scn != null)
    scn.close();
    sc.close();
    }
    catch (Exception e)
    {
    showAlert("Could not stop server");
    }
    }
    public void startStreaming()
    {
    }
    public void sendMessage(String msg) throws IOException
    {
    try {
    out = sc.openDataOutputStream();
    out.writeChars(msg);
    out.flush();
    out.close();
    }
    catch (IOException ex)
    {
    ex.printStackTrace();
    }


    }
    public void showAlert(String message)
    {
    alert = new Alert("Alert",message,null,AlertType.INFO);
    }

    }

    --------------------------------------------------------------
    PhoneInfo.java (Main MIDLet)

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    import java.io.IOException;
    import javax.microedition.location.LocationException;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.bluetooth.*;
    import java.lang.*;
    import java.util.Timer;
    import java.util.TimerTask;


    /**
    * @author Yasir
    */

    public class PhoneInfo extends MIDlet implements CommandListener
    {
    public Display current;
    public Form form;
    public StringItem str1, str2;


    private Command cmdExit;
    private Command cmdSend;
    private Command cmdOK;

    private Command cmdText;
    private TextBox t;

    private DiscoveryAgent agent;
    private Thread t2;


    List devList = new List("List of devices", List.IMPLICIT);

    public PhoneInfo()
    {
    current = Display.getDisplay(this);
    form = new Form("Device information");
    cmdExit = new Command("Exit",Command.EXIT,1);
    cmdOK = new Command("Ok", Command.SCREEN,2);
    cmdText = new Command("Show TextBox",Command.SCREEN,3);
    str1 = new StringItem("Phone Information","Press OK to send");
    str2 = new StringItem("Test data","");

    }
    public void startApp()
    {
    current.setCurrent(form);
    form.addCommand(cmdOK);
    form.addCommand(cmdExit);
    form.addCommand(cmdText);
    current.setCurrent(form);
    form.setCommandListener(this);

    BluetoothServer myServer = new BluetoothServer();
    myServer.startServer();
    try {
    myServer.sendMessage("hello");
    } catch (IOException ex) {
    ex.printStackTrace();
    }


    }


    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
    notifyDestroyed();

    }

    public void commandAction(Command c, Displayable d)
    {
    if (c==cmdOK)
    {
    }
    else if (c==cmdExit)
    {
    destroyApp(false);
    }
    else if (c == cmdText)
    {
    Thread t1;
    RemoteDeviceDiscovery rdd = new RemoteDeviceDiscovery();
    t1 = new Thread((Runnable) rdd);
    t1.start();
    rdd.discover();

    }

    }


    public void showAlert(String message)
    {
    Alert alert = new Alert("Alert",message,null,AlertType.INFO);
    current.setCurrent(alert,form);
    }

    class RemoteDeviceDiscovery
    {
    DiscoveryAgent agent;
    LocalDevice local = null;

    private List device;

    public RemoteDeviceDiscovery()
    {
    try
    {
    local = LocalDevice.getLocalDevice();
    }
    catch (BluetoothStateException ex)
    {
    Alert alert = new Alert("Error retrieving local device!Did you switch on Bluetooth?");
    }
    }

    public void discover()
    {
    agent = local.getDiscoveryAgent();
    try {
    agent.startInquiry(DiscoveryAgent.GIAC, (DiscoveryListener) this);
    current.setCurrent(form);
    form.append("Discovery started");
    } catch (BluetoothStateException ex)
    {
    showAlert("Exception when starting discovery!");
    }

    }
    public void deviceDiscovered(RemoteDevice dev, DeviceClass cod)
    {
    String address = dev.getBluetoothAddress();
    device.insert(0, address, null);
    //form.append("Device Address:"+address+"\n");
    current.setCurrent(device);

    }
    public void inquiryCompleted(int arg0)
    {
    form.append("Inquiry completed");

    }

    public void serviceSearchCompleted(int arg0, int arg1)
    {

    }

    public void cancelInquiry()
    {
    agent.cancelInquiry((DiscoveryListener) this);
    try
    {
    local.setDiscoverable(agent.NOT_DISCOVERABLE);
    } catch (BluetoothStateException ex)
    {
    showAlert("Could not set device mode to hidden!");
    }
    }
    public void servicesDiscovered(int arg0, ServiceRecord[] arg1)
    {

    }

    }


    }

    Please help me out there. I need some guidance pros.

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

    Re: JME MIDLet returning exception

    "sc" is null, because an exception is thrown (and caught and ignored) in startServer before the object gets created.

    Some advice:

    1. Don't do anything time-consuming (like opening connections) in startApp(). startApp() is an "event", and event methods must always return as quickly as possible. Until an event method returns, no other event can be delivered (they wait in a queue), which tends to result in your application becoming unresponsive. You won't be able to process any input, or generate any output. Time-consuming work (reading or writing data, connecting to networks, etc.) should be done in a separate thread.

    2. Find a good example of using JSR82 (Bluetooth API). Check for how your emulator handles running Bluetooth-enabled applications, and try running some demos.

    3. Find an example of using Threads. An object is only runnable if it implements the Runnable interface. You cannot make it runnable by reference casting.

    4. Be very cautious about using class-casts. Don't just put one in (as you have done casting your BluetoothServer and RemoteDeviceDiscovery objects to Runnable) to make a compiler error go away. Compiler errors are not there just to annoy you. They're there to tell you you've done something wrong. All you've done by adding that cast to Runnable is change a compiler error into a runtime error (it will throw a ClassCastException). Runtime errors are much harder to diagnose.

    Graham.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    11

    Re: JME MIDLet returning exception

    Quote Originally Posted by grahamhughes View Post
    "sc" is null, because an exception is thrown (and caught and ignored) in startServer before the object gets created.

    Some advice:


    2. Find a good example of using JSR82 (Bluetooth API). Check for how your emulator handles running Bluetooth-enabled applications, and try running some demos.

    3. Find an example of using Threads. An object is only runnable if it implements the Runnable interface. You cannot make it runnable by reference casting.

    4. Be very cautious about using class-casts. Don't just put one in (as you have done casting your BluetoothServer and RemoteDeviceDiscovery objects to Runnable) to make a compiler error go away. Compiler errors are not there just to annoy you. They're there to tell you you've done something wrong. All you've done by adding that cast to Runnable is change a compiler error into a runtime error (it will throw a ClassCastException). Runtime errors are much harder to diagnose.

    Graham.

    Thanks for replying, I did have a look @ JSR samples but all the examples make use of forms my friend and in my scenario, there should be only 1 form and not more than this. It all should be automated in the following case:

    As soon as the user launches the app it should do the following:

    1. retrieve phone information (make/model/firmware using System.getProperty(), MAC address of the device)
    2. Start a thread for starting inquiry of nearby bluetooth devices and end the thread after inquiry completed
    3. Use stream connection to send the retrieved information above.

    That's what I've been trying to do since a very long time and this exception is bugging me. How can I change my code to free it from the exceptions above?

    Regards,
    Yasir

Similar Threads

  1. VS.Net 2003 Carbide 2.01 and epoc32.exe
    By ValentinK in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 2
    Last Post: 2007-01-12, 12:31
  2. plz help me be clear about signing midlet
    By pom_cc in forum Mobile Java General
    Replies: 5
    Last Post: 2006-12-12, 01:51
  3. security exception on datagram open despite signed midlet
    By dwigdor in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2005-08-22, 00:21
  4. 6230 Signed MIDlet exception
    By johanmeyer in forum Bluetooth Technology
    Replies: 0
    Last Post: 2004-06-18, 07:51
  5. Exception when playing 3gp files in MediaSampler MIDlet
    By adsr in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2004-06-09, 12:32

Posting Permissions

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