-1

In my app in try to change a TextView text using setText. If the call is made in the onCreate method it works just fine but if I call another method to do so it crashes giving me a NullPointerException

here is the working code

package staub.olivier.aros.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

    public TextView ca00;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    final TextView ca00 = (TextView) findViewById(R.id.case00); 
    ca00.setText("stuff");
}

what I want to do is to have a method that will change my texts whenever I call it so I tried this :

package staub.olivier.aros.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

    public TextView ca00;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    final TextView ca00 = (TextView) findViewById(R.id.case00); 
    updateText();
}

    public updateText() {
            ca00.setText("stuff");

    }
    }

What do I do wrong or how can I manage to do what I want ?

Thanks for your time and answers !

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
user3214327
  • 33
  • 1
  • 2

5 Answers5

3

Your problem is variable scope. Inside the onCreate() method you are actually defining a new local variable and not using the class property.

You need to change this line in your onCreate()

final TextView ca00 = (TextView) findViewById(R.id.case00);

To this:

ca00 = (TextView) findViewById(R.id.case00);

This will resolve your NPE.

Scott Helme
  • 4,786
  • 2
  • 23
  • 35
1
package staub.olivier.aros.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

    public TextView ca00;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

     ca00 = (TextView) findViewById(R.id.case00); 
    updateText();
}

    public updateText() {
            ca00.setText("stuff");

    }
    }
Digvesh Patel
  • 6,503
  • 1
  • 20
  • 34
  • Whilst the code you have provided is correct, you should really explain why the changes are required. The OP is likely to make the same mistake again unless he understands why the changes are required. Give a man a fish... – Scott Helme Jan 20 '14 at 09:02
1

You are creating TextView ca00 two times in your code, instead that declare it globally and use it directly in onCreate()

package staub.olivier.aros.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

    public TextView ca00;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    ca00 = (TextView) findViewById(R.id.case00);    // Change here
    updateText();
}

    public updateText() {
            ca00.setText("stuff");

    }
    }
Vigbyor
  • 2,568
  • 4
  • 21
  • 35
0

your global ca00 is never initialized, hence null pointer exception you experiencing

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

It's about the problem of scope in your code. In the onCreate method, you declared a ca00 variable:

final TextView ca00 = (TextView) findViewById(R.id.case00); 

Therefore, anything you set to ca00 only affect a variable in that method, not the one in the class scope. You should change to:

this.ca00 = (TextView) findViewById(R.id.case00);  
// this keyword is not mandatory, but I recommend it.
Luke Vo
  • 17,859
  • 21
  • 105
  • 181