2

Since I have uploaded my app I have received many java.lang.NullPointerException crash reports. I am struggling to understand the problem as it does not crash on the emulators or the physical devices i have personally tested. Below is an example of one of the reports:

java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:2072)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8817)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4914)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at com.bryan.barrett.countdownforandroid.practiceSubscreen.openNumbersSelect(Unknown         Source)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at android.view.View$1.onClick(View.java:2067)
... 11 more
Caused by: java.lang.NullPointerException
... 15 more

I presume this is crashing in the 'practiceSubscreen' when 'openNumbersSelect' button is pressed. This is my piece of code for that button:

public void openNumbersSelect(View view){
    Intent iNum = new Intent(this, numbersSelect.class);
    String gamemode = "practice";   //practice mode
    iNum.putExtra("gamemode", gamemode);
    startActivity(iNum);

    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    if(sound){
        soundPool.play(Main.SNDclick, audio.getStreamVolume(AudioManager.STREAM_MUSIC), 
                audio.getStreamVolume(AudioManager.STREAM_MUSIC), 1, 0, 1f);
    }
} 

and this is the onCreate code in the following numbersSelect class that it is meant to open:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers_select);

        Intent i = getIntent();
        gamemode = i.getStringExtra("gamemode");    
        gameround = i.getIntExtra("gameround", 0) + 1;
        points = i.getIntExtra("points", 0);    

        //Shared Preferences
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        sound = prefs.getBoolean("sound", true);

        //Sounds
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        soundPool = Main.soundPool;

        //Big Numbers
        for (int n=25; n<=100; n+=25){      //add 25, 50, 75, 100
            bignumbers.add(n);              
        }
        //Small Numbers
        for (int m=1; m<=10; m++){      //adds 1-10 twice
            smallnumbers.add(m); 
            smallnumbers.add(m); 
        }

        //Screen metrics
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenw = metrics.widthPixels; int screenh = metrics.heightPixels;
        int unitw = (screenw/14); int unith = (screenh/40); //divide screen into 14 columns x 40 rows...

        num1 = (Button)findViewById(R.id.num1img); nums.add(num1); 
        num2 = (Button)findViewById(R.id.num2img); nums.add(num2); 
        num3 = (Button)findViewById(R.id.num3img); nums.add(num3); 
        num4 = (Button)findViewById(R.id.num4img); nums.add(num4); 
        num5 = (Button)findViewById(R.id.num5img); nums.add(num5); 
        num6 = (Button)findViewById(R.id.num6img); nums.add(num6); 
        for(int n=0; n<6; n++){
            nums.get(n).setHeight(screenh/4); nums.get(n).setTextSize(Main.fontSize); 
            nums.get(n).setWidth(screenw/6); nums.get(n).setPadding(1, 1, 1, 1);
        }
        lrg = (Button)findViewById(R.id.largebutton); lrg.setWidth((unitw*200)/35); 
        sml = (Button)findViewById(R.id.smallbutton); sml.setWidth((unitw*200)/35);

        Typeface scribble = Typeface.createFromAsset(getAssets(), "fonts/scribble.ttf");

        TextView numbersSeltext = (TextView)findViewById(R.id.numbersSeltext);
        numbersSeltext.setTypeface(scribble);
        numbersSeltext.setWidth(screenw); numbersSeltext.setHeight(unith*8);
        if(gamemode.equals("full")){
            numbersSeltext.setText("Round " + gameround + ": Select 6 Numbers");
        }
}

If anyone could shed any light on this it would be greatly appreciated! It is a frustrating problem as it works flawlessly on devices i have tested myself, and it seems to be working fine on a lot of devices based on feedback so im not even sure if there is a problem with the code or if its how certain devices are built.. I can throw up more crash reports or code if needed!

Thanks in advance!

Bryan
  • 23
  • 6
  • Can you expand the part of the stack trace that says `... 15 more` and add it to the question? It might be helpful. – Nick Rempel Apr 23 '13 at 00:33
  • Those "... XX more" messages in the trace are redundant. There's nothing there that wasn't previously revealed by the stack trace. – MarsAtomic Apr 23 '13 at 01:24
  • show onCreate of numberSelect Class – stinepike Apr 23 '13 at 05:12
  • @StinePike I have edited the question to include the onCreate for the numbersSelect class. Any guidance at all would help as I am at a complete loss! – Bryan Apr 23 '13 at 09:42

1 Answers1

0

If I were you, I would test whether all the Buttons num1,num2... and numerous other views are null before they are used, because simply getting a view might raise nullpointerexception if the view is not yet created into existence for whatever reasons (for example, a scrollable listview items only populate views that can fit into screen). It might help more if you let use know what is your layout xml.

Neoh
  • 15,906
  • 14
  • 66
  • 78
  • thanks for the reply. I have the buttons in a linear layout to hold the 6 number buttons, and the global viewgroup is a relative layout.. I use relative layout for most the screens and at this stage I have received a crash report for nearly every screen. An odd behavior i forgot to mention is sometimes when i run the app from eclipse, the view sizes are all wrong (I define the sizes in onCreate in most cases and its as if isnt being called or something). I wonder would this have anything to do with it? It happens very rarely but has happened more than once @Neoh – Bryan Apr 24 '13 at 09:20
  • Your error report doesn't reflect the true source of error. What is the lines below "Caused by: java.lang.NullPointerException ... 15 more", if any? There is the main source you should look at. – Neoh Apr 24 '13 at 16:52
  • That is all the crash report gives and i cant expand the 15 more part.. I have thoroughly checked my code and cant find any null values being passed. It is very peculiar, It has never crashed for me (ZTE blade), and there are no reports of a crash from a Galaxy S3 when i checked (2800 downloads to an s3 and not one s3 crash report even tho i have near 200 reports of this nullPointerException). I will investigate other devices, it has crashed on nexus 7 but for some people it worked fine on their nexus 7. @Neoh – Bryan Apr 24 '13 at 20:35