-1

I am trying to implement a java class that keeps football facts. it allows the user to click an image button in order to increment a value. a keep a different int value for each button, at when the user clicks on the image button, the counter is incremented and then showed on individual editTexts. I get a null point exception when the activity runs. can somebody why I get this null point exception at line 68?

package com.example.buttondemo;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.EditText;
import android.app.Activity;

public class FootballFacts extends Activity implements OnClickListener {

    ImageButton btnGoalBlue,btnGoalRed,btnFoulBlue,btnFoulRed,btnCornerBlue,btnCornerRed,btnYellowBlue,btnYellowRed,
    btnRedBlue,btnRedRed, btnRestartValues;
   EditText textGoalBlue,textGoalRed,textFoulBlue,textFoulRed,textYellowBlue,textYellowRed,textRedBlue,
   textRedRed,textCornerBlue,textCornerRed;
   int counterBlueGoal,counterRedGoal,counterCornerBlue,counterCornerRed,counterFoulBlue,counterFoulRed,counterYellowBlue,counterYellowRed,
   counterRedBlue,counterRedRed= 0;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_football_facts);
       //find the buttons in layout
       btnGoalBlue = (ImageButton)findViewById(R.id.btnGoalBlue);
       btnGoalRed = (ImageButton)findViewById(R.id.btnGoalRed);

       btnFoulBlue = (ImageButton)findViewById(R.id.btnFoulBlue);       
       btnFoulRed = (ImageButton)findViewById(R.id.btnFoulRed);

       btnCornerBlue = (ImageButton)findViewById(R.id.btnCornerBlue);
       btnCornerRed = (ImageButton)findViewById(R.id.btnCornerRed);

       btnYellowBlue = (ImageButton)findViewById(R.id.btnYellowBlue);
       btnYellowRed = (ImageButton)findViewById(R.id.btnYellowRed);

       btnRedBlue = (ImageButton)findViewById(R.id.btnRedBlue);
       btnRedRed = (ImageButton)findViewById(R.id.btnRedRed);

       btnRestartValues = (ImageButton)findViewById(R.id.btnResetFacts);

       //textfields
       textGoalBlue = (EditText)findViewById(R.id.editTextGoalBlue);
       textGoalRed = (EditText)findViewById(R.id.editTextGoalRed);

       textFoulBlue = (EditText)findViewById(R.id.editTextFoulBlue);
       textFoulRed = (EditText)findViewById(R.id.editTextFoulRed);

       textYellowBlue = (EditText)findViewById(R.id.editTextYellowBlue);
       textYellowRed = (EditText)findViewById(R.id.editTextYellowRed);

       textRedBlue = (EditText)findViewById(R.id.editTextRedBlue);
       textRedRed = (EditText)findViewById(R.id.editTextRedRed);

       textCornerBlue = (EditText)findViewById(R.id.editTextCornerBlue);
       textCornerRed = (EditText)findViewById(R.id.editTextCornerRed);



       //---set on click listeners on the buttons-----
       btnGoalBlue.setOnClickListener(this);
       btnGoalRed.setOnClickListener(this);
       btnCornerBlue.setOnClickListener(this);
       btnCornerRed.setOnClickListener(this);
       btnFoulBlue.setOnClickListener(this);
       btnFoulRed.setOnClickListener(this);
       btnYellowBlue.setOnClickListener(this);
       btnYellowRed.setOnClickListener(this);
       btnRedBlue.setOnClickListener(this);
       btnRedRed.setOnClickListener(this);
       btnRestartValues.setOnClickListener(this);

   }

   @Override
   public void onClick(View v) {
       if (v == btnGoalBlue){
           counterBlueGoal++;
           textGoalBlue.setText(Integer.toString(counterBlueGoal));
       }
       if (v == btnGoalRed){
           counterRedGoal++;
           textGoalRed.setText(Integer.toString(counterRedGoal));
       }
       if (v == btnFoulBlue){
           counterFoulBlue++;
           textFoulBlue.setText(Integer.toString(counterFoulBlue));
       }
       if (v == btnFoulRed){
           counterFoulRed++;
           textFoulRed.setText(Integer.toString(counterFoulRed));
       }
       if (v == btnCornerBlue){
           counterCornerBlue++;
           textCornerBlue.setText(Integer.toString(counterCornerBlue));
       }
       if (v == btnCornerRed){
           counterCornerRed++;
           textCornerRed.setText(Integer.toString(counterCornerRed));
       }
       if (v == btnYellowBlue){
           counterYellowBlue++;
           textYellowBlue.setText(Integer.toString(counterYellowBlue));
       }
       if (v == btnYellowRed){
           counterYellowRed++;
           textYellowRed.setText(Integer.toString(counterYellowRed));
       }
       if (v == btnRedBlue){
           counterRedBlue++;
           textRedBlue.setText(Integer.toString(counterRedBlue));
       }
       if (v == btnRedRed){
           counterRedRed++;
           textRedRed.setText(Integer.toString(counterRedRed));
       }


       if (v == btnRestartValues){
           counterBlueGoal = 0;
           counterRedGoal = 0;
           counterCornerBlue = 0;
           counterCornerRed = 0;
           counterYellowBlue = 0;
           counterYellowRed = 0;
           counterRedBlue = 0;
           counterRedRed = 0;
           counterFoulBlue = 0;
           counterFoulRed = 0;

           textGoalBlue.setText(Integer.toString(counterBlueGoal));
           textGoalRed.setText(Integer.toString(counterRedGoal));

           textYellowBlue.setText(Integer.toString(counterYellowBlue));
           textYellowRed.setText(Integer.toString(counterYellowRed));

           textRedBlue.setText(Integer.toString(counterRedBlue));
           textRedRed.setText(Integer.toString(counterRedRed));

           textCornerBlue.setText(Integer.toString(counterCornerBlue));
           textCornerRed.setText(Integer.toString(counterCornerRed));

           textFoulBlue.setText(Integer.toString(counterFoulBlue));
           textFoulRed.setText(Integer.toString(counterFoulRed));

       }
   }

}

logcat file

-03 14:39:10.959: D/AndroidRuntime(7371): Shutting down VM
02-03 14:39:10.959: W/dalvikvm(7371): threadid=1: thread exiting with uncaught exception (group=0x415e6ce0)
02-03 14:39:10.969: E/AndroidRuntime(7371): FATAL EXCEPTION: main
02-03 14:39:10.969: E/AndroidRuntime(7371): Process: com.example.buttondemo, PID: 7371
02-03 14:39:10.969: E/AndroidRuntime(7371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttondemo/com.example.buttondemo.FootballFacts}: java.lang.NullPointerException
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.os.Looper.loop(Looper.java:136)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread.main(ActivityThread.java:5146)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at java.lang.reflect.Method.invoke(Method.java:515)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at dalvik.system.NativeStart.main(Native Method)
02-03 14:39:10.969: E/AndroidRuntime(7371): Caused by: java.lang.NullPointerException
02-03 14:39:10.969: E/AndroidRuntime(7371):     at com.example.buttondemo.FootballFacts.onCreate(FootballFacts.java:68)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.Activity.performCreate(Activity.java:5231)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-03 14:39:10.969: E/AndroidRuntime(7371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
02-03 14:39:10.969: E/AndroidRuntime(7371):     ... 11 more
cmario
  • 605
  • 1
  • 7
  • 22
  • You are getting Null Pointer explanation when you are setting onclick listener on one of the buttons. So you should check whether your button objects are getting initialized or not. Which ever button object is NULL , you need to check whether it has the correct id and present on the view of your activity. – theJango Feb 03 '15 at 14:49
  • Is it falling over when it is trying to add an OnClickListener to btnYellowRed? If so check that this button is in your layout file. It may not be finding it when you run btnYellowRed = (ImageButton)findViewById(R.id.btnYellowRed); in line 35. – David Christopher Reynolds Feb 03 '15 at 14:49
  • is line 68 this? > btnYellowRed.setOnClickListener(this); – Andy B Feb 03 '15 at 14:50
  • Are you sure you are using correct id's. I mean are they in the same xml you are using in `setContentView()`? – Rohit5k2 Feb 03 '15 at 14:51
  • can you provide view definition – user902383 Feb 03 '15 at 14:52

2 Answers2

0

All you have to do is go to line 60 and make sure that all of your names and id's are correct. If this doesn't work. I'm still investigating this, but I believe there may be a small bug in android studio initializing certain views. If/when it happens to me I check the id's, if that doesn't work restart the program, and if that doesn't work then change the names for the buttons having the issue. Usually the second steep fixes the issue for me. Hope this helped!

Ethan
  • 225
  • 2
  • 13
0

From Activity:

  /**
 * Finds a view that was identified by the id attribute from the XML that
 * was processed in {@link #onCreate}.
 *
 * @return The view if found or null otherwise.
 */
public View findViewById(int id) {
    return getWindow().findViewById(id);
}

It seems that returns null for you. Is that view located in the same layout you set in setContentView()?

The problem seems to be in your layout XML file rather than in the code. This one looks fine.

mhenryk
  • 551
  • 5
  • 13
  • This doesn't answer OP question. Comments are used for clarifications. You ll be able to use them once you get enough reputation. – Rohit5k2 Feb 03 '15 at 14:55
  • Thank you for your comment. The improper location of view in layouts is a possible cause for such issues so I think it does answer the question. Perhaps I stated it wrongly so it looks more like additional clarification is required rather than an actual suggestion. Anyway hopefully it helped. – mhenryk Feb 03 '15 at 15:04