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

Skurmedel
  • 21,515
  • 5
  • 53
  • 66
David Homes
  • 2,725
  • 8
  • 33
  • 53

4 Answers4

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

chrisbunney
  • 5,819
  • 7
  • 48
  • 67
  • 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. – David Homes Jun 26 '10 at 01:15
  • 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 – David Homes Jun 26 '10 at 01:51
  • 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 – David Homes Jun 26 '10 at 02:20
  • 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? – chrisbunney Jun 26 '10 at 07:48
  • 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. – David Homes Jun 26 '10 at 08:33
  • nope, works like a charm on an emulator with 2.1 as well :S. feeling a bit frustrated by now, I'm debating on whether or not to go back to objC/iphone development which at least I know fairly well by now. Is there any reason why the compile R.java file wouldn't be moved / compiled with the apk to the device (some sort of permission needed to be set)? Maybe there is something I'm missing on my manifest (just throwing rocks in the air as I'm lost really). btw, appreciate your interest chris. – David Homes Jun 26 '10 at 08:50
  • I agree these problems are frustrating. You could try a clean build of the project to clear out any issues with things not being compiled correctly. Also, you may want to try loading the layout and getting the Spinner and ListView at the beginning of the method just in case that has any weird effects. If possible try running on a different device to see if it's just the HTC Legend. Unfortunately I don't know any good reason for this behaviour, and am running out of ideas :\ – chrisbunney Jun 26 '10 at 09:09
  • same here chris, i will follow on you last tips but if things keep like this i will just go back to code for iphone / ipad at the moment, I have an application about 35% done there that I put on hold just to test android. But the more income i can get in the next few months the better as I'm getting married soon. – David Homes Jun 28 '10 at 13:54
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

David Homes
  • 2,725
  • 8
  • 33
  • 53
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.

Aiden Fry
  • 1,672
  • 3
  • 21
  • 45
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".

UFGator
  • 55
  • 7