shuffle numbers in an Arraylist - arraylist

#SuppressLint("SetTextI18n")
public void kocokAjah (View view) {
TextView textViewCard = findViewById(R.id.textViewCard);
relativeLayoutGone.setVisibility(View.INVISIBLE);
textViewCard.setVisibility(View.VISIBLE);
int MyFirstNumber = Integer.parseInt(editNumBegin.getText().toString());
int MyLastNumber = Integer.parseInt(editNumEnd.getText().toString());
ArrayList<String> kocok = new ArrayList<>();
for (int i = MyFirstNumber; i <= MyLastNumber; i++) {
kocok.add(Integer.toString(i));
Collections.shuffle(kocok);
textViewCard.setText((CharSequence) kocok);
}
}
}
This is my code to shuffle numbers in an ArrayList thean sen it to a TextView.
but whenever i the code is executed the app crash.
log :
FATAL EXCEPTION: main
Process: com.theinmark.fernando.kocoktestmainactivity, PID: 6506
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.CharSequence
at com.theinmark.fernando.kocoktestmainactivity.MainActivity.kocokAjah(MainActivity.java:91)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.view.View$1.onClick(View.java:4015) 
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
01-27 16:21:27.520 6506-6506/? I/Process: Sending signal. PID: 6506 SIG: 9
Any advice?
Thanks

In the middle of that long error message you will find this:
Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.CharSequence
The problem is an ArrayList (regadless of what it contains) is not a CharSequence. If I remember correctly, Strings implement CharSequence, so maybe you'll want to build a string instead of a list of strings.
The quick and dirty way is to just "add" the strings together.
String kocok="";
for(...) {
kocok += Integer.toString(i);
}
The better way is to use a StringBuilder. But it's only really necessary if you add a lot of strings together.
Also note that you are calling setText on the text view inside the loop. You would typically to that after the loop.
If you want to shuffle the numbers before appending them to the string, I would recommend that you first add the numbers to an array, then after that loop shuffle once, then another loop to convert to a string, then after that set text on text field.
/* Pseudocode.*/
for (int i; ...) {
array.add(i);
}
Collections.shuffle(array);
String str=""
for (int i: array) { /* for each int i in array. */
str+=i;
}
textview.setText(str);

Related

My position coordinate, invoke virtual method on a null object reference

In one of the fragments of my app I am trying to get the coordinates of my current position but when I try to save them in a variable this is not possible because I get a reference to a null object. I think the reason is a matter of time, my position is not initialized before being saved in the variable. How can I make sure this happens before saving it in the variable? what am I doing wrong?
This is the function i tried to get it coordinates
fun routePathPosi(){
myLocationNewOverlay = MyLocationNewOverlay(GpsMyLocationProvider(context), mapView)
myLocationNewOverlay.enableMyLocation()
myLocationNewOverlay.isDrawAccuracyEnabled = true
val a: Double = myLocationNewOverlay.myLocation.latitude
val b: Double = myLocationNewOverlay.myLocation.longitude
println("my location is : $a and $b")
}
this is my error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: uk.co.lorenzopulcinelli.navigationdrawer, PID: 32684
java.lang.NullPointerException: Attempt to invoke virtual method 'double org.osmdroid.util.GeoPoint.getLatitude()' on a null object reference
at uk.co.lorenzopulcinelli.navigationdrawer.NavFun.routePathPosi(NavFun.kt:90)
at uk.co.lorenzopulcinelli.navigationdrawer.CreatePathFragment.onCreateView$lambda-3(CreatePathFragment.kt:98)
at uk.co.lorenzopulcinelli.navigationdrawer.CreatePathFragment.$r8$lambda$wqVLFXUeXU7qjx0UlbTvNIoGrJQ(Unknown Source:0)
at uk.co.lorenzopulcinelli.navigationdrawer.CreatePathFragment$$ExternalSyntheticLambda2.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7185)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
at android.view.View.performClickInternal(View.java:7162)
at android.view.View.access$3500(View.java:819)
at android.view.View$PerformClick.run(View.java:27678)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
I/Process: Sending signal. PID: 32684 SIG: 9

Creating Intent results in "Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()'"

