Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

ArrayList of objects is giving me an exception

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Following is my code:



I am getting following exception:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.set(ArrayList.java:473)
at com.mycompany.fileread_opcode.Main.main(Main.java:114)
Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
   at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
   at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
   at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:982)
   at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:929)
   at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:457)
   at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
   at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
   at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
   at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
   at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
   at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
   at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
   at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
   at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
   at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
   at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
   at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
   at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
   at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
   at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
   at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
   at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke (Method.java:566)
   at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
   at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
   at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
   at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
------------------------------------------------------------------------
BUILD FAILURE


I want to store both integer and string objects in my arraylist of objects:

Somebody please guide me.

Zulfi.
 
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 9 you can't use set() because that will replace an existing entry and so far you have no entries. Do you mean add() ?
 
Marshal
Posts: 72479
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please avoid _s in variable names. Not opr_CntList, please, but  oprCntList. Actually, that name is unpronounceable and unintelligible, so I think you should consider clearer names for all your variables. You have so many variables with similar names that your loop is confusing to read.
Also please explain how many Lists you have and what you are adding to each. If you are removing duplicates, there is bound to be a much simpler way to do it.
I challenge you to identify the serious error and the two bits of bad style in the GFG link you posted.
 
Zulfi Khan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr. Ritchie- Thanks for your guidance.

opr_CntList means operation code and their Count List. I used '_' for 'and' please guide me how to create a variable name for 'operation code and their count List.

Right now I would use the same names otherwise it would be difficult to compare with my original code:

Hi Mr. Carey Brown:

God blesses you. Your solution worked.

Zulfi.


 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Marshal
Posts: 26494
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zulfi Khan wrote:please guide me how to create a variable name for 'operation code and their count List.



A good name for an operation code: operationCode. Or if it happens that "opcode" is the standard name for an operation code in this application and everybody says that, then opCode would be satisfactory.

As for "count list"... I don't understand why you use that term, since all that code does is to produce a list which excludes some of the duplicates in the original list. There's no counting necessary, since the "add" method just adds an entry to the end of its list.

I also have to say that "tempStr" is a very strange name for something which comes out of an ArrayList<Object>. It's almost as if the entries in that list are actually String objects but for some reason the list was declared differently. Anyway I would call that variable current and the variable which is supposed to hold the previous value, I'd call that previous. There's no need to specify that they are Strings even if they are, the algorithm doesn't care and neither does the reader.
 
Zulfi Khan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a variable  opr_CntList. It contains two things: opcode strings and their integer count. For 'and" I used '-'. Please tell me what is the good replacement for '_'.

Zulfi.
 
Carey Brown
Saloon Keeper
Posts: 8020
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In mixed case (a.k.a. "camel case") the separation is denoted by the change from lower to upper case, so "opCountList".
 
Paul Clapham
Marshal
Posts: 26494
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zulfi Khan wrote:I have a variable  opr_CntList. It contains two things: opcode strings and their integer count.



Presumably those strings are in some kind of collection, a list or something like that. Storing their count somewhere is unnecessary since it should be the same as the size of the list. That is, if it's the same then it's redundant and if it's not the same then it's wrong.
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic