kort.es
kort.es

Reputation: 479

issue using constructor in java

I want to pass some data between classes but i cant get it working. Getting NullPointerException at line home.adapter.updateStatus();

Main class

public class HomeScreen extends Activity implements OnClickListener{
SocialAuthAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    adapter = new SocialAuthAdapter(new ResponseListener());
    adapter.addProvider(Provider.TWITTER, R.drawable.button_twitter);
    }
}

Helper class

 public class ResponseListener implements DialogListener {

HomeScreen home = new HomeScreen();

    public void onComplete(Bundle values) {
    home.adapter.updateStatus("update");
    }
 }

Upvotes: 0

Views: 142

Answers (5)

Raja Asthana
Raja Asthana

Reputation: 2100

When Activity is started for the first time only once the onCreate() method will be called. It will not be created when you create object for your activity.

Upvotes: 1

MuraliGanesan
MuraliGanesan

Reputation: 3261

Change this line in your code

 adapter = new SocialAuthAdapter(new ResponseListener(HomeScreen.this));

Helper class,

 public class ResponseListener implements DialogListener {
 private HomeScreen context;
 public ResponseListener(HomeScreen homescreen) {
    context = homescreen;

}


}

Upvotes: 0

CAMOBAP
CAMOBAP

Reputation: 5657

You should move:

adapter = new SocialAuthAdapter(new ResponseListener());
adapter.addProvider(Provider.TWITTER, R.drawable.button_twitter);

to HomeScreen constructor like this:

public class HomeScreen extends Activity implements OnClickListener {
    public SocialAuthAdapter adapter;

    public HomeScreen() {
        adapter = new SocialAuthAdapter(new ResponseListener());
        adapter.addProvider(Provider.TWITTER, R.drawable.button_twitter);
    }

Upvotes: 0

Kent
Kent

Reputation: 195029

you instantiated HomeScreen home = new HomeScreen(); by constructor, but in your HomeScreen class, you instantiated adapter in a method onCreate(). did your HomeScreen constructor call this method?

If not, adapter is null, so you got NPE.

Upvotes: 0

Bob Flannigon
Bob Flannigon

Reputation: 1294

I'd suggest that the adapter is not being initialised, which can only mean onCreate() isn't being invoked...

Upvotes: 1

Related Questions