Java.Lang.Classcastexception

java.lang.ClassCastException

According to the documentation:

Thrown to indicate that the code has attempted to cast an Object to a subclass
of which it is not an instance. For example, the following code generates a ClassCastException:

Object x = new Integer(0);
System.out.println((String)x);

java.lang.ClassCastException: com.example.readdoang.ui.login_regist_activity.LoginRegistActivity cannot be cast to com

in your class RegisterFragment,override the onCreateView(), in this method you can pass your view(fragment) to presenter like this :

presenter =  RegisterPresenter(this)

and your presenter should like this :

class RegisterPresenter(internal val view: RegisterView.View) : RegisterView.Presenter

Unable to resolve java.lang.ClassCastException

Problem is here

View cartTotalView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cart_total_amount_layout, viewGroup, false);
return new CartItemViewHolder(cartTotalView); // Returning wrong ViewHolder it should be CartTotalAmountViewHolder

java.lang.ClassCastException Why?

Perhaps specify the target class as the second argument to createNativeQuery:

val query = entityManager.createNativeQuery(sql, Resume.class)

Without that each row is represented as an Object[], so the return type of getResultList would be List<Object[]>.

java.lang.ClassCastException: myClass cannot be cast to class java.util.List

You're saying: MyClass (the entity) consists of lists, therefore it is a list.

That's as crazy as saying: My house is made of bricks, therefore my house is a brick.

The central job here is taking the thing you pass to the entity() method and turning that into a sack of bytes, because the HTTP protocol sends bytes. How does that work? That's where you need to learn how your tools and libraries work: I'm assuming that you're using Jersey/JAX-RS here. That is set up as follows:

  • You can send any object at all back.
  • However, that object is spooled through a bunch of registered 'translators' that each have the ability to translate a subset of object types into a stream of bytes. Out of the box, the system knows how to 'translate' Strings (that's trivial - convert the strings to bytes using .getBytes(someCharsetEncoding), voila - bytes) and a few other things. But you can add on more translators if you want. That is why the entity() method takes any Object.
  • Some registered translator assumes they are lists when they are not, causing your error. (Because you have an instance of MyClass, which isn't a list. Sure, all its fields are lists, but, a house is not a brick).

But the solution (probably) isn't in fixing that 'translator'. First think it through: What are you actually trying to do here? HTTP just sends bytes, so what bytes do you want to send? That MyClass object needs to be turned into bytes one way or another. How should that go? What service is calling this? What does it do?

Here's one example answer:

Some website has javascript that makes an AJAX call to this endpoint and turns whatever it receives into a javascript object by parsing it with a JSON library, or just eval()ing it.

If that is your answer, that you want your MyClass entity to be turned into JSON. Jackson can do that for you.



Related Topics



Leave a reply



Submit