Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a method from an object in a vector  RSS feed

 
Victor Tassone
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello 
I have tried searching Google before posting and have not found anything that helps me. I don't know if I am even asking the right questions, so sorry if this has been asked before, I am still very new to java and programming in general.
In my program, I have a vector which I put objects that are created by a user into. I then display the objects in this vector on a JList. I am trying to run a method that increases a variable on the specific object which is selected from the JList when a button is pressed. So What I have tried is to put this in my buttonListener/actionPerformed code
;
I get an error from this saying that the method "increaseMethod" is undefined for the type object.
I have also tried

How should I go about calling a method from an object that is in a vector?
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method "increaseMethod" is undefined for the type object. 

If the definition of the Vector object does not use the generics notation:  Vector<YourClassName> then the vector is assumed to hold Object class elements.  The Object class does not have an increaseMethod() method.
Make sure you define the Vector to contain the type of objects you are putting in it.
The same is true of the JList class.

See: http://docs.oracle.com/javase/tutorial/java/generics/index.html
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

How have you declared this JList? What specific type of object are you putting into it?  Please show us your code where you're adding objects to the vector and also where you're declaring/creating the JList.

If you're getting errors, please cut and paste the error messages exactly as they are so we have a better idea of what's going on.
 
Victor Tassone
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the replies!
The Program has two tabs in it, In the first tab you enter an athlete's first name, last name, and sport and there is a button to create an athlete object with the information given.
The second tab(which I am having trouble with) you are to select an athlete from a JList and select a type of medal (gold, silver, bronze) with radio buttons and then hit a button which increases that athlete's count of the specified medal.
Below is the code from the second tab. There is a class Athlete which has the methods increaseGold(), increaseSilver(), increaseBronze().
Sorry that I kept it really general in the original post, I thought that might make it easier.
myapplet.PNG
[Thumbnail for myapplet.PNG]
ScreenShot of 2nd tab
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please copy the full text of the error messages and paste it here. It has important info about the error.

Also the code needs to use generics for several classes to allow the compiler to test for correctly used data types and to handle any needed casting.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!