GUI Array display one item

I need to be able to display one item at a time of an array into JTextFields. The way I'm trying to do it here gives me this error for each line where I try to add an array item's method to the display. Error: int cannot be dereferenced.

import java.util.*;

import java.awt.*;

import javax.swing.*;

import javax.swing.event.*;

import java.text.*;

import java.awt.event.*;

import java.lang.*;

publicclass Inventoryextends JFrame// Main class

{

private JLabel prodNameLabel;// name label

private JLabel numberLabel;// item number label

private JLabel unitLabel;// units in stock label

private JLabel priceLabel;// price each label

private JLabel featureLabel;// added faeture label

private JLabel valueLabel;// value of that item label

private JLabel rstkLabel;// cost to restock label

private JLabel totalLabel;// total value of inventory label

private JTextField prodNameField;// name display

private JFormattedTextField numberField;// item number display

private JFormattedTextField unitField;// units in stock display

private JFormattedTextField priceField;// price each display

private JTextField featureField;// added feature display

private JFormattedTextField valueField;// value of that item display

private JFormattedTextField rstkField;// cost to restock display

private NumberFormat numberFormat;// format field and parse numbers

private NumberFormat unitFormat;// format field and parse numbers

private NumberFormat priceFormat;// format field and parse numbers

private NumberFormat valueFormat;// format field and parse numbers

private NumberFormat rstkFormat;// format field and parse numbers

private JButton firstBtn;// first button

private JButton prevBtn;// previous button

private JButton nextBtn;// next button

private JButton lastBtn;// last button

private JPanel buttonJPanel;// JPanle to hold buttons

private JPanel fieldJPanel;// JPanel to hold labels and displays

private JPanel fontJPanel;// JPanel to display logo

privateint currProd;// current product display to use for button action

privateint i;// iterator

privatedouble total = 0;// variable for total inventory

public Inventory()// create class and method to perform GUI build

{

initComponents();

}

privatevoid initComponents()

{

// create label names

prodNameLabel =new JLabel("Product Name:");

numberLabel =new JLabel("Item Number:");

unitLabel =new JLabel("In Stock:");

priceLabel =new JLabel("Each Item Cost:");

featureLabel =new JLabel("Type of Item:");

valueLabel =new JLabel("Value of Item Inventory:");

rstkLabel =new JLabel("Cost to Re-Stock Item:");

totalLabel =new JLabel("Total Value of Inventory:$"+total);

// create button names

firstBtn =new JButton("First");

prevBtn =new JButton("Previous");

nextBtn =new JButton("Next");

lastBtn =new JButton("Last");

// create textFields and set uneditable

prodNameField =new JTextField(15);

prodNameField.setEditable(false);

numberField =new JFormattedTextField(numberFormat);

numberField.setEditable(false);

numberField.setColumns(10);

unitField =new JFormattedTextField(unitFormat);

unitField.setEditable(false);

unitField.setColumns(10);

priceField =new JFormattedTextField(priceFormat);

priceField.setEditable(false);

priceField.setColumns(10);

featureField =new JTextField(15);

featureField.setEditable(false);

valueField =new JFormattedTextField(valueFormat);

valueField.setEditable(false);

valueField.setColumns(10);

rstkField =new JFormattedTextField(rstkFormat);

rstkField.setEditable(false);

rstkField.setColumns(10);

// add buttons to panel

JPanel buttons =new JPanel();// set up panel

buttons.setLayout(new GridLayout(1, 4));//set layout

// add buttons to buttons

buttons.add(firstBtn);

buttons.add(prevBtn);

buttons.add(nextBtn);

buttons.add(lastBtn);

// Instantiate a product object

ProductAdd[] nwProduct;

nwProduct =new ProductAdd[5];

// Instantiate objects for the array

for (currProd = 0; currProd < 5; currProd++)

{

nwProduct[0] =new ProductAdd("Paper", 101, 10, 1.00,"Box");

nwProduct[1] =new ProductAdd("Pen", 102, 10, 0.75,"Pack");

nwProduct[2] =new ProductAdd("Pencil", 103, 10, 0.50,"Pack");

nwProduct[3] =new ProductAdd("Staples", 104, 10, 1.00,"Box");

nwProduct[4] =new ProductAdd("Clip Board", 105, 10, 3.00,"Two Pack");

}

for (currProd = 0; currProd < 5; currProd++)

total += nwProduct[currProd].calcValue();// calculate total inventory cost

// add values to textFields

prodNameField.setText(currProd.getProdName());

numberField.setValue(new Integer(currProd.getItmNumber()));

unitField.setValue(new Integer(currProd.getUnits()));

priceField.setValue("$"+new Double(currProd.getPrice()));

featureField.setText(currProd.getFeature());

valueField.setValue("$"+new Double(currProd.calcValue()));

rstkField.setValue("$"+new Double(currProd.calcValueRstk()));

// add labels to panel

JPanel labels =new JPanel();

labels.setLayout(new GridLayout(0, 1));

labels.add(prodNameLabel);

labels.add(numberLabel);

labels.add(unitLabel);

labels.add(priceLabel);

labels.add(featureLabel);

labels.add(valueLabel);

labels.add(rstkLabel);

labels.add(totalLabel);

// add fields to panel

JPanel fields =new JPanel();

fields.setLayout(new GridLayout(0, 1));

fields.add(prodNameField);

fields.add(numberField);

fields.add(unitField);

fields.add(priceField);

fields.add(featureField);

fields.add(valueField);

fields.add(rstkField);

//pair label and fields

prodNameLabel.setLabelFor(prodNameField);

numberLabel.setLabelFor(numberField);

unitLabel.setLabelFor(unitField);

priceLabel.setLabelFor(priceField);

featureLabel.setLabelFor(featureField);

valueLabel.setLabelFor(valueField);

rstkLabel.setLabelFor(rstkField);

// add labels and fields to panel

// labels left, fields right

JPanel display =new JPanel();

display.add(labels, BorderLayout.CENTER);

display.add(fields, BorderLayout.LINE_END);

// create display frame

JFrame invFrame =new JFrame();

invFrame.setLayout(new BorderLayout());

invFrame.setTitle("Office Min Inventory");

invFrame.add(fontJPanel, BorderLayout.NORTH);

invFrame.add(buttons, BorderLayout.SOUTH);

invFrame.add(display, BorderLayout.CENTER);

invFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// termination command

invFrame.setSize(800, 800);// set size of JPanel

invFrame.setLocationRelativeTo(null);// set screem location

invFrame.setVisible(true);// display window

firstBtn.addActionListener(new ActionListener()// first button listener

{

publicvoid actionPerformed(ActionEvent evt)

{

goFirst();

}

});

lastBtn.addActionListener(new ActionListener()// last button listener

{

publicvoid actionPerformed(ActionEvent evt)

{

goLast();

}

});

prevBtn.addActionListener(new ActionListener()// previous button listener

{

publicvoid actionPerformed(ActionEvent evt)

{

goBack();

}

});

nextBtn.addActionListener(new ActionListener()// next button listener

{

publicvoid actionPerformed(ActionEvent evt)

{

goNext();

}

});

}

privatevoid goFirst()// first button action method

{

currProd = 0;

prodNameField.setText(currProd.getProdName());

numberField.setValue(new Integer(currProd.getItmNumber()));

unitField.setValue(new Integer(currProd.getUnits()));

priceField.setValue("$"+new Double(currProd.getPrice()));

featureField.setText(currProd.getFeature());

valueField.setValue("$"+new Double(currProd.calcValue()));

rstkField.setValue("$"+new Double(currProd.calcValueRstk()));

}

privatevoid goLast()// last button action method

{

currProd = 4;

prodNameField.setText(currProd.getProdName());

numberField.setValue(new Integer(currProd.getItmNumber()));

unitField.setValue(new Integer(currProd.getUnits()));

priceField.setValue("$"+new Double(currProd.getPrice()));

featureField.setText(currProd.getFeature());

valueField.setValue("$"+new Double(currProd.calcValue()));

rstkField.setValue("$"+new Double(currProd.calcValueRstk()));

}

privatevoid goBack()// previous button action method

{

if(currProd > 0)

{

currProd--;

prodNameField.setText(currProd.getProdName());

numberField.setValue(new Integer(currProd.getItmNumber()));

unitField.setValue(new Integer(currProd.getUnits()));

priceField.setValue("$"+new Double(currProd.getPrice()));

featureField.setText(currProd.getFeature());

valueField.setValue("$"+new Double(currProd.calcValue()));

rstkField.setValue("$"+new Double(currProd.calcValueRstk()));

}

}

privatevoid goNext()// next button action method

{

if(currProd < 4)

{

currProd++;

prodNameField.setText(currProd.getProdName());

numberField.setValue(new Integer(currProd.getItmNumber()));

unitField.setValue(new Integer(currProd.getUnits()));

priceField.setValue("$"+new Double(currProd.getPrice()));

featureField.setText(currProd.getFeature());

valueField.setValue("$"+new Double(currProd.calcValue()));

rstkField.setValue("$"+new Double(currProd.calcValueRstk()));

}

}

//main method begins execution of java application

publicstaticvoid main(String args[])

{

Inventory inventory =new Inventory();

}// end main method

}// end class Inventory

