获取NullPointerException用于在edifield为空时显示状态(或)警报

时间:2012-06-11 11:10:28

标签: blackberry dialog status

我想对我的editfield进行验证。所以我正在ButtonFieldsetChangeListener方法上编写验证。如果editField为空,单击按钮时我必须显示该字段为空。要使用status.show()dialog.alert()方法显示我尝试过的消息。但两者都产生了NullPointerException。问题是什么?任何人都可以帮助解决这个问题,还是有任何其他解决方案可以解决这个问题?

我写了这样的代码:

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);
                        }

                    }

                }
            }

        });

1 个答案:

答案 0 :(得分:1)

我有一个有用的方法,我放入我的StringUtils类,检查字符串是否有效。

/**
 * Tests if a string is a non-null, non-empty string. This can be called to
 * determine if the string should be displayed, or not.
 * 
 * @param text
 *        String to test.
 * @return
 *         If <code>text</code> is <code>null</code>, returns
 *         <code>false</code>. <br>
 *         If <code>text</code> is an empty string (""), returns
 *         <code>false</code>. <br>
 *         Else returns <code>true</code>.
 */
public static boolean isNonBlankString(String text)
{
    // null text -> false
    if (text == null)
        return false;

    // empty text -> false
    if ("".equals(text))
        return false;

    return true;
}

这将帮助您解决Rupak的问题。