Search code examples
androidbuttoncolorssettext

Trying to simply set button color and text upon press. Keeps crashing?


So here is my code, I am just trying to have the text and color of the Button posNeg_btn change when it is pressed. But it keeps crashing and giving me null pointer exception? Any ideas? What could I be missing?

ListView recurrenceListView;
Button add_btn;
EditText reccurenceEntry_et;
EditText reccurenceExplanation_et;
Button clear_btn;
Button posNeg_btn;
Boolean positiveIfTrue = false;
//Double[] comparisonArray = new Double[8];
int reccurenceCounter;

String[] reccurenceArray = new String[30];
String[] reccurenceExpArray = new String[30];
String[] finalDisplayArray = new String[30];



private ArrayAdapter arrayAdapter;


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


        recurrenceListView = (ListView) findViewById( R.id.recurrenceListView );
        reccurenceEntry_et = (EditText) findViewById( R.id.reccurenceEntry_et );
        reccurenceExplanation_et = (EditText) findViewById( R.id.reccurenceExplanation_et );

        Button add_btn = (Button) findViewById( R.id.add_btn);
        Button clear_btn = (Button) findViewById( R.id.clear_btn);
        Button posNeg_btn = (Button) findViewById( R.id.posNeg_btn);




        add_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                addRecurrence();
                //calculateMovingAverage();

            }
        });

        posNeg_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                changeSign();
                //calculateMovingAverage();

            }
        });

        clear_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                clearReccurences();
                //calculateMovingAverage();

            }
        });


}

    public void changeSign(){


        posNeg_btn.setText("Test");
        posNeg_btn.setTextColor(Color.GREEN);

        /*
        if(positiveIfTrue ==false){
        positiveIfTrue = true;
        posNeg_btn.setText("+");
      //  posNeg_btn.setBackgroundColor(Color.GREEN);
        }

        if(positiveIfTrue == true){
            positiveIfTrue = false;
            posNeg_btn.setText("-");
        //    posNeg_btn.setBackgroundColor(Color.RED);

        }   
    }

forecast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Specific Bills/Recurrences" />

<EditText
    android:id="@+id/reccurenceEntry_et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Specific Bills/Recurrences"
    android:inputType="numberDecimal" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/reccurenceExplanation_et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Explanation" />

<ListView
    android:id="@+id/recurrenceListView"
    android:layout_width="114dp"
    android:layout_height="119dp" >
</ListView>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="191dp"
    android:layout_height="48dp" />

<Button
    android:id="@+id/add_btn"
    android:layout_width="77dp"
    android:layout_height="wrap_content"
    android:text="Add" />

<Button
    android:id="@+id/clear_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clear Reccurences" />

<Button
    android:id="@+id/posNeg_btn"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:text="-"
    android:textSize="40sp" />

Logcat

09-10 09:46:06.935: E/AndroidRuntime(12964): FATAL EXCEPTION: main
09-10 09:46:06.935: E/AndroidRuntime(12964): Process: com.example.forecastspending, PID: 12964
09-10 09:46:06.935: E/AndroidRuntime(12964): java.lang.NullPointerException
09-10 09:46:06.935: E/AndroidRuntime(12964):    at       com.example.forecastspending.SavingsForecastActivity.changeSign(SavingsForecastActivity.java:102)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at com.example.forecastspending.SavingsForecastActivity$2.onClick(SavingsForecastActivity.java:79)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.view.View.performClick(View.java:4442)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.view.View$PerformClick.run(View.java:18473)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.os.Handler.handleCallback(Handler.java:733)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.os.Looper.loop(Looper.java:136)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at android.app.ActivityThread.main(ActivityThread.java:5105)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at java.lang.reflect.Method.invokeNative(Native Method)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at java.lang.reflect.Method.invoke(Method.java:515)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
09-10 09:46:06.935: E/AndroidRuntime(12964):    at dalvik.system.NativeStart.main(Native Method)

Solution

  • On your code:

     Button posNeg_btn = (Button) findViewById( R.id.posNeg_btn);
    

    remove Button. It must be like this:

    posNeg_btn = (Button) findViewById( R.id.posNeg_btn);
    

    Same for other buttons

    The problem is that you're creating a local variable. This way you're setting the listener to that variable. When you get to the function you're using the field, which is null, because you just set the local variable and not the field.