Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7600565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:56:27+00:00 2026-05-30T22:56:27+00:00

I’m getting an error when trying to implement an onClickListener, this is the error:

  • 0

I’m getting an error when trying to implement an onClickListener, this is the error:
03-06 19:21:20.432: E/AndroidRuntime(1197): Caused by: java.lang.NullPointerException
03-06 19:21:20.432: E/AndroidRuntime(1197):at com.coursework.braintrain.Game.Easy12(Game.java:186)

and this is the line that is causing it: buttonhash.setOnClickListener(new OnClickListener(){

Basically I made buttonhash in the onCreate() method, but I want to make it so that when the button is pressed, it listens to whether the users input matches the actual answer of the equation. Maybe if I just put the code up it will make more sense (not good at explaining…)

public class Game extends Activity {
private static final String TAG = "Brain Training";

public static final String KEY_DIFFICULTY = "com.coursework.braintrain.difficulty";
public static final int DIFFICULTY_EASY = 0;
public static final int DIFFICULTY_MEDIUM = 1;
public static final int DIFFICULTY_HARD = 2;
public static final int DIFFICULTY_GURU = 3;


private int brain;

private EditText edittext;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private Button button0;

private TextView answerLabel;

private Button buttonhash;



@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate");
    setContentView(R.layout.gamelayout);

    int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
    getGame(diff);


    edittext = (EditText) findViewById(R.id.USERentry);

    answerLabel = (TextView) findViewById(R.id.rightwrong_label);

    button1 = (Button) findViewById(R.id.keypad_1);
    button1.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            // Perform action on clicks
            //String buttonText = (String) button.getText();
            edittext.setText(edittext.getText() + "1");
            //edittext.setText() = edittext.getText() + "1";
        }
    });

    button2 = (Button) findViewById(R.id.keypad_2);
    button2.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "2");
        }
    });

    button3 = (Button) findViewById(R.id.keypad_3);
    button3.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "3");
        }
    });

    button4 = (Button) findViewById(R.id.keypad_4);
    button4.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "4");
        }
    });

    button5 = (Button) findViewById(R.id.keypad_5);
    button5.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "5");
        }
    });

    button6 = (Button) findViewById(R.id.keypad_6);
    button6.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "6");
        }
    });

    button7 = (Button) findViewById(R.id.keypad_7);
    button7.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "7");
        }
    });

    button8 = (Button) findViewById(R.id.keypad_8);
    button8.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "8");
        }
    });

    button9 = (Button) findViewById(R.id.keypad_9);
    button9.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "9");
        }
    });

    button0 = (Button) findViewById(R.id.keypad_0);
    button0.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "0");
        }
    });

    buttonhash = (Button) findViewById(R.id.keypad_hash);


    //final Button buttonDel = (Button) findViewById(R.id.keypad_del);
    //buttonDel.setOnClickListener(new OnClickListener(){
        //public void onClick(View v){
            //edittext.setText(edittext.getText() - 1);

        //}
    //});
}

private void getGame(int diff){

    //TODO: Continue last game
    switch(diff){
    case DIFFICULTY_HARD:

        break;
    case DIFFICULTY_MEDIUM:

        break;
    case DIFFICULTY_EASY:
        Easy12();
        break;
    }
    //might require to return an array or something

}

public void Easy12(){
    Random rand = new Random();
    int a = (int) rand.nextInt(100)+1;
    int b = (int) rand.nextInt(100)+1;
    final TextView tv = (TextView) findViewById(R.id.question_label);
    String aString = Integer.toString(a);
    String bString = Integer.toString(b);
    String display = aString + " + " + bString + " =";
    tv.setText(display);
    final int c = a + b;
    buttonhash.setOnClickListener(new OnClickListener(){ //HERE IS THE ERROR!!

        public void onClick(View v) {

            if(edittext.getText().toString().equals(String.valueOf(c))){

                answerLabel.setText(R.string.answer_correct);
                answerLabel.setTextColor(R.color.correct_color);
                //answerLabel.setBackgroundColor(R.color.background_answer);
            }
            else
            {

                answerLabel.setText(R.string.answer_wrong);
                answerLabel.setTextColor(R.color.wrong_color);
                //answerLabel.setBackgroundColor(R.color.background_answer);
            }
        }
    });

}

}

My XML is fine if that is one of the things that might be causing it but I don’t think it has anything to do with it anyway 😛

So my problem is getting a nullpointerexception at the line where I set my buttonhash’s onClickListener, which is in another method (Easy12()). Any help on what might be causing this/how to fix it would be much appreciated. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:56:29+00:00Added an answer on May 30, 2026 at 10:56 pm

    You’re calling getGame() before defining your button, so it’s null.
    Put the button declaration above getGame like this:

    buttonhash = (Button) findViewById(R.id.keypad_hash);
    getGame(diff)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.