即使不为空,字段验证也会显示空指针异常

时间:2015-01-08 09:43:31

标签: android

我试图检查edittext是否为空。如果是空的,只需警告用户在其中放入一些东西。问题是如果我将该字段留空应用程序崩溃。如果我在现场放置一些东西应用程序崩溃。两次都是同一个错误:

01-08 09:34:34.989: E/AndroidRuntime(2183): FATAL EXCEPTION: main
01-08 09:34:34.989: E/AndroidRuntime(2183): java.lang.NullPointerException
01-08 09:34:34.989: E/AndroidRuntime(2183):     at com.res.Reservation$3.onClick(Reservation.java:108)

这是代码

@Override
        public void onClick(View v) {

            if(editText1.getText().toString().length()==0)  
            {           
                editText1.setError( "Please enter your name!" );  
            }  
            else if(editText3.getText().toString().length()==0)  
            {           
                editText3.setError( "Please enter phone number in format 1234567890" ); 
            }
            else if(editText2.getText().toString().length() == 0)  
            {           
                editText2.setError( "Please enter your Email" );
            }
            else if(datePicker.getText().toString().length()==0)  
            {           
                datePicker.setError( "Please choose date and time" );  
            }
            else if(editText5.getText().toString().length()==0)  
            {           
                editText5.setError( "Please enter number of people" );  
            }
            else
            {  
                Intent intent = new Intent(Reservation.this, TableReservation.class);
                String Name = editText1.getText().toString();
                String Email = editText2.getText().toString();
                String Phone = editText3.getText().toString();
                String Comment = editText4.getText().toString();
                String DateTime = datePicker.getText().toString();
                String numberOfPeople = editText5.getText().toString();


                intent.putExtra("Name", editText1.getText().toString());
                intent.putExtra("Email", editText2.getText().toString());
                intent.putExtra("Phone", editText3.getText().toString());
                intent.putExtra("Comment", editText4.getText().toString());
                intent.putExtra("DateTime", datePicker.getText().toString());
                intent.putExtra("numberOfPeople", editText5.getText().toString());
                intent.putExtra("rest_id", getIntent().getStringExtra("rest_id").toString());
                startActivity(intent);

            }

第108行

   else if(editText5.getText().toString().length()==0)  
   {           
        editText5.setError( "Please enter number of people" );  
   }

知道可能出现什么问题?

1 个答案:

答案 0 :(得分:2)

editText5为null。确保你已经初始化了