Fincoder
Fincoder

Reputation: 9

How do I not get nullpointer exception on my fragment?

So I get a null pointer exception whenever I am opening this fragment. Could you please help?

 public class ProfileFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
ViewGroup container, @Nullable Bundle savedInstanceState) {


    Bundle bundle = getArguments();
    String eka;
    if(bundle.containsKey("Name:")) {
        eka = bundle.getString("Name:");
        String toka;
        toka = bundle.getString("Country");


        TextView ekat = (TextView) getView().findViewById(R.id.nimit);
        TextView tokat = (TextView) getView().findViewById(R.id.maat);
        ekat.setText(eka);
        tokat.setText(toka);
    }


    return inflater.inflate(R.layout.fragment_profile, container, false);
}
}

Here is an activity that I am sending info from

  public class MainActivity extends AppCompatActivity {


EditText edit, editt, edittt, editttt;
Button btn;


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

    edit = (EditText) findViewById(R.id.nimi);
    editt = (EditText) findViewById(R.id.maa);
    edittt = (EditText) findViewById(R.id.salasana);
    editttt = (EditText) findViewById(R.id.syntymäaika);
    btn = (Button) findViewById(R.id.nappi);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String eka = edit.getText().toString();
            String toka = editt.getText().toString();

            Bundle bundle = new Bundle();
            bundle.putString("Name:", eka);
            bundle.putString("Country", toka);

            ProfileFragment profileFragment = new ProfileFragment();
            profileFragment.setArguments(bundle);
            edit.setVisibility(View.GONE);
            editt.setVisibility(View.GONE);
            edittt.setVisibility(View.GONE);
            editttt.setVisibility(View.GONE);
            btn.setVisibility(View.GONE);
        }
     });

and I get this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.Bundle.containsKey(java.lang.String)' on a null object reference

So the app crashes whenever I open the fragment. For some reason it doesn't send the info from the activity to the fragment. Thank you for your help.

Upvotes: 0

Views: 83

Answers (4)

Naim.Jusufi
Naim.Jusufi

Reputation: 155

Based on what I can see you are missing a fragment transaction, so I am guessing you are displaying the fragment statically, defining it in the xml file (you need to post the activity_layout.xml file to be sure) In your activity_main.xml you need to have a FrameLayout

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

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

and then inside your View.OnClickListener you can do

Bundle bundle = new Bundle(); 
bundle.putString("Name:", eka); 
bundle.putString("Country", toka); 

ProfileFragment profileFragment = new ProfileFragment();
profileFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
edit.setVisibility(View.GONE);
//Continue with your code

Also it is good practice to get the arguments in onCreate()

Override onCreate() and do the following

    @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if(bundle.containsKey("Name:")) {
    eka = bundle.getString("Name:"); // make this field to use in onCreateView()
    toka = bundle.getString("Country"); // make this field to use in onCreateView()

then in onCreateView() you will need the following

 @Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);
    TextView ekat = (TextView) view.findViewById(R.id.nimit);
    TextView tokat = (TextView) view.findViewById(R.id.maat);
    ekat.setText(eka);
    tokat.setText(toka);
}

Upvotes: 4

Haroun Hajem
Haroun Hajem

Reputation: 5638

You are trying to get a Bundle that is null here:

Bundle bundle = getArguments();
....
if(bundle.containsKey("Name:")) {

In your Fragment, retrieve the data (e.g. in onCreate() method) with:

Bundle bundle = this.getArguments();
if (bundle != null) {
   int myInt = bundle.getInt(key, defaultValue);
}

Here is a chart explaining the lifecycle of a fragment enter image description here

Upvotes: 1

Sofien Sahli
Sofien Sahli

Reputation: 48

By using: public static String eka =""; public static String toka="";

on your activity and accessing it by: MainActivity.eka MainActivity.toka

It will stop crashing.

Upvotes: 1

TheWanderer
TheWanderer

Reputation: 17874

You can't use getView() until onCreateView() returns.

Move everything you have above the return statement into onViewCreated().

Upvotes: 1

Related Questions