I am trying to follow the code on Google for creating a notification on Wear OS devices.
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.NotificationCompat.WearableExtender
class MainActivity : WearableActivity() {
val notificationId = 1
// The channel ID of the notification.
val id = "my_channel_01"
// Build intent for notification content
val viewPendingIntent = Intent(this, com.alborz.innout.MainActivity::class.java).let { viewIntent ->
viewIntent.putExtra(EXTRA_EVENT_ID, notificationId)
PendingIntent.getActivity(this, 0, viewIntent, 0)
}
// Notification channel ID is ignored for Android 7.1.1
// (API level 25) and lower.
val notificationBuilder = NotificationCompat.Builder(this, id)
.setLocalOnly(true)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("TITLE")
.setContentText("TEXT")
.setContentIntent(viewPendingIntent)
Later I try to activate the notification, like this:
button_in.setOnClickListener {
NotificationManagerCompat.from(this).apply {
notify(notificationId, notificationBuilder.build())
}
}
But I am getting an error which I do not understand. (I am super new to Kotlin so might have missed something obvious)
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.alb.innout/com.alb.innout.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2844)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:142)
at android.content.ComponentName.<init>(ComponentName.java:130)
at android.content.Intent.<init>(Intent.java:6082)
at com.alb.innout.MainActivity.<init>(MainActivity.kt:46)
Line 46 is val viewPendingIntent = Intent ...
You should put this Intent and notification setup inside a function and call the function when it’s time to send the notification. The way you have it now, the Intent is created at the time the Activity is instantiated, which is before onCreate() and too early for it to be passed around as the this argument of functions and constructors that want to use its Context.

Getting RuntimeException for kotlin code

I am using Bluetooth LE for fetching surrounding devices but I am not getting a list and also I am getting Runtime Exception here is stack-trace
06-18 13:59:33.188 10189-10189/com.labs.aress.bleomronkotlin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.labs.aress.bleomronkotlin, PID: 10189
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.labs.aress.bleomronkotlin/com.labs.aress.bleomronkotlin.MainActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
at android.app.ActivityThread.deliverResults(ActivityThread.java:4094)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4137)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data
at com.labs.aress.bleomronkotlin.BaseMainActivity.onActivityResult(BaseMainActivity.kt)
at android.app.Activity.dispatchActivityResult(Activity.java:6931)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4090)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4137) 
at android.app.ActivityThread.-wrap20(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6123) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
code is here
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
AppLog.dMethodIn()
super.onActivityResult(requestCode, resultCode, data)
if (REQUEST_CODE_SCAN != requestCode) {
AppLog.w("Unknown request code : $requestCode")
return
}
if (BleScanActivity.RESPONSE_CODE_CONNECT != resultCode) {
return
}
val discoverPeripheral = data.getParcelableExtra<DiscoverPeripheral?>(BleScanActivity.EXTRA_CONNECT_REQUEST_PERIPHERAL)
if (null == discoverPeripheral) {
AppLog.e("null == discoverPeripheral")
return
}
initDeviceInfoView()
initConnectionView()
onConnect(discoverPeripheral)
}
I dont understand why data is throwing null
The problem with your code is that the data parameter is not nullable, but Android calls it with a null value.
If you override the onActivityResult function in Android Studio the function signature you get is actually
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
as opposed to what you have
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent)
The difference is the ? after the "data: Intent"
Add the ? and make the appropriate changes to your code (to account for the nullable value) and it should work.

ERR EXEC without MULTI - spring redis+jedis

I meet with exception during a transactional operation with spring-data-redis
RedisTemplate<String,Object> cartCache;
public void create(final Cartline cartline) {
Object txResults = cartCache.execute(new SessionCallback<List>() {
public List execute(RedisOperations operations) throws DataAccessException {
String cartId = cartline.getMemberId();
String cartlineId = cartline.getCartlineId();
operations.multi();
......
return operations.exec();
}
});
}
redis.clients.jedis.exceptions.JedisDataException: ERR EXEC without MULTI
at redis.clients.jedis.Protocol.processError(Protocol.java:115)
at redis.clients.jedis.Protocol.process(Protocol.java:133)
at redis.clients.jedis.Protocol.read(Protocol.java:202)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:285)
at redis.clients.jedis.Connection.getRawObjectMultiBulkReply(Connection.java:230)
at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:236)
at redis.clients.jedis.Transaction.exec(Transaction.java:38)
at org.springframework.data.redis.connection.jedis.JedisConnection.exec(JedisConnection.java:738)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.data.redis.core.CloseSuppressingInvocationHandler.invoke(CloseSuppressingInvocationHandler.java:57)
at com.sun.proxy.$Proxy512.exec(Unknown Source)
at org.springframework.data.redis.core.RedisTemplate$3.doInRedis(RedisTemplate.java:593)
at org.springframework.data.redis.core.RedisTemplate$3.doInRedis(RedisTemplate.java:591)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:190)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:140)
at org.springframework.data.redis.core.RedisTemplate.execRaw(RedisTemplate.java:591)
at org.springframework.data.redis.core.RedisTemplate.exec(RedisTemplate.java:578)
at com.znova.stone.cart.repository.impl.CartCacheImpl$1.execute(CartCacheImpl.java:51)
at com.znova.stone.cart.repository.impl.CartCacheImpl$1.execute(CartCacheImpl.java:1)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:217)
I read this post ERR EXEC without MULTI - Jedis which indicates a multi with exec, I also have redisTemplate.setEnableTransactionSupport(true); enabled, the error stood still.
I removed operation logic within the multi-exec block, I found there is no difference whatever is there, the exception happens event with a zero-operation block.
I had this same problem and fixed it by updating spring-data-redis from 1.4.2.RELEASE to 1.5.2.RELEASE.

