jeudi 19 septembre 2019

How to validate if the ImageView is null?

This is my add data code for my sqlite the rest works well but when I did not choose image in the to be show in ImageView then click the btnAddData the app crash but when I select image the system successfully insert data in SQL. what do you think I need to add in my IF ELSE STATEMENT to make a Toast "Please Select Signature"

    private void AddData() {
    btnAddData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            imageViews = (ImageView) findViewById(R.id.image_view);


            String time_out = edittime_out.getText().toString();
            String time_ino = edittime_in.getText().toString();
            String time_cuo = edittime_cu.getText().toString();
            String time_bro = edittime_br.getText().toString();
            String time_mao = edittime_ma.getText().toString();
            String time_tec = edittimei_te.getText().toString();
            String time_sta = edittimei_st.getText().toString();
            String time_sre = edittimei_sr.getText().toString();
            String time_qou = edittimei_qt.getText().toString();
            String serial_out = jobserialout.getText().toString();
            String timein_jobno = timeinjob.getText().toString();
            String time_co = timecompany.getText().toString();
            String time_dates = timedates.getText().toString();
            String timei_legend = timeilegend.getSelectedItem().toString();
            String timei_department = timedept.getText().toString();
            final Bitmap bitmap = ((BitmapDrawable) imageViews.getDrawable()).getBitmap();


            if (TextUtils.isEmpty(time_out)) {
                Toast.makeText(FormTimeout.this, "Please enter Time Out ", Toast.LENGTH_LONG).show();
                return;
            }

            if (TextUtils.isEmpty(time_ino)) {
                Toast.makeText(FormTimeout.this, "Please enter Time In ", Toast.LENGTH_LONG).show();
                return;
            }

            if (TextUtils.isEmpty(time_cuo)) {
                Toast.makeText(FormTimeout.this, "Please enter Customer ", Toast.LENGTH_LONG).show();
                return;
            }
            if (TextUtils.isEmpty(time_bro)) {
                Toast.makeText(FormTimeout.this, "Please enter Branch ", Toast.LENGTH_LONG).show();
                return;
            }
            if (TextUtils.isEmpty(time_mao)) {
                Toast.makeText(FormTimeout.this, "Please enter Machine ", Toast.LENGTH_LONG).show();
                return;
            }

            if (TextUtils.isEmpty(time_tec)) {
                Toast.makeText(FormTimeout.this, "Please enter  Technician ", Toast.LENGTH_LONG).show();
                return;
            }

            if (TextUtils.isEmpty(time_sta)) {
                Toast.makeText(FormTimeout.this, "Please enter Status ", Toast.LENGTH_LONG).show();
                return;
            }

            if (TextUtils.isEmpty(time_sre)) {
                Toast.makeText(FormTimeout.this, "Please enter  SR", Toast.LENGTH_LONG).show();
                return;
            }

            if (TextUtils.isEmpty(time_qou)) {
                Toast.makeText(FormTimeout.this, "Please enter Qoutation ", Toast.LENGTH_LONG).show();
                return;
            }

            if (TextUtils.isEmpty(serial_out)) {
                Toast.makeText(FormTimeout.this, "Please enter Serial ", Toast.LENGTH_LONG).show();
                return;
            }

            if (TextUtils.isEmpty(timein_jobno)) {
                Toast.makeText(FormTimeout.this, "Please enter Time in ID ", Toast.LENGTH_LONG).show();
                return;
            }

            if (TextUtils.isEmpty(timei_legend)) {
                Toast.makeText(FormTimeout.this, "Please select Remarks ", Toast.LENGTH_LONG).show();
                return;
            }

            if (TextUtils.isEmpty(timei_department)) {
                Toast.makeText(FormTimeout.this, "Please select Department ", Toast.LENGTH_LONG).show();
                return;
            }


            if  (imageViews.getDrawable() == null){
                Toast.makeText(FormTimeout.this, "Please select Signature ", Toast.LENGTH_LONG).show();
                return;
             }





        boolean isInserted = myDb.insertDataout( time_out, time_ino, time_cuo, time_bro, time_mao, time_tec, time_sta, time_sre,time_qou, serial_out, timein_jobno, timei_legend, time_co,  time_dates, timei_department, Utils.getBytes(bitmap));
            if (isInserted == true) {
                Toast.makeText(FormTimeout.this, "Time Out Successfully", Toast.LENGTH_LONG).show();
                edittime_out.setText("");
                edittime_in.setText("");
                edittime_cu.setText("");
                edittime_br.setText("");
                edittime_ma.setText("");
                edittimei_te.setText("");
                edittimei_st.setText("");
                edittimei_sr.setText("");
                edittimei_qt.setText("");
                jobserialout.setText("");
                timeinjob.setText("");


                Intent intent = new Intent(FormTimeout.this, Timeoutsms.class);
                startActivity(intent);

            } else
                Toast.makeText(FormTimeout.this, "Time Out Failed", Toast.LENGTH_LONG).show();
        }
    });
}

This is the error in Logcat LOGCAT ERROR

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap 
 android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference

Aucun commentaire:

Enregistrer un commentaire