class Product

{

protected String prodName;// name of product

protectedint itmNumber;// item number

protectedint units;// number of units

protecteddouble price;// price of each unit

protecteddouble value;// value of total units

public Product(String name,int number,int unit,double each)// Constructor for class Product

{

prodName = name;

itmNumber = number;

units = unit;

price = each;

}// end constructor

publicvoid setProdName(String name)// method to set product name

{

prodName = name;

}

public String getProdName()// method to get product name

{

return prodName;

}

publicvoid setItmNumber(int number)// method to set item number

{

itmNumber = number;

}

publicint getItmNumber()// method to get item number

{

return itmNumber;

}

publicvoid setUnits(int unit)// method to set number of units

{

units = unit;

}

publicint getUnits()// method to get number of units

{

return units;

}

publicvoid setPrice(double each)// method to set price

{

price = each;

}

publicdouble getPrice()// method to get price

{

return price;

}

publicdouble calcValue()// method to set value

{

return units * price;

}

}// end class Product

class ProductAddextends Product

{

private String feature;// variable for added feature

public ProductAdd(String name,int number,int unit,double each, String addFeat)

{

// call to superclass Product constructor

super(name, number, unit, each);

feature = addFeat;

}// end constructor

publicvoid setFeature(String addFeat)// method to set added feature

{

feature = addFeat;

}

public String getFeature()// method to get added feature

{

return feature;

}

publicdouble calcValueRstk()// method to set value and add restock fee

{

return units * price * 0.05;

}

}// end class ProductAdd

