Rohit Pal
Rohit Pal

Reputation: 15

java.lang.NullPointerException: On Button Click Listener

Whenever The Custom Dialog is opened the app crashes throwing the following error

ive checked for duplicate initialization but could not find anything

In MainActivity.java

public class MainActivity extends AppCompatActivity {

    PieChart pieChart;
    ImageView edit;
    Dialog editDialog;

    EditText edit_cal,edit_carb,edit_prot,edit_fats;
    String cal,carb,prot,fats;
    Button save;

    public static final String SHARED_PREFS = "sharedPrefs";
    public static final String CAL = "text";
    public static final String CARB = "text";
    public static final String PROT = "text";
    public static final String FATS = "text";

    TextView tcal,tcarb,tprot,tfats;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit=findViewById(R.id.edit);
        pieChart=findViewById(R.id.pie);
        tcal=findViewById(R.id.tcal);
        tcarb=findViewById(R.id.tcarb);
        tprot=findViewById(R.id.tprot);
        tfats=findViewById(R.id.tfats);
        saveData();
        loadData();
        updateViews();


        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                custom_Dialog();
            }
        });

    }

    private void custom_Dialog() {
        editDialog=new Dialog(MainActivity.this);
        editDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        editDialog.setCancelable(true);
        editDialog.setContentView(R.layout.edit_nutrient);
        editDialog.show();

        edit_cal=findViewById(R.id.edit_cal);
        edit_carb=findViewById(R.id.edit_carb);
        edit_prot=findViewById(R.id.edit_prot);
        edit_fats=findViewById(R.id.edit_fats);

        save=findViewById(R.id.savebtn);


        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cal=edit_cal.getText().toString();
                carb=edit_carb.getText().toString();
                prot=edit_prot.getText().toString();
                fats=edit_fats.getText().toString();

                if(isValid(cal,carb,prot,fats))
                {
                    saveData();
                    loadData();
                    updateViews();
                    editDialog.dismiss();

                }
                else
                {
                   Toast.makeText(MainActivity.this,"Please fill your diet plan correctly",Toast.LENGTH_SHORT).show();
                }



            }
        });


    }

In edit_nutrient.xml

    <Button
        android:id="@+id/savebtn"
        android:layout_width="296dp"
        android:layout_height="48dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="340dp"
        android:background="@drawable/button_default_2"
        android:fontFamily="sans-serif-medium"
        android:letterSpacing="-0.03"
        android:text="Save"
        android:textColor="#ffffff"
        android:textSize="13sp"
        android:textStyle="normal"


        />
android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
        at com.example.android.chartvisual.MainActivity.custom_Dialog(MainActivity.java:122)
        at com.example.android.chartvisual.MainActivity.access$000(MainActivity.java:29)
        at com.example.android.chartvisual.MainActivity$1.onClick(MainActivity.java:101)
        at android.view.View.performClick(View.java:6319)
        at android.view.View$PerformClick.run(View.java:24955)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6701)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)

Upvotes: 0

Views: 69

Answers (2)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15423

You have to create View from layout and then pass it to create dialog. Also use view instance to get child elements. Check below:

private void custom_Dialog() {
    View view = LayoutInflater.from(this).inflate(R.layout.edit_nutrient, null, false);

    editDialog=new Dialog(MainActivity.this);
    editDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    editDialog.setCancelable(true);
    editDialog.setContentView(view);
    editDialog.show();

    edit_cal = view.findViewById(R.id.edit_cal);
    edit_carb = view.findViewById(R.id.edit_carb);
    edit_prot = view.findViewById(R.id.edit_prot);
    edit_fats = view.findViewById(R.id.edit_fats);

    save = view.findViewById(R.id.savebtn);


    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cal=edit_cal.getText().toString();
            carb=edit_carb.getText().toString();
            prot=edit_prot.getText().toString();
            fats=edit_fats.getText().toString();

            if(isValid(cal,carb,prot,fats))
            {
                saveData();
                loadData();
                updateViews();
                editDialog.dismiss();

            }
            else
            {
               Toast.makeText(MainActivity.this,"Please fill your diet plan correctly",Toast.LENGTH_SHORT).show();
            }
        }
    });
}

Upvotes: 0

Ben P.
Ben P.

Reputation: 54204

Here, you set the content view of your dialog:

editDialog.setContentView(R.layout.edit_nutrient);

However, in these lines, you are searching for views inside of your Activity, not your dialog:

edit_cal=findViewById(R.id.edit_cal);
edit_carb=findViewById(R.id.edit_carb);
edit_prot=findViewById(R.id.edit_prot);
edit_fats=findViewById(R.id.edit_fats);

save=findViewById(R.id.savebtn);

You need to use the findViewById() method of your dialog to search within it:

edit_cal=editDialog.findViewById(R.id.edit_cal);
edit_carb=editDialog.findViewById(R.id.edit_carb);
edit_prot=editDialog.findViewById(R.id.edit_prot);
edit_fats=editDialog.findViewById(R.id.edit_fats);

save=editDialog.findViewById(R.id.savebtn);

Upvotes: 1

Related Questions