Hello guys! I'm currently working on a matching app. In the app, the user can select what mode of difficulty they want(easy, medium, hard and suvival) as well as select one of the sets of images for the GridView(nba logos or world flags). My problem is that when the user selects their preferred difficulty and image set, the app stops working. I'm getting a NullPointerException. Here's my code:

 View.OnClickListener startGame = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            Bundle b = getIntent().getExtras();
            Bundle d = getIntent().getExtras();

            if(b != null){
                String mode = b.getString("mode");
                String logo = d.getString("logo");


                if(mode.equals("easy") && logo.equals("nba")) {
                    i.setClass(getBaseContext(), PlayEasyNBA.class);
                    startActivity(i);

                }
                else if(mode.equals("medium") && logo.equals("nba")){
                    i.setClass(getBaseContext(), PlayMediumNBA.class);
                    startActivity(i);
                }
                else if(mode.equals("hard") && logo.equals("nba")){
                    i.setClass(getBaseContext(), PlayHardNBA.class);
                    startActivity(i);
                }
            }


        }
    };

In this activity, the user chooses which image set they want:

    View.OnClickListener nba = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            choiceLogo = "nba";
            Intent i = new Intent();
            i.putExtra("logo", choiceLogo);
            i.setClass(getBaseContext(), Choose.class);
            startActivity(i);
            Toast.makeText(getApplicationContext(), "NBA Teams selected!",
                    Toast.LENGTH_SHORT).show();
            finish();
        }
    };


    View.OnClickListener flags = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            choiceLogo = "flags";
            Intent i = new Intent();
            i.putExtra("logo", choiceLogo);
            i.setClass(getBaseContext(), Choose.class);
            startActivity(i);
            Toast.makeText(getApplicationContext(), "Flags of the World selected!",
                    Toast.LENGTH_SHORT).show();
            finish();
        }
    };

The NullPointerException is right before the if loop. The app works well if I code it this way: if(mode.equals("easy")) { but with this, it would only display the Easy difficulty with the NBA image set.
Any advice?

Recommended Answers

All 2 Replies

  1. There is only one Bundle so no point for creating second (first snippet line 5 & 6)
  2. Use containsKey(String key) to see if bundle contains mode or logo and avoid getting NPE because bundle will return null if mode not found withgetString("mode")
  3. Since I didn't see rest of the code I can only asume given above that Bundle value for mode will be null as never set, hence failing condition

The NullPointerException is right before the if loop. The app works well if I code it this way: if(mode.equals("easy")) { but with this, it would only display the Easy difficulty with the NBA image set.

there is no such thing as an if-loop, only an if-statement. This means that your logo is not instantiated, meaning d.getString("logo")returns null.

Not that this will solve all your problems, but a recommended way of working in these cases is like this:

if ( myVariable.equals("Text"))

here, you have two variables: myVariable and "Text", which is a String literal. It is possible for myVariable to be null, but impossible for the used literal to be null. So, write it like this:

if ( "Text".equals(myVariable))

The result is exactly the same, except for the possibility of NullPointerExceptions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.