Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getter not running

 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my class Recipes has a getter to take various data, in a window i create an instance of Recipes and i try to get the "name", but if i create the file with those value, there is no value. the file is created, but empty



 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a recipe is created, the recipeName passed to the constructor comes from the member variable recipeName in NewJDialog. I don't see where anything ever sets that value to a non-empty String. the NewJDialog class has a constructor that accepts a recipeName argument, but (1) nothing ever calls it, and (2) the constructor ignores the arguments anyway. Therefore the Recipe is created with "" as a recipeName, and that's what you get back when you call getRecipeName().

Where do you expect the name to be coming from?
 
deepak kushwaha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private String ingredients[]; <<isnt this giving you an error!!!how can you
make a String of an object !!!
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:When a recipe is created, the recipeName passed to the constructor comes from the member variable recipeName in NewJDialog. I don't see where anything ever sets that value to a non-empty String. the NewJDialog class has a constructor that accepts a recipeName argument, but (1) nothing ever calls it, and (2) the constructor ignores the arguments anyway. Therefore the Recipe is created with "" as a recipeName, and that's what you get back when you call getRecipeName().

Where do you expect the name to be coming from?


i forgot to add :P i added now but the file created is empty
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepak kushwaha wrote:private String ingredients[]; <<isnt this giving you an error!!!how can you
make a String of an object !!!

is a simple string array
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepak kushwaha wrote:private String ingredients[]; <<isnt this giving you an error!!!how can you
make a String of an object !!!


This is a perfectly valid declaration for a variable that holds an array of Strings. You can write it as either

private String[] ingredients;

or

private String ingredients[];

Both mean exactly the same thing.
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i have no error now, but the file i try to create, is created but empty
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as I said, the recipeName is never getting set to a non-empty value; the same holds for the other members, I would guess.
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Well, as I said, the recipeName is never getting set to a non-empty value; the same holds for the other members, I would guess.


no read again, i fixed that,

before to create the file i made a print of value to put in file, and the value are ok, but the file is the same empty
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there's an empty catch block in the "try" around the file writing code. That means if something fails, you won't hear about it. Empty catch blocks are murderous evil -- always at least include a printStackTrace() so you can see what happened! Add this, and see if you get an error message.
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
solved that thing

now i get this error while working on some number value



Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.DefaultListModel.getElementAt(DefaultListModel.java:70)
at desktopapplication1.NewJDialog.jButton2_addActionPerformed(NewJDialog.java:168)
at desktopapplication1.NewJDialog.access$100(NewJDialog.java:22)
at desktopapplication1.NewJDialog$2.actionPerformed(NewJDialog.java:86)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
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)

what is it?
 
Michael Montean
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your loop is going out of bounds on the index.

Java index starts at 0 in an array. Therefore an array of size 10 would have a maximum index of 9.

Your code says


c will be greater than the maximum index in the array on the last iteration. You need to say,
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Michael Montean
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ty i corrected the index but still an error

while ( c < jList1.getModel().getSize() ) {
System.out.println(c+"<"+jList1.getModel().getSize());
System.out.println(jList1.getModel().getElementAt(c));
ingredients[c] = (String)jList1.getModel().getElementAt(c);
c++;
}

this code, stamp just the first value of the list
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ty i corrected the index but still an error

while ( c < jList1.getModel().getSize() ) {
System.out.println(c+"<"+jList1.getModel().getSize());
System.out.println(jList1.getModel().getElementAt(c));
ingredients[c] = (String)jList1.getModel().getElementAt(c);
c++;
}

this code, stamp just the first value of the list




this is how i edited, but still doesnt work, just stamp the first raw of jList, if i put setingredient((string= string under comment, it works,why???'
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a method setIngredient(String, int) in your Recipes class - perhaps you forgot to write it?
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i edited the class and didnt updated on here, this is the class Recipes



 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see anything wrong. Do you get any runtime errors or warnings during compilation?
 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:I can't see anything wrong. Do you get any runtime errors or warnings during compilation?


no is really strange

when i made the stamp of the while, the while run just the first iteration if i have the command that assign the value of the list to the array

 
Antony Amicone
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please helppppppppp i need to finish soon this program and this thing is blocking me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic