serverError: class java.lang.ClassCastException java.lang.Integer cannot be cast to java.lang.String

Peter Penzov

I want to create validator for input filed in order to check values and send error message if the inserted value is not int.

bean:

public class PricingCalculatorValidator implements Validator
{
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
    {
        // Cast the value of the entered input to String.
        String input = (String) value;

        // Check if they both are filled in.
        if (input == null || input.isEmpty())
        {
            return; // Let required="true" do its job.
        }

        // Compare the input with the confirm input.
        if (containsDigit(input))
        {
            throw new ValidatorException(new FacesMessage("Value is not number."));
        }
    }

    public final boolean containsDigit(String s)
    {
        boolean containsDigit = false;

        if (s != null && !s.isEmpty())
        {
            for (char c : s.toCharArray())
            {
                if (containsDigit = Character.isDigit(c))
                {
                    break;
                }
            }
        }

        return containsDigit;
    }

}

What is the proper way to cast the inserted value? Now I get exception

serverError: class java.lang.ClassCastException java.lang.Integer cannot be cast to java.lang.String
BalusC

As per the exception, JSF is actually passing an Integer instance as value to the validate() method. Technically, you should be casting it to Integer as below to keep the Java runtime happy.

// Cast the value of the entered input to Integer.
Integer input = (Integer) value;

Apparently you already bound the input field to an Integer property in the model like so:

<h:inputText value="#{bean.pricing}" />
private Integer pricing;

In other words, the whole custom validator is unnecessary. There's no point of validating if the value is an integer if it's already an Integer in first place. It can impossibly contain a non-digit as value.

Just get rid of that custom validator altogether.

JSF has several builtin converters for standard types like Integer which run automatically depending on the model value type. And, converters run right before validators. That's why the value already arrives as Integer in your custom validator. The only thing which seems relevant in your custom validator is the error message which is different from the standard conversion error message. In case you merely wanted to customize the conversion error message on the specific input field, just set it as converterMessage attribute on the input field.

<h:inputText value="#{bean.pricing}" converterMessage="Value is not numeric." />

Unrelated to the concrete problem, "validating" if the value represents a valid Integer or not (this process is in JSF actually called conversion and is supposed to take place in a custom converter) can be performed in a much simpler way:

String string = getItSomehowWithoutClassCastException();

try {
    Integer integer = Integer.valueOf(string);
} catch (NumberFormatException e) {
    throw new ConverterException(new FacesMessage("Value is not an integer."));
}

No need to inspect every single digit if you ultimately want to get an Integer. As said, the JSF builtin converter already takes care of this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer in tableau?

From Dev

java.lang.ClassCastException: class java.sql.Date cannot be cast to java.lang.String

From Dev

Hibernate : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

From Dev

java.lang.Integer cannot be cast to java.lang.String

From Dev

Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

From Dev

java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

From Dev

Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

From Dev

JRException: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean

From Java

Cannot cast class java.lang.Integer to class java.lang.String

From Dev

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String

From Dev

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String

From Dev

How to fix: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Integer

From Dev

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer in DAO

From Dev

How to fix: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Integer

From Dev

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer

From Dev

java.lang.ClassCastException (Cannot cast class to same class)

From Dev

java.lang.RuntimeException: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String

From Dev

java.lang.ClassCastException: cannot be cast to java.lang.Object

From Dev

java.lang.ClassCastException: CLASS/Activity cannot be cast to MainActivity

From Dev

java.lang.ClassCastException:[I cannot be cast to java.lang.Integer

From Dev

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

From Dev

ClassCastException: JSONArray cannot be cast to java.lang.String[]

From Dev

How to resolve ClassCastException: java.lang.String cannot be cast exception

From Dev

java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String

From Dev

SDN4 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long when using AttributeConverter

From Dev

Hibernate HQL casting java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

From Dev

SDN4 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long when using AttributeConverter

From Dev

java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class

From Dev

Mockito java.lang.ClassCastException: my.custom.class.Project cannot be cast to java.lang.Boolean

Related Related

  1. 1

    java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer in tableau?

  2. 2

    java.lang.ClassCastException: class java.sql.Date cannot be cast to java.lang.String

  3. 3

    Hibernate : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

  4. 4

    java.lang.Integer cannot be cast to java.lang.String

  5. 5

    Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

  6. 6

    java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

  7. 7

    Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

  8. 8

    JRException: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean

  9. 9

    Cannot cast class java.lang.Integer to class java.lang.String

  10. 10

    java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String

  11. 11

    java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String

  12. 12

    How to fix: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Integer

  13. 13

    java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer in DAO

  14. 14

    How to fix: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Integer

  15. 15

    java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer

  16. 16

    java.lang.ClassCastException (Cannot cast class to same class)

  17. 17

    java.lang.RuntimeException: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String

  18. 18

    java.lang.ClassCastException: cannot be cast to java.lang.Object

  19. 19

    java.lang.ClassCastException: CLASS/Activity cannot be cast to MainActivity

  20. 20

    java.lang.ClassCastException:[I cannot be cast to java.lang.Integer

  21. 21

    java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

  22. 22

    ClassCastException: JSONArray cannot be cast to java.lang.String[]

  23. 23

    How to resolve ClassCastException: java.lang.String cannot be cast exception

  24. 24

    java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String

  25. 25

    SDN4 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long when using AttributeConverter

  26. 26

    Hibernate HQL casting java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

  27. 27

    SDN4 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long when using AttributeConverter

  28. 28

    java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class

  29. 29

    Mockito java.lang.ClassCastException: my.custom.class.Project cannot be cast to java.lang.Boolean

HotTag

Archive