1

In main activity i send the String[] name like this:

private void sendNames() {

    Bundle b=new Bundle();
    b.putStringArray("key", names);
    Intent i=new Intent(this, ListFriendsFragment.class);
    i.putExtras(b);

}

when i send names it is not empty 100%, and put this code in a method, and called it after the i get the names.

In activity i want to receive the string[] i get it like this:

 names = this.getIntent().getExtras().getStringArray("key");

In both, main activity and the one i want to receive the string, names is declared as follows:

private String[] names;

When i start the activity which should get the names the application crashes:

Caused by: java.lang.NullPointerException
at com.utm.course.friendslist.ListFriendsFragment.PrintNames(ListFriendsFragment.java:26)
at com.utm.course.friendslist.ListFriendsFragment.onCreate(ListFriendsFragment.java:20)

What am i doing wrong?


Update

these are parts where i use Intent

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (currentSession != null) {
        currentSession.onActivityResult(this, requestCode, resultCode, data);
    }
}
...
private void sendNames() {
    Log.d("sendNames", "started");
    Bundle b=new Bundle();
    b.putStringArray(key, names);
    Intent i=new Intent(this, ListFriendsFragment.class);
    i.putExtras(b);
}
...
 private void listFriends() {
    Log.d("Activity", "List Friends Activity Starting");
    Intent i=new Intent(MainActivity.this,ListFriendsFragment.class);
    startActivity(i);
    finish();
}
Filip Luchianenco
  • 6,912
  • 9
  • 41
  • 63

3 Answers3

3

It looks like sendNames() does not return the intent you created and you probably call startActivity(i); somewhere else where the intent you created here is no longer in the scope.

Change the signature of sendNames() to return the intent you've created and use that intent when you start the activity.

If you'll run with a debugger, add a break point where you start the activity and make sure that the intent you pass is containing that bundle with the "key" string array.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • I have searched for `Intent` in my project. please check the update in the question, as here it will be hard to understand. – Filip Luchianenco Oct 12 '13 at 19:40
  • 1
    1. your code doesn't show where the last two methods are getting called from. 2. Like I wrote, the `intent` that you create in `sendNames()` is being destroyed the moment `sendNames()` finish to execute. – Nir Alfasi Oct 12 '13 at 22:00
  • 1
    you are right. i put sendNames and listFriends together, and i send the intent when i start the activity. thank you. – Filip Luchianenco Oct 12 '13 at 22:27
0

Just do this way:

Assumption, String[] names;

Intent intent = new Intent(this, ListFriendsFragment.class);
intent.putExtra("key", names);
startActivity(intent);

On next activity,

Intent intent = getIntent();
String[] names = intent.getStringArrayExtra("key");
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
0

Sending

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

Receiving

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
Zohaib
  • 2,845
  • 2
  • 23
  • 33
  • the sending part goes ok without any error. FriendsListFragment is not a fragment, its activity. still getting NullPointerException on FriendsListActivity – Filip Luchianenco Oct 12 '13 at 19:27