Backendless Support
 
Waiting for user's response

CodeRunner ClassCastException

Hey,

I'm running Code runner and getting java.lang.ClassCastException: java.util.HashMap cannot be cast to com.tapextreme.models.UserCPECoinsReached (UserCPECoinsReachedTableEventHandler.java:35)

on this line of code:

@Asset( "UserCPECoinsReached" )

here is the full exception:SEVERE: java.util.HashMap cannot be cast to com.tapextreme.models.UserCPECoinsReached

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at com.backendless.coderunner.runtime.task.EventInvocationTask.runImpl(EventInvocationTask.java:113)

at com.backendless.coderunner.runtime.concurrent.ExtendedRunnable.run(ExtendedRunnable.java:26)

at java.lang.Thread.run(Unknown Source)

Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to com.tapextreme.models.UserCPECoinsReached

at com.tapextreme.events.persistence_service.UserCPECoinsReachedTableEventHandler.afterCreate(UserCPECoinsReachedTableEventHandler.java:35)

... 7 more

here is the code:

@Asset( "UserCPECoinsReached" )

public class UserCPECoinsReachedTableEventHandler extends com.backendless.servercode.extension.PersistenceExtender<UserCPECoinsReached>

{

@Override

public void afterCreate( RunnerContext context, UserCPECoinsReached userCPECoinsReached, ExecutionResult<UserCPECoinsReached> result ) throws Exception

{

System.out.println("UserCPECoinsReachedTableEventHandler afterCreate starting");

List<Map> redirectDataList = getRedirectData(userCPECoinsReached.getUserAdvertisingId());

System.out.println("UserCPECoinsReachedTableEventHandler afterCreate got map");

if (!redirectDataList.isEmpty()) {

....(the exception already happened. System.out.println is not happening)

P.S. I have another similar code on another table pre-insert that is running successfully at the same time.

Thanks,

Tal

Leave a Comment

Comments (17)

photo
1

The exception occurs on line 35 in UserCPECoinsReachedTableEventHandler.java:

  1. com.tapextreme.events.persistence_service.UserCPECoinsReachedTableEventHandler.afterCreate(UserCPECoinsReachedTableEventHandler.java:35)

Have you tried running the code in CodeRunner debug to see what's going on?

Mark

photo
1

no. since It says the problem is at this line;

@Asset( "UserCPECoinsReached" )

which is in the definition of the class and running before the actual code

photo
1

The line of code you pointed out is a Java annotation. The only time it can cause an exception is during the compilation time, but in your case, the exception occurs during the runtime (after the program is compiled and while it is running).

To figure out where the problem is, I recommend using the debug mode of code runner, step through the code and see what's going on.

Mark

photo
1

Hi Mark,

I connected the debugger and I am debugging successfully other methods.

but this one does not go into debug mode. it crashes before the first line. maybe it is crashing when trying to pass a hashmap to the method. it is not casting to UserCPECoinsReached

photo
1

I am struggling to understand how a method can crash before the first line. A method starts on the first line, there's really no code before that to crash..

photo
1

it is crushing on the method invocation:

Apr 07, 2017 4:15:14 PM com.backendless.coderunner.runtime.task.EventInvocationTask runImpl

SEVERE: java.util.HashMap cannot be cast to com.tapextreme.models.UserCPECoinsReached

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at com.backendless.coderunner.runtime.task.EventInvocationTask.runImpl(EventInvocationTask.java:113)

at com.backendless.coderunner.runtime.concurrent.ExtendedRunnable.run(ExtendedRunnable.java:26)

at java.lang.Thread.run(Unknown Source)

Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to com.tapextreme.models.UserCPECoinsReached

at com.tapextreme.events.persistence_service.UserCPECoinsReachedTableEventHandler.afterCreate(UserCPECoinsReachedTableEventHandler.java:35)

... 7 more

photo
1

What confuses me is the following line in the stacktrace:

  1. Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to com.tapextreme.models.UserCPECoinsReached
  2. at com.tapextreme.events.persistence_service.
  3. UserCPECoinsReachedTableEventHandler.
  4. afterCreate(UserCPECoinsReachedTableEventHandler.java:35)

It tells me that the underlying exception has occurred on line 35 in UserCPECoinsReachedTableEventHandler.java

What do you make of that?

photo
1

Seems to me like for some reason UserCPECoinsReachedTableEventHandler, cannot use UserCPECoinsReached.

It cannot parse it correctly. maybe it's the structure of UserCPECoinsReached.

I don't know the insides of this. how does the hashmap convert to UserCPECoinsReached object?

package com.tapextreme.models;

import com.backendless.Backendless;

import com.backendless.BackendlessCollection;

import com.backendless.BackendlessUser;

import com.backendless.async.callback.AsyncCallback;

import com.backendless.geo.GeoPoint;

import com.backendless.persistence.BackendlessDataQuery;

import java.util.concurrent.Future;

public class UserCPECoinsReached

{

private String country;

private java.util.Date created;

private String objectId;

private String userAdvertisingId;

private String campaignAdNetwork;

private java.util.Date updated;

private String ownerId;

private Integer numOfCoins;

private String ipAddress;

public String getCountry()

{

return country;

}

public void setCountry( String country )

{

this.country = country;

}

public java.util.Date getCreated()

{

return created;

}

public String getObjectId()

{

return objectId;

}

public String getUserAdvertisingId()

{

return userAdvertisingId;

}

public void setUserAdvertisingId( String userAdvertisingId )

{

this.userAdvertisingId = userAdvertisingId;

}

public String getCampaignAdNetwork()

{

return campaignAdNetwork;

}

public void setCampaignAdNetwork( String campaignAdNetwork )

{

this.campaignAdNetwork = campaignAdNetwork;

}

public java.util.Date getUpdated()

{

return updated;

}

public String getOwnerId()

{

return ownerId;

}

public Integer getNumOfCoins()

{

return numOfCoins;

}

public void setNumOfCoins( Integer numOfCoins )

{

this.numOfCoins = numOfCoins;

}

public String getIpAddress()

{

return ipAddress;

}

public void setIpAddress( String ipAddress )

{

this.ipAddress = ipAddress;

}

public UserCPECoinsReached save()

{

return Backendless.Data.of( UserCPECoinsReached.class ).save( this );

}

public void saveAsync( AsyncCallback<UserCPECoinsReached> callback )

{

Backendless.Data.of( UserCPECoinsReached.class ).save( this, callback );

}

public Long remove()

{

return Backendless.Data.of( UserCPECoinsReached.class ).remove( this );

}

public void removeAsync( AsyncCallback<Long> callback )

{

Backendless.Data.of( UserCPECoinsReached.class ).remove( this, callback );

}

public static UserCPECoinsReached findById( String id )

{

return Backendless.Data.of( UserCPECoinsReached.class ).findById( id );

}

public static void findByIdAsync( String id, AsyncCallback<UserCPECoinsReached> callback )

{

Backendless.Data.of( UserCPECoinsReached.class ).findById( id, callback );

}

public static UserCPECoinsReached findFirst()

{

return Backendless.Data.of( UserCPECoinsReached.class ).findFirst();

}

public static void findFirstAsync( AsyncCallback<UserCPECoinsReached> callback )

{

Backendless.Data.of( UserCPECoinsReached.class ).findFirst( callback );

}

public static UserCPECoinsReached findLast()

{

return Backendless.Data.of( UserCPECoinsReached.class ).findLast();

}

public static void findLastAsync( AsyncCallback<UserCPECoinsReached> callback )

{

Backendless.Data.of( UserCPECoinsReached.class ).findLast( callback );

}

public static BackendlessCollection<UserCPECoinsReached> find( BackendlessDataQuery query )

{

return Backendless.Data.of( UserCPECoinsReached.class ).find( query );

}

public static void findAsync( BackendlessDataQuery query, AsyncCallback<BackendlessCollection<UserCPECoinsReached>> callback )

{

Backendless.Data.of( UserCPECoinsReached.class ).find( query, callback );

}

}

photo
1

I don't know what to check. Since the exception happens before the debugger reaches my breakpoint (it works for other table methods)

photo
1

Please let me know the following:

1. Application ID

2. an example of the API request which will trigger the business logic.

Thanks,

Mark

photo
1

1. app ID: AF8A6FD1-7119-6D55-FF83-BC9AA0072B00

2. it's the post insert on table UserCPECoinsReached

Thanks,

Tal

photo
1

UserCPECoinsReachedTableEventHandler - afterCreate()

photo
1

Hey.. Is there any progress in this one? We are stuck on development waiting to be able to use code runner.

Thanks,

Tal

photo
1

Tal,

We need to know how the UserCPECoinsReached object is structured when you save one. It is important for us to use the same structure as you do - that's why I asked for a sample API call. If you could share a cURL request, it would be the best.

Mark

photo
1

I am not using a URL but an insert through the app, I can share the data that is inserted or the class as it was generated.

photo
1

We need to know the data that goes into an object which you save. If you could describe it as a JSON object, it would be ideal.

photo
1

Here is the json object:

{"campaignAdNetwork":"Archy","country":"il","ipAddress":"46.117.237.187","numOfCoins":20,"userAdvertisingId":"6c606fc8-7ad7-401c-a793-029b35110e88"}

It's being inserted to the table, but creates an exception when reaching the afterCreate() method.

Thanks,

Tal