"Absent Code attribute in method that is not native or abstract in class" exception when starting a project in Netbeans 7.2

I've just installed the newest version of Netbeans ( 7.2 ) and tried to run an existing Swing project in it. I got the following exception:
StackTrace:
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file com/eositservices/eldorado/dialogs/zahlungverrechnung/model/InitDataList
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getDeclaredMethod(Class.java:1935)
at java.awt.Component.isCoalesceEventsOverriden(Component.java:5975)
at java.awt.Component.access$500(Component.java:169)
at java.awt.Component$3.run(Component.java:5929)
at java.awt.Component$3.run(Component.java:5927)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Component.checkCoalescing(Component.java:5926)
at java.awt.Component.<init>(Component.java:5895)
at java.awt.Container.<init>(Container.java:249)
at javax.swing.JComponent.<init>(JComponent.java:570)
at javax.swing.JPanel.<init>(JPanel.java:65)
at javax.swing.JPanel.<init>(JPanel.java:92)
at javax.swing.JPanel.<init>(JPanel.java:100)
at com.eositservices.eldorado.masken.FormForderung.<init>(FormForderung.java:72)
at com.eositservices.eldorado.tabsheets.zahlung.TSZahlung.<init>(TSZahlung.java:183)
at com.eositservices.eldorado.masken.TFormForderung.addTabs(TFormForderung.java:366)
at com.eositservices.eldorado.masken.TFormForderung.initializeComponents(TFormForderung.java:275)
at com.eositservices.eldorado.masken.TFormForderung.configureComponents(TFormForderung.java:148)
at com.eositservices.bones.view.components.EOSInternalFrame.setUp(EOSInternalFrame.java:81)
at com.eositservices.eldorado.masken.TFormForderung.<init>(TFormForderung.java:113)
at com.eositservices.eldorado.main.MainFrame.oeffneForderung(MainFrame.java:577)
at com.eositservices.eldorado.main.MainFrame.showForderung(MainFrame.java:488)
at com.eositservices.eldorado.dev.QuickLogin.openForderung(QuickLogin.java:109)
at com.eositservices.eldorado.dev.QuickLogin.openInitialViews(QuickLogin.java:143)
at com.eositservices.eldorado.main.MainFrame$2.run(MainFrame.java:433)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:646)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:607)
at java.awt.EventQueue$1.run(EventQueue.java:605)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:616)
at com.eositservices.bones.view.GlobalHotkeyManager.dispatchEvent(GlobalHotkeyManager.java:163)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
The method in question looks like this:
#Override
public boolean add( InitData e ) {
boolean returnValue = super.add( e );
Collections.sort( this, c );
return returnValue;
}
The strange thing is that the exception only comes when I set the project properties to "Compile on save". When the option is unchecked it runs fine. We had no such problems in the previous versions of NetBeans. Is it just a coincidence and I should look elsewhere for the source of this error or has anyone had similar problems? Thanks in advance for your help.
It seems to be a bug in the NetBeans.
When this exception occurs, let's make an error in the code (e.g
#Override
**efefefefef**
public boolean add( InitData e ) {
boolean returnValue = super.add( e );
Collections.sort( this, c );
return returnValue;
}
)
save it and when the Netbeans displays the error, correct it back. I have tried to clear the Project, remove everything from the cache, but nothing happened. But this way helped on me.