Hi - I am getting below ‘npe’ while trying to call ‘incrementAndGet’ android API for counters (code lines below):
IAtomic<Long> myCounter = Backendless.Counters.of(“merchant_reg_count”, Long.class);
myCounter.incrementAndGet(createRegCounterCb(context, callback));The callback function does not get called - and exception comes prior to that.Am I doing anything wrong ?? Am I required to create/initialize counter before calling ‘incrementAndGet’ ??Also, can’t I see counter values from console ??
java.lang.NullPointerException: Attempt to invoke interface method ‘java.lang.reflect.Type[] java.lang.reflect.ParameterizedType.getActualTypeArguments()’ on a null object reference
at com.backendless.utils.ReflectionUtil.getCallbackGenericType(ReflectionUtil.java:126)
at com.backendless.atomic.AtomicCallback.handleResponse(AtomicCallback.java:47)
at com.backendless.async.message.AsyncMessage$ResponseHandler.handle(AsyncMessage.java:64)
at com.backendless.async.message.AsyncMessage.handleCallback(AsyncMessage.java:41)
at com.backendless.core.AndroidCarrier$1.handleMessage(AndroidCarrier.java:37)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Thanks.
Regards,
Aditya
Aditya,
Do you have Backendless.initApp call in your application? The only time I was able to get a NPE if the initApp method is not invoked.
As for console, no, there is no place in console where you can see counters.
Mark
Yes, it’s called once in main activity - I do a file upload before fetching counter and that works fine …in fact I call get counter method from the file upload callback function only …below is complete code …
new LoadingCallback<BackendlessFile>(context, AppConstants.imageUpload, true)
{
@Override
public void handleResponse(BackendlessFile file)
{
setImage_url(file.getFileURL());
Log.d(TAG, “Display image uploaded successfully at :” + getImage_url());
// stop progress dialog
super.handleResponse(file);
// If registration scenario, fetch unique ciunter value asynchronously
if(registerCase) {
IAtomic<Long> myCounter = Backendless.Counters.of("merchant_reg_count, Long.class);
myCounter.incrementAndGet(createRegCounterCb(context, callback));
} else {
callback.onSuccess();
}
}
@Override
public void handleFault( BackendlessFault fault ) {
Log.e(TAG, "Image file upload failed: " + fault.toString());
// stop progress dialog
super.handleFault(fault);
// Image upload failed, but still try for registration
if(registerCase) {
Backendless.Counters.incrementAndGet( MERCHANT_REG_COUNTER_NAME, createRegCounterCb(context, callback) );
} else {
callback.onFailure(ErrorCodes.FILE_UPLOAD_FAILED);
}
}
};
Below is reg counter callback function - however, this does not get called and exception comes prior to this.private LoadingCallback<Long> createRegCounterCb(final Context context, final OnResult callback)
{
// start showing in progress dialog also
return new LoadingCallback<Long>(context, AppConstants.registrationCounter, true)
{
@Override
public void handleResponse(Long value)
{
Log.d(TAG, "Unique registration counter value: "+value);
mRegCounter = value;
if(mRegCounter == null || mRegCounter == 0) {
super.handleResponse(value);
callback.onFailure(ErrorCodes.GENERAL_ERROR);
return;
}
Log.d(TAG, “Unique registration counter value :” + mRegCounter);
// Set userid and ‘cashback table name’
setAutoUserId();
setCashbackTable();
// stop progress dialog
super.handleResponse(value);
// Register asynchronously
Backendless.UserService.register(backendUser, createRegCallback(context, callback));
}
@Override
public void handleFault( BackendlessFault fault ) {
Log.e(TAG, "Increment registration counter value failed: " + fault.toString());
// stop progress dialog
super.handleFault(fault);
callback.onFailure(ErrorCodes.GENERAL_ERROR);
}
};
}
Can you tell me which line causes the exception? Are you absolutely sure it is the line fetching the counter value?
yes, 100% it comes in between calling incrementAndGet() and before its callback gets called.
I added 3 below debug statement, and I see them - but I do not see debug statement from callback.
Log.d(TAG, “Get IAtomic counter”);
IAtomic<Long> myCounter = Backendless.Counters.of(MERCHANT_REG_COUNTER_NAME, Long.class);
Log.d(TAG, “calling incrementAndGet”);
myCounter.incrementAndGet(createRegCounterCb(displayImg, context, callback));
Log.d(TAG, “returned from incrementAndGet”);
I tried vanilla incrementAndGet() - and its working - so some issue with my code.
I am checking and will update. Thanks.
I do not understand what your LoadingCallback does. Here, try this in a JAVA program (not android):
public class GetCounter
{
public static void main( String[] args )
{
String APPLICATION_ID = “PUT-YOUR-APP-ID-HERE”;
final String SECRET_KEY = “PUT-YOUR-ANDROID-KEY-HERE”;
Backendless.initApp( APPLICATION_ID, SECRET_KEY, “v1” );
Backendless.Counters.of("merchant_reg_count", Long.class ).incrementAndGet( new AsyncCallback<Long>()
{
@Override
public void handleResponse( Long counterValue )
{
System.out.println( "got value - " + counterValue );
}
@Override
public void handleFault( BackendlessFault fault )
{
System.out.println( "got fault - " + fault.getMessage() );
}
} );
}
}
Hi - The problem got solved. You were right - it was somewhere else, due to a race condition.
Thanks a lot for bearing with me
LoadingCallback is from your ‘Restaurant-to-go’ app - issue anyways was in my code and not due to LoadingCallback.