Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (retired 2014) Getting Started with Android Java Variables Explained

Simple App

Hello i have done what you have said but the error is not changing see code !:

package com.example.ballshaker;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Declare out view variables
        TextView answerLable = findViewById(R.id.textView1);
        Button getAnswerButton;

        getAnswerButton.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                // TODO Auto-generated method stub
                String answer = "";




                answerLabel.setText(answer);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

3 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Check out these lines in your onCreate() method:

Button getAnswerButton;
getAnswerButton.setOnClickListener(new View.OnClickListener() {

The first line declares a Button variable but never sets it, meaning it is null, or empty. The next line tries to use it, but it hasn't been set yet. You simply need to set it on the first line in the same manner as how you are setting the TextView variable on the line above it.

Tried it but nothing .. ?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Can you paste in the code you've tried?

package com.example.ballshaker;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ClassBody 
ClassBody
    protected void ClassBody(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Declare out view variables
        TextView answerLable = findViewById(R.id.textView1);
        Button getAnswerButton;
        getAnswerButton.setOnClickListener(new View.OnClickListener() {     

                public void onClick(View v) {
                // TODO Auto-generated method stub
                String answer = "";




                answerLabel.setText(answer);
    }



    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Okay, in this code you still haven't set the Button variable. In the video we end up with this line of code below. Since yours isn't doing this, your Button is never being set, and you get an error trying to do anything with it (like trying to set an OnClickListener to it).

Button getAnswerButton = (Button)findViewById(R.id.button1);