I want to perform validations for my editfields.so I am writing the validations on ButtonField.setChangeListener method. If editField is empty and when clicking on the button i have to show that the field is empty. To show the message i tried by using both status.show() and dialog.alert() methods. But both are generating a NullPointerException. What is the problem? Can anyone help to solve this problem or are there any other solutions to this problem?
I have written my code like this:
btnencrypt = new ButtonField("Encrypt");
btnencrypt.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
//getphonenos();
System.out.println("savedPhone no are in compose encrypt:"+savedphoneno);
encryptClicked= true;
if (savedphoneno.equals("")) { **Getting the exception here....**
Dialog.alert("Please select valid contact");
} else {
if (!(savedphoneno.equals(""))) {
if (edmsg.getText().toString().trim().equals("")) {
Dialog.alert("Please enter message");
}else {
int index = savedphoneno.indexOf(",");
if (index < 0) {
encryptBTNClicked = true;
try {
base64msgString = encrypt(savedphoneno);
} catch (CryptoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
edencryptmsg.setText(base64msgString);
} else {
//encryptTV.setText("");
edencryptmsg
.setText("Sending data to multiple receipients,"
+ "can't show the encrypted msg,as it varies");
//edencryptmsg.setTextColor(Color.MAGENTA);
}
btnencrypt.setEnabled(false);
btnclear.setEnabled(false);
}
}
}
}
});
I have a useful method that I put into my StringUtils class that checks if a string is valid.
This will help you with Rupak’s answer.