class FontJPanelextends JPanel

{

// display welcome message

publicvoid paintComponent( Graphics g )

{

super.paintComponent( g );//call superclass's paintComponent

// set font to Monospaced (Courier), italic, 12pt and draw a string

g.setFont(new Font("Monospaced", Font.ITALIC, 12 ) );

g.drawString("min", 90, 70 );

// set font to Serif (Times), bold, 24pt and draw a string

g.setColor( Color.RED );

g.setFont(new Font("Serif", Font.BOLD, 24 ) );

g.drawString("OFFICE", 60, 60 );

}// end method paintComponent

}// end class FontJPanel

[25879 byte] By [itchibana] at [2007-11-27 9:56:50]
# 1
Can you post a full stack trace along with the method that is giving the error?
CaptainMorgan08a at 2007-7-13 0:27:01 > top of Java-index,Java Essentials,New To Java...
# 2

itchiban, you have a lot of code, and while you say that there is an error, you don't show us where it is. A quick word of advice. Your chances of getting the help you need will increase by an order of magnitude if you make it as easy as possible for someone to help you. Also truthfully, it is somewhat disrespectful to make volunteers do extra work that they don't have to be doing in order to help you.

Until you provide more information, I for one am not going to be able to help you or even desire to help you.

Good luck.

petes1234a at 2007-7-13 0:27:01 > top of Java-index,Java Essentials,New To Java...
# 3

> itchiban, you have a lot of code, and while you say

> that there is an error, you don't show us where it

> is. A quick word of advice. Your chances of getting

> the help you need will increase by an order of

