1

JSFバリデーターでNPEを取得しましたが、この問題の原因を見つけることができません。

// Validate Datacenter Name
    public void validateDatacenterName(FacesContext context, UIComponent component, Object value) throws ValidatorException, SQLException
    {
        // Original value
        Object modelValue = ((UIInput) component).getValue();

        String oriDCName = modelValue.toString();

        String s; // New inserted value

        if (value != null && !(s = value.toString().trim()).isEmpty())
        {

            if (s.length() > 18)
            {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "  Value is too long! (18 digits max)", null));
            }

            if (ds == null)
            {
                throw new SQLException("Can't get data source");
            }

            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs;
            String resDCName = null;
            try
            {
                conn = ds.getConnection();
                // if componentsstatsid <> edited componentstatsid in jsf -> throw validator exception
                ps = conn.prepareStatement("SELECT NAME from COMPONENTSTATS where NAME = ?");
                ps.setString(1, s);
                rs = ps.executeQuery();

                while (rs.next())
                {
                    resDCName = rs.getString(1);
                }

                if (resDCName != null && (!resDCName.equals(oriDCName) ))
                {
                    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "  '" + s + "' is already in use!", null));
                }

            }
            catch (SQLException x)
            {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "  SQL error!", null));
            }
            finally
            {
                if (ps != null)
                {
                    ps.close();
                }
                if (conn != null)
                {
                    conn.close();
                }
            }
        }
        else
        {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                  "  This field cannot be empty!", null));
        }

    }

一意の新しい値を入力すると、NPEが取得されます。DBテーブルにすでに見つかっている値を入力すると、が得られ...is already in use!ます。

問題はここのどこかにありますif (resDCName != null && (!resDCName.equals(oriDCName) ))が、私はそれを解決することはできません。これを修正する方法はありますか?

4

1 に答える 1

1

以下のコードのようにロジックを変更します。resDCnameがorigDCNameと等しくないようにする必要があると想定しています。

while (rs.next())
{

    resDCName = rs.getString(1);
 if (resDCName != null && resDCName.equals(oriDCName))
    {
       throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
                           "  '" + s + "' is already in use!", null));
     }
   }
于 2013-02-05T11:27:51.237 に答える