| Author |
HELP NEEDED~~
|
Jose Cisse
Greenhorn
Joined: Mar 20, 2006
Posts: 28
|
|
in this bit:"System.out.println("The entry for: " + InventoryItem.getName()+ " has been removed");" how do you make the name of the book shown after"The entry for: "??? code " InventoryItem.getName()" doesnt work actually~~~error message coming out:" non-static method getName() cannot be referenced from a static context" AND basically in my InventoryItem, it looks like: so where is the problem??? [ March 22, 2006: Message edited by: Jose Cisse ] [ March 22, 2006: Message edited by: Jose Cisse ] [ March 22, 2006: Message edited by: Jose Cisse ]
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
InventoryItem's getName() method is not static, so you can't access it in a static way. You need an instance of InventoryItem to call the getName() method on. For example:
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Jonathan Moore
Ranch Hand
Joined: Nov 02, 2005
Posts: 36
|
|
Hi Jose, You need to reference the actual item you are removing from the ArrayList and call getName() on it - as Paul said you are trying to call the method on the class, not the object. As far as I'm aware ArrayList doesn't have a removeItem() method, but it does have a remove() method which returns the object you are removing. So you can replace your two lines of code with: Cheers Jon
|
 |
Jose Cisse
Greenhorn
Joined: Mar 20, 2006
Posts: 28
|
|
Cheers Jon[/qb]<hr></blockquote> thanks for your help~~~ for "InventoryItem item = (InventoryItem)inventory.remove(toRemove - 1);" i got a error message:"cannot resolve symbol - method remove(int)" ...so seems i need more your help~~~  [ March 22, 2006: Message edited by: Jose Cisse ]
|
 |
Jose Cisse
Greenhorn
Joined: Mar 20, 2006
Posts: 28
|
|
|
???
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Jose Cisse: ...for "InventoryItem item = (InventoryItem)inventory.remove(toRemove - 1);" i got a error message:"cannot resolve symbol - method remove(int)"...
I don't know what type "inventory" is, but it does not appear to have a remove(int) method. It seems that you're trying to treat "inventory" as an ArrayList, but that doesn't quite make sense because your original post has a line... ArrayList items = inventory.getInventoryItems(); So the question is: What type is "inventory"?
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Jose Cisse
Greenhorn
Joined: Mar 20, 2006
Posts: 28
|
|
how about i change them to: [ March 22, 2006: Message edited by: Jose Cisse ] :roll: [ March 22, 2006: Message edited by: Jose Cisse ]
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Jose Cisse: how about i change them to...
Did it work? The question is still the same: What type is "inventory"? And are you calling the proper methods defined in that type?
|
 |
Jose Cisse
Greenhorn
Joined: Mar 20, 2006
Posts: 28
|
|
Originally posted by marc weber: Did it work? The question is still the same: What type is "inventory"? And are you calling the proper methods defined in that type?
no it doesnt, for line " InventoryItem temp=(InventoryItem)inventory.get(toRemove - 1);" cannot resolve symbol-method get(int)... ... and in my inventory class , i did like:
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
A little advice: You should do some basic tutorials first. You don't seem to know the basics, and you don't understand the compiler errors. 'Trial and error' will not resolve your problems.
|
[My Blog]
All roads lead to JavaRanch
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Your inventory class does not seem to have a method called "get," so "inventory.get()" does not work.Your inventory class does not seem to have a method called "remove," so "inventory.remove()" does not work.Is there a method in your inventory class that you want to call? Hint: I think you took a wrong turn several posts above when an assumption was made that "inventory" represented an ArrayList.
|
 |
Jonathan Moore
Ranch Hand
Joined: Nov 02, 2005
Posts: 36
|
|
Hmm, sorry I didn't read it properly - I was assuming inventory was the ArrayList. You won't be able to call get() on your inventory item because you haven't defined a get() method. You can either define a get() method and your code snippet should work, or you could change your inventory class so that the removeItem() method returns the item you have removed. Wither way will obtain a reference to the item you are about to remove so that you can call getName() on it. Another tip - you should capitalise class names, so call your class Inventory rather than inventory. Cheers Jon
|
 |
Jose Cisse
Greenhorn
Joined: Mar 20, 2006
Posts: 28
|
|
i think i defined get() method in my InventoryItem class, plz have a look:
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Jonathan Moore: Hmm, sorry I didn't read it properly - I was assuming inventory was the ArrayList...
Yeah, it's hard to get a good overall picture based on snippets.
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Jose Cisse: i think i defined get() method in my InventoryItem class...
No. InventoryItem has a few "getter" methods: getName, getDescription, and getPrice. But it does not have a method simply called "get." Also, Inventory and InventoryItem are two different classes, neither of which extend the other. So even if InventoryItem did have a "get" method, that would not allow you to call "get" on an instance of Inventory. [ March 23, 2006: Message edited by: marc weber ]
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
i think i defined get() method in my InventoryItem class...
Good luck Marc.
|
 |
Jose Cisse
Greenhorn
Joined: Mar 20, 2006
Posts: 28
|
|
oh~~~finally i figured it out~~~many thanks for you guys~~~
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
Jose, that's cool you've figured it out. You may be interested to read a tutorial : http://java.sun.com/docs/books/tutorial/
|
 |
 |
|
|
subject: HELP NEEDED~~
|
|
|