Problems extracting data from JTables

Hey all,

Here's the situation, I'm making an inventory program for my university rec center and i'm using a JTable for them to input data. I add up one column (prices) of that data using the actionListener, it gets the data fine except for the last data element, it returns null.So, basically, no matter the amount of data entered in the table, and no matter the amount of empty cells after the last data entry, it returns the last data entry as null. Hope this makes sense, and your help is greatly appreciated!

-Justin

[542 byte] By [Krus3ra] at [2007-10-3 10:03:59]
# 1
Not sure how you expect anyone to know what the problem is with so few details. Please create a Short,Self Contained, Compilable and Executable, Example Program showing your problem and post it here. Then others can run it and make suggestions.
zadoka at 2007-7-15 5:23:06 > top of Java-index,Desktop,Core GUI APIs...
# 2

Sorry...little new to this, here's code that affects what the problem:

public class addColumn()

{

//the following creates a table to record the invetory number,

//Rental Item, when it's returned, number of days out, price and total price

JPanel item = new JPanel();

JPanel itemInfo = new JPanel();

String[] header = {"Inventory #", "Rental Item", "Returned (Emp Intl.)","# of Days", "Price"};

DefaultTableModel model = new DefaultTableModel(header, 4);

JTable rent = new JTable(model);

JScrollPane verticle = new JScrollPane(rent,20,30);

verticle.setPreferredSize(new Dimension(1050, 150));

itemInfo.add(verticle);

addCells = new JButton("Add More Items"); //adds cells to table

addCells.addActionListener(this);

item.add(itemInfo);

item.add(addCells);

getContentPane().add(item);

public void actionPerformed(ActionEvent e)

if(e.getActionCommand().equals("Calculate Total"))

{

System.out.println(model.getRowCount());

System.out.println(model.getColumnCount());

float rentFee = 0;

float taxPercent = taxRate/100;

System.out.println(model.getValueAt(3,4) );

for(int i = 0; i < model.getRowCount(); i++)

{

Object value = model.getValueAt(i, 4);

System.out.println(value+" "+i);

Float value2 = new Float(value.toString());

rentFee += value2.floatValue();

System.out.println(rentFee);

}

rentalFee.setText("Rental Fee: " + rentFee);

float taxAmt = rentFee * taxPercent;

total = rentFee + taxAmt;

totalLabel.setText("Total: $" + total);

}

}

Hope that helps

Thanks a lot!

Justin

Krus3ra at 2007-7-15 5:23:06 > top of Java-index,Desktop,Core GUI APIs...
# 3

ok...so i found out, if don't press enter, the last element entered into the table is thought to be null and you get a NullPointerExecption when you press the button, but if you press enter after it, the last element is seen as it's assigned value...is there a way to make it seen w/o pressing enter?

hope that makes sense, again, thanks for your help.

Justin

Krus3ra at 2007-7-15 5:23:06 > top of Java-index,Desktop,Core GUI APIs...
# 4

I trying to help but I am missing a lot of the details of the problem.

When posting code please use code formatting tags.

Also, it would be easier if you could provide a Short,Self Contained, Compilable and Executable, Example Program that anyone else can run. You did post some code but it is not something that I can compile or run to see your problem. (A picture paints a thousand words).

Where are you pressing enter? In the table? An example program would be nice.

zadoka at 2007-7-15 5:23:06 > top of Java-index,Desktop,Core GUI APIs...
# 5
I'm pressing enter in the table after entering an elementthanks
Krus3ra at 2007-7-15 5:23:06 > top of Java-index,Desktop,Core GUI APIs...