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

How to reach a variable from outside the function in Android

I'm trying to create a function that will use an EditText variable that is not a parameter of the function (or it's called method, please correct me). I know that with java I use static for the variable and declare the variable from outside a class, but it gives me all kind of errors, how can I use this variable in the function? Here is the link to my project on GitHub: https://github.com/ishay1999/FunFacts

and here is the code for MainActivity: (There is a new version down below!)

package com.howtoevery.funfacts;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends ActionBarActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Creating the random
        final Random rand = new Random();


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView factLabel;
        final Button showFactButton;

        factLabel = (TextView) findViewById(R.id.fact);
        final ViewGroup backGround = (RelativeLayout) findViewById(R.id.mainBackground);
        showFactButton = (Button) findViewById(R.id.factButton);

        View.OnClickListener myListener = new View.OnClickListener() {

            int randomNumber;
            int currentState = 0;

            @Override
            public void onClick(View view) {

                do {
                    randomNumber = rand.nextInt(2); // randomNumber will be 0 or 1
                }   while (currentState == randomNumber);

                currentState = randomNumber;

                switch (randomNumber) {
                    case 0: { // in case it is 0, show fact number 1 with green color
                        setFact(R.string.fact1, R.color.android_green);
                    } break;

                    case 1: { // in case it is 1, show fact number 2 with orange color
                        setFact(R.string.fact2, R.color.orangish);

                    } break;
                }

          //      factLabel.setText(String.valueOf(randomNumber)); // show the number generated with randomNumber, ONLY FOR DEBUG PURPOSES

            }
        };

        showFactButton.setOnClickListener(myListener);
    }
    public void setFact(int factString, int color) {
        factLabel.setText(factString);
        backGround.setBackgroundColor(getResources().getColor(color));
        showFactButton.setTextColor(getResources().getColor(color));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

I just changed it and it looks like it should work but when I click the button the app crashes and gives me this error in the catLog:

06-09 12:09:48.155 1756-1756/com.howtoevery.funfacts E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache 06-09 12:09:48.159 1756-1756/com.howtoevery.funfacts E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384 06-09 12:09:48.171 1756-1756/com.howtoevery.funfacts E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 06-09 12:09:48.171 1756-1756/com.howtoevery.funfacts E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384 06-09 12:09:53.651 1756-1756/com.howtoevery.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.howtoevery.funfacts, PID: 1756 java.lang.NullPointerException at com.howtoevery.funfacts.MainActivity.setFact(MainActivity.java:72) at com.howtoevery.funfacts.MainActivity$1.onClick(MainActivity.java:58) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)

And this is the new MainActivity code:


```java
package com.howtoevery.funfacts;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends ActionBarActivity {

    static TextView factLabel;
    static Button showFactButton;
    static ViewGroup backGround;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Creating the random
        final Random rand = new Random();


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        factLabel = (TextView) findViewById(R.id.fact);
        ViewGroup backGround = (RelativeLayout) findViewById(R.id.mainBackground);
        showFactButton = (Button) findViewById(R.id.factButton);

        View.OnClickListener myListener = new View.OnClickListener() {

            int randomNumber;
            int currentState = 0;

            @Override
            public void onClick(View view) {

                do {
                    randomNumber = rand.nextInt(2); // randomNumber will be 0 or 1
                }   while (currentState == randomNumber);

                currentState = randomNumber;

                switch (randomNumber) {
                    case 0: { // in case it is 0, show fact number 1 with green color
                        setFact(R.string.fact1, R.color.android_green);
                    } break;

                    case 1: { // in case it is 1, show fact number 2 with orange color
                        setFact(R.string.fact2, R.color.orangish);

                    } break;
                }

          //      factLabel.setText(String.valueOf(randomNumber)); // show the number generated with randomNumber, ONLY FOR DEBUG PURPOSES

            }
        };

        showFactButton.setOnClickListener(myListener);
    }
    public void setFact(int factString, int color) {
        factLabel.setText(factString);
        backGround.setBackgroundColor(getResources().getColor(color));
        showFactButton.setTextColor(getResources().getColor(color));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

2 Answers

If you want to declare the variable outside the class, you will have to declare it without the

Static

keyword since

Static

variables can only be created inside a class.

From what I understand, that should work.

I hope that helps.

Hi Ishay

I'm not entirely sure what you are trying to achieve but it is really bad practice to try and pull a variable from another class without using the correct getter or setter method unless of course this variable is a constant value. The error from your logcat points to the setFact method where you are trying to set the Text using an int variable type which doesn't wash with android. I think you simply need to change the variable type to String (shown below).

package com.howtoevery.funfacts;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends ActionBarActivity {

    static TextView factLabel;
    static Button showFactButton;
    static ViewGroup backGround;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Creating the random
        final Random rand = new Random();


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        factLabel = (TextView) findViewById(R.id.fact);
        ViewGroup backGround = (RelativeLayout) findViewById(R.id.mainBackground);
        showFactButton = (Button) findViewById(R.id.factButton);

        View.OnClickListener myListener = new View.OnClickListener() {

            int randomNumber;
            int currentState = 0;

            @Override
            public void onClick(View view) {

                do {
                    randomNumber = rand.nextInt(2); // randomNumber will be 0 or 1
                }   while (currentState == randomNumber);

                currentState = randomNumber;

                switch (randomNumber) {
                    case 0: { // in case it is 0, show fact number 1 with green color
                        setFact(R.string.fact1, R.color.android_green);
                    } break;

                    case 1: { // in case it is 1, show fact number 2 with orange color
                        setFact(R.string.fact2, R.color.orangish);

                    } break;
                }

          //      factLabel.setText(String.valueOf(randomNumber)); // show the number generated with randomNumber, ONLY FOR DEBUG PURPOSES

            }
        };

        showFactButton.setOnClickListener(myListener);
    }
    public void setFact(String factString, int color) { //I have changed the variable type here to String as int throws an error
        factLabel.setText(factString); 
        backGround.setBackgroundColor(getResources().getColor(color));
        showFactButton.setTextColor(getResources().getColor(color));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Hope this helps Daniel