> magnitude if you make it as easy as possible for

> someone to help you. Also truthfully, it is somewhat

> disrespectful to make volunteers do extra work that

> they don't have to be doing in order to help you.

>

> Until you provide more information, I for one am not

> going to be able to help you or even desire to help

> you.

>

> Good luck.

My apologies I am only a few days old in this forum. I'm not even sure if this is being more specific. I am trying to display a single object of my array into 7 element textFields. Basically I am trying to call my class constructors for ProductAdd one array item at a time and display each getMethod into it's own textField. I tried to add ArrayList and use it to build my array but my instructor said that goes beyond out text book. Do I need to build a seperate method to add my objects into the array a different way?

itchibana at 2007-7-13 0:27:01 > top of Java-index,Java Essentials,New To Java...
# 4
Thank you for your reply, but where is your error? what line is it on? and what is the specific error message?
petes1234a at 2007-7-13 0:27:01 > top of Java-index,Java Essentials,New To Java...
# 5

I have copied your code and compiled it. An exerpt with code tags is shown below:

public class Inventory extends JFrame // Main class

{

.....................

private int currProd; // current product display to use for button action

private void initComponents()

{

.............................

for (currProd = 0; currProd < 5; currProd++)

{

nwProduct[0] = new ProductAdd("Paper", 101, 10, 1.00, "Box");

nwProduct[1] = new ProductAdd("Pen", 102, 10, 0.75, "Pack");

nwProduct[2] = new ProductAdd("Pencil", 103, 10, 0.50, "Pack");

nwProduct[3] = new ProductAdd("Staples", 104, 10, 1.00, "Box");

nwProduct[4] = new ProductAdd("Clip Board", 105, 10, 3.00,

"Two Pack");

}

for (currProd = 0; currProd < 5; currProd++)

total += nwProduct[currProd].calcValue(); // calculate total

// inventory cost

// add values to textFields

prodNameField.setText(currProd.getProdName());

numberField.setValue(new Integer(currProd.getItmNumber()));

unitField.setValue(new Integer(currProd.getUnits()));

priceField.setValue("$" + new Double(currProd.getPrice()));

featureField.setText(currProd.getFeature());

valueField.setValue("$" + new Double(currProd.calcValue()));

rstkField.setValue("$" + new Double(currProd.calcValueRstk()));

Some problems on first glance

1) currProd is an int instance variable. I highly recommend that you don't use these as loop indices. Unless there are strong reasons not to, define a fresh new int var for each loop index. Otherwise you are asking for some serious unexpected side-effects.

2) At the bottom here, you are treating currProd, an int, as if it were an object, which it is not. Just putting in a comment stating that it is the current product will not make it so. Find out the object that you really want here, and use it instead.

3) Even if you do find out the correct object to use in this place in the code, your logic is still seriously broken. I suggest that you break down your big problem into smaller problems and solve each one separately, and then combine to form your code. A total re-write would help you tremendously.

4) Solve your problem without the GUI code first. Then do the GUI. For instance, you could put your product array in its own class together with an Product variable that represents the Product array item that is currently "active". Then you could create the methods getFirst, getLast, getNext, and getPrev, etc... Then when tested in a non-GUI way, add in a GUI.

Good luck.

Message was edited by:

petes1234

petes1234a at 2007-7-13 0:27:01 > top of Java-index,Java Essentials,New To Java...
# 6

Look closely at this loop:

for (currProd = 0; currProd < 5; currProd++)

{

nwProduct[0] = new ProductAdd("Paper", 101, 10, 1.00, "Box");

nwProduct[1] = new ProductAdd("Pen", 102, 10, 0.75, "Pack");

nwProduct[2] = new ProductAdd("Pencil", 103, 10, 0.50, "Pack");

nwProduct[3] = new ProductAdd("Staples", 104, 10, 1.00, "Box");

nwProduct[4] = new ProductAdd("Clip Board", 105, 10, 3.00, "Two Pack");

}

What is it doing? How many times is it doing it? If you don't understand, walk through it as if you were the compiler saying to yourself what it is doing at each step.

petes1234a at 2007-7-13 0:27:01 > top of Java-index,Java Essentials,New To Java...