Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP NEEDED~~

 
Jose Cisse
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jonathan Moore
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
???
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
Jose Cisse
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • 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
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i think i defined get() method in my InventoryItem class, plz have a look:
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i think i defined get() method in my InventoryItem class...



    Good luck Marc.
     
    Jose Cisse
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    oh~~~finally i figured it out~~~many thanks for you guys~~~
     
    Christophe Verré
    Sheriff
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jose,
    that's cool you've figured it out.

    You may be interested to read a tutorial :
    http://java.sun.com/docs/books/tutorial/
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic