2

I have been slowing learning and building my first android app. I'm VERY new to java but have already done a couple of projects in C#, VB.NET (back in the day), Objective-C (have 6 apps in the store) and Fortran (waaaaaaaaaaaaaaaaaaaay back in the day ;)

So I just received from overseas a htc legend (I'm not in the US), which i bought in order to have a decent mid-level device for development (it's running non-rooted adnroid 2.1)

The application I have been developing is target level 4 (android 1.6). It uses a 5 Mb sqlite3 database with a .mp3 extension to avoid compression within the apk and proper copying from assets to system folder.

It all works fine on the emulator, and on the device I see that the file size of the app after copying the database matches exactly what I see on the emulator.

now, on my main activity with a list view and a spinner, I bind some data through two array adapters. when running on the device all does smoothly. but when trying to run on the device this part of the code:

public class mainAct extends Activity implements OnItemSelectedListener, TextWatcher, OnItemClickListener
{   
        /** members */
        //private EditText searchtext;
        private ListView designations;
        private ArrayAdapter<String> adapterShapes;
        private ArrayAdapter<String> adapterTypes;
        private Spinner types;
      .  
      .  
      .    
    public void onCreate(Bundle savedInstanceState)   
    {  
      .  
      .  
      .    
        // DESIGNATIONS  
        // 
        adapterShapes = new ArrayAdapter<String>(this,R.layout.list_item,shapes);       // custom TextView Adapter  
        designations=(ListView)findViewById(R.id.designations);
        Log.e("MAIN.ACCT", "ok to 172");
        designations.setAdapter(adapterShapes);
        Log.e("MAIN.ACCT", "ok to 174");
        designations.setOnItemClickListener(this);

        // TYPES
        //
        adapterTypes=new ArrayAdapter<String>(this,R.layout.spinner_item,DT.get().typesInLibrary);
        types=(Spinner)findViewById(R.id.types);
        types.setAdapter(adapterTypes);
        types.setOnItemSelectedListener(this);
      .  
      .  
      .  
    }
}

Both designations.setAdapter(adapterShapes);
& types.setAdapter(adapterTypes);

give me a Null Pointer exception.

I'm using eclipse under mac, the LogCat window throws:

06-25 18:41:37.842: ERROR/AndroidRuntime(9523): Uncaught handler: thread main exiting due to uncaught exception

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.davidhomes.steel/com.davidhomes.steel.mainAct}: java.lang.NullPointerException

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.ActivityThread.access$2200(ActivityThread.java:126)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.os.Handler.dispatchMessage(Handler.java:99)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.os.Looper.loop(Looper.java:123)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.ActivityThread.main(ActivityThread.java:4595)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at java.lang.reflect.Method.invokeNative(Native Method)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at java.lang.reflect.Method.invoke(Method.java:521)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at dalvik.system.NativeStart.main(Native Method)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): Caused by: java.lang.NullPointerException


-------------------------------------------------------------- 06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at com.davidhomes.steel.mainAct.onCreate(mainAct.java:183)

--------------------------------------------------------------

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)

06-25 18:41:37.891: ERROR/AndroidRuntime(9523): ... 11 more

06-25 18:46:38.252: ERROR/ActivityManager(99): fail to set top app changed!

Line 183 is the first setAdapter call (designations.setAdapter(adapterShapes);), when I comment it out the second setAdapter is the one breaking code

I'm a little lost here, the adapters show the proper number of items on the log window when running from the simulator and the device.

I admit to being a noob to both java and android, so any help is highly appreciated.

regards
david

4 Answers 4

4

Well, as you've probably figured out yourself, some variable is null. Unfortunately, there is no obvious source of the NullPointerException in your code.

Therefore, you should first try to identify what variable is null, and hence causing the exception.

For example, findViewById returns null if it cannot find the view, so you may want to double check your ListView and Spinner are being initialised properly.

Of course, the problem may be with your ArrayAdapters, so you should also check them, but from your question it sounds as if you've already done that.

Once you (and we) know exactly where the NullPointerException is occurring, it will be easier to give more specific advice

Sign up to request clarification or add additional context in comments.

8 Comments

Hi Chris, your input in appreciated, i'm just sitting down again to try and figure it out. What throws me off is that everything runs fine on the simulator, so why would a variable go null when running on the device if it wasn't some sort of the bundled sqlite3 database not being properly moved from the assets folder (and using a pre-existing database, especially one bigger than 1Mb is a complete topic on its own). In any case, I'm just about to work on this tonight, if anyone has some tips on additional debugging features on eclipse that I may be missing that would be appreciated.
it's definitely not in the adapters, a Log.e on them provides the proper number of elements (so they are indeed being filled from the database) Log.e("SHAPES SIZE",String.format("%d",shapes.size())); gives: 06-25 21:14:52.701: ERROR/SHAPES SIZE(11447): 1973
chris, you were right, my ListView designations:: designations=(ListView)findViewById(R.id.designations); if(designations==null) { Log.e("designation null :: ","null"); } throws an error. I still don't know why it works fine on emulator though
You mention your device is running 2.1, but you're targeting 1.6. Are you using the 2.1 emulator to match your device, or are you using the 1.6 emulator to match your target API level?
chris, I'm using 1.6 on emulator as you mentioned since that is actually my target minSDKversion, but I will test it on a new avd with 2.1 right away. unfortunately, i will be leaving on a short weekend leisure trip and wont be able to work on this again until late sunday at the earliest.
|
3

Oops! it was my fault all along, somehow I had TWO layouts (one for different resolutions), the one being used on my device did not have the proper ListView and Spinner ID. That alternative layout was collapse into a folder and I just forgot about it (I had paused development of the app for about 2 month until I actually got a device to test on).

the one working fine was installing on the simulator but not on the device and vice-versa, weird as the good one matches the resolution of my device (I'm pretty sure I'm also missing something there but that's not important right now)

Still, thanks Chris for pointing me to looking at the Null return value on findViewById. Being new to android I was lost as to were to begin

Best regards david

Comments

1

I know this is a long closed subject. However I just came across the same problem and this post did not answer it.

What i did, was very silly. I forgot to add a reference to the xml. ie. setContentView(R.layout.settings);

it actually looks like the original post might have also forgotten this so i thought I would just post it. Cheers.

Comments

1

I had the same problem. I put Log.d messages in printing out every object. It turned out that my ListView object was "null". The reason for this was I had the wrong XML activity listed in my SetContentView. I think the easiest way to debug this problem is keep putting in Log.d messages until you find the object/variable that is "null".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.