0

Im having an issue with a click listener for a submit modal submit button.

This is the error.

FATAL EXCEPTION: main
              Process: com.kuryetaxi.omerkaptan.kuryetaxi, PID: 6564
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.button.MaterialButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

I have a reasonable understanding of what a null pointer exception is and I have search thoroughly for an issue similar to mine. I have tried to reformat the click listener in several ways, made sure I have the correct view ID etc.

fragment code

View view;
private RecyclerView siparislerrecyclerview;
private SiparisAdapter siparisAdapter;
private List<SiparisModel> siparisList;
private ChangeFragments changeFragments;
private String siparis_id,kurye_id;
private GetSharedPreferences getSharedPreferences;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_kurye_siparis, container, false);
    tanimla();
    gerek();
    getSip("0");

    return view;

}
public void tanimla()
{
    siparisList = new ArrayList<>();
    siparislerrecyclerview = view.findViewById(R.id.siparislerrecyclerview);
    RecyclerView.LayoutManager mng = new GridLayoutManager(getContext(),1);
    siparislerrecyclerview.setLayoutManager(mng);
    changeFragments = new ChangeFragments(getContext());
    getSharedPreferences = new GetSharedPreferences(getActivity());
    siparis_id=getSharedPreferences.getSession().getString("id",null);
}
public void getSip(String kur_durum)
{
    Call<List<SiparisModel>> req = getInstance().getSip(kur_durum);
    req.enqueue(new Callback<List<SiparisModel>>() {
        @Override
        public void onResponse(Call<List<SiparisModel>> call, Response<List<SiparisModel>> response) {
            Log.i("listem",response.body().toString());
            if (response.body().get(0).isTf())
            {
                siparisList = response.body();
                siparisAdapter = new SiparisAdapter(siparisList,getContext());
                siparislerrecyclerview.setAdapter(siparisAdapter);

            }
            else
            {
                Toast.makeText(getContext(),"FİRMA SİPARİSİ BULUNMAMAKTADIR",Toast.LENGTH_LONG).show();
                changeFragments.change(new HomeFragment());
            }
        }

        @Override
        public void onFailure(Call<List<SiparisModel>> call, Throwable t) {
            Toast.makeText(getContext(), Warnings.internetProblemText,Toast.LENGTH_LONG).show();

        }
    });
}

public void gerek()
{
    MaterialButton ekle = (MaterialButton)view.findViewById(R.id.ekleButton);
    ekle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            siparisAl(siparis_id);

        }
    });
}

public void siparisAl(String siparis_id)
{

    Call<SiparisAlModel> req = ManagerAll.getInstance().siparisAl(siparis_id);
    req.enqueue(new Callback<SiparisAlModel>() {
        @Override
        public void onResponse(Call<SiparisAlModel> call, Response<SiparisAlModel> response) {
            Log.i("hatas",response.body().toString());
            Toast.makeText(getContext(),response.body().getText(),Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFailure(Call<SiparisAlModel> call, Throwable t) {
            Toast.makeText(getContext(),Warnings.internetProblemText,Toast.LENGTH_LONG).show();

        }
    });
}

layout.xml code

xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/siparisdetaylayout">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    card_view:cardBackgroundColor="#f8b518"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="1dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:clickable="true"
        android:id="@+id/siparisdetaylayout"
        android:layout_margin="6dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center"
        android:orientation="vertical">




        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_margin="15dp"
            android:id="@+id/logo"
            app:civ_border_width="2dp"
            app:civ_border_color="#FF000000"/>
        <TextView
            android:layout_gravity="left"
            android:layout_width="125dp"
            android:id="@+id/firmatext"

            android:textStyle="bold"
            android:textSize="17dp"
            android:gravity="left"
            android:textColor="#FF000000"
            android:layout_height="wrap_content" />



        <TextView
            android:layout_gravity="left"
            android:id="@+id/tarihtext"
            android:layout_width="125dp"

            android:textStyle="bold"
            android:textSize="17dp"
            android:gravity="left"
            android:textColor="#FF000000"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_gravity="left"
            android:id="@+id/saattext"
            android:layout_width="125dp"

            android:textStyle="bold"
            android:textSize="17dp"
            android:gravity="left"
            android:textColor="#FF000000"
            android:layout_height="wrap_content" />

        <android.support.design.button.MaterialButton

            android:id="@+id/ekleButton"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="15dp"
            android:text="Ekle"
            style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
            android:layout_height="wrap_content" />

    </LinearLayout>

</android.support.v7.widget.CardView>

  • What is your layout file name? `layout.xml` or `fragment_kurye_siparis.xml`? – Shaiful Aug 10 '19 at 18:13
  • @Shaiful layout name = siparisitemlayout java class name = KuryeSiparisFragment – Ömer Kaptan Aug 10 '19 at 18:21
  • You are inflating `fragment_kurye_siparis` layout here `view = inflater.inflate(R.layout.fragment_kurye_siparis, container, false);`. But you are saying the `MaterialButton` is actually defined in another layout. You have to use the same layout for the view where the `MaterialButton` is defined. – Shaiful Aug 10 '19 at 18:24
  • @Shaiful `LayoutInflater layoutInflater = this.getLayoutInflater(); View view = layoutInflater.inflate(R.layout.siparisitemlayout,null); MaterialButton ekle = (MaterialButton)view.findViewById(R.id.ekleButton); ekle.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.i("deneme",""+v); Toast.makeText(getContext(),"deneme",Toast.LENGTH_LONG).show(); siparisAl(siparis_id); } });` But nothing happens when I press the button – Ömer Kaptan Aug 10 '19 at 18:43

2 Answers2

0

Try calling gerek() in onViewCreated() to ensure that the entire layout has been inflated before trying to use it

Joshua Feltimo
  • 323
  • 1
  • 3
  • 12
0

Override onViewCreated() method in your class and put this methods tanimla() gerek() getSip("0") into it and trying call getView() inside your methods just like this:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup  container, @Nullable Bundle savedInstanceState) {
    View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_kurye_siparis, container, false);
    return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
    super.onViewCreated(view, savedInstanceState);
    //
    tanimla();
    gerek();
    getSip("0");
}

public void gerek()
{
    MaterialButton ekle = (MaterialButton)getView().findViewById(R.id.ekleButton);
    ...
}

read this doc!

  • 'E/AndroidRuntime: FATAL EXCEPTION: main Process: com.kuryetaxi.omerkaptan.kuryetaxi, PID: 7067 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at com.kuryetaxi.omerkaptan.kuryetaxi.Fragments.KuryeSiparisFragment.tanimla(KuryeSiparisFragment.java:64) at com.kuryetaxi.omerkaptan.kuryetaxi.Fragments.KuryeSiparisFragment.onCreateView(KuryeSiparisFragment.java:48)' – Ömer Kaptan Aug 11 '19 at 04:14
  • layout name = siparisitemlayout java class name = KuryeSiparisFragment – Ömer Kaptan Aug 11 '19 at 04:18
  • i use u code but FATAL EXCEPTION: main Process: com.kuryetaxi.omerkaptan.kuryetaxi, PID: 6564 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.button.MaterialButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference – Ömer Kaptan Aug 11 '19 at 04:43
  • Your xml layout in code is `fragment_kurye_siparis` , if your layout is `siparisitemlayout` just replace right layout – Hamed.Ghaderian Aug 11 '19 at 10:09