Why does my Android program crash when I access the Orientation sensor (HTC Desire)?
<p>So I am playing around with the Android api, and am trying to figure out how to get the heading the phone is pointing towards.</p>

<p>I have the following code:</p>

<pre><code>private SensorManager mSensorManager;
private Sensor mAcceller;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    checkNotNull(mSensorManager);
    try {
        mAcceller = mSensorManager.getSensorList(3).get(0);
    } catch (Exception e) {
        showToast(e.getMessage(), Toast.LENGTH_LONG);
    }
}
</code></pre>

<p>But when I try to run it on my phone I just get a message that the program has crashed. When I run it in the emulator it tells me that there are no sensors (thats to be expect, but it means I can't test the code).</p>

<p>Does anybody know how to work around this issue? I know the error happens in the try statement because if I comment it out, the program runs.</p>

<p><em>Edit</em>: It turns out that I am an idiot, the crash happened when I null refered somewhere else in my code, as a response to getting a notification that the sensor had been turned on.</p>

<p>Dave if you upgrade your comment to an answer, I will mark that as accepted (since I found the problem as soon as I figured out how the debugger worked).</p>
