Hello,
I'm currently working on the dreaded Inventory Program. I'm on part four and just learning GUIs. I need to modify the Inventory Program to use a GUI. In the GUI, it should display the information in my array one line at a time as well as the value of my entire inventory, additional feature and restocking fee. I was able to create the GUI, but I can't populate it with my array items. Any tips on getting my array information to display on the GUI would be wonderful! I am able to compile the file, but get an empty GUI.
Any assistance is greatly appreciated!!!!!
Here is my array code and the dispaly call to the GUI in my Main class: - // constructor
-
public Inventory_Program4()
-
{
-
double totalInventoryValue = 0.0;
-
-
// Specify how many items are in the array
-
// instantiate Book2 object
-
myBook = new Book2[5];
-
myBook[0] = new Book2("The Key Triology", 1, 25, 7.99, "Nora Roberts");
-
myBook[1] = new Book2("The Shinning", 2, 15, 5.99, "Stephen King");
-
myBook[2] = new Book2("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
-
myBook[3] = new Book2("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
-
myBook[4] = new Book2("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
-
-
// call method sort book inventory for display
-
sortBookInventory();
-
-
for (int i = 0; i<5; i++)
-
{
-
-
// display the book title
-
System.out.println("The book title is: " + myBook[i].getBookTitle());
-
-
// display the item number
-
System.out.println("The item number is: " + myBook[i].getItemNumber());
-
-
// display the number of units in stock
-
System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());
-
-
// display the price per book
-
System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());
-
-
// display the value of the inventory
-
System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());
-
-
// display the book author
-
System.out.println("The book author is: " + myBook[i].getBookAuthor());
-
-
// display the restocking fee
-
System.out.println("Restock Fee: " + myBook[i].inventoryValue() * 0.05);
-
-
// calculate total inventory value with restocking fee added
-
totalInventoryValue += (myBook[i].inventoryValue() * 1.05);
-
-
// insert blank line
-
System.out.println();
-
-
-
} // end for
-
-
// display the total value of the inventory with the restocking fee
-
System.out.printf("The total value of the book inventory including the restocking fee is: $%5.2f.\n", totalInventoryValue);
-
-
// display the total value of the inventory excluding the restocking fee
-
System.out.println("The total value of the book inventory is: " + totalInventoryValue());
-
-
// calling GUI to display contents
-
Labels labels = new Labels(); // create Labels
-
labels.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
-
labels.setSize(275, 180); // set frame size
-
labels.setVisible( true ); // display frame
-
-
} // end constructor
Here is my the code to my Labels class where I defined all the JLabels for the GUI: - import java.awt.FlowLayout;
-
import javax.swing.JFrame;
-
import javax.swing.JLabel;
-
import javax.swing.SwingConstants;
-
import javax.swing.Icon;
-
import javax.swing.ImageIcon;
-
-
public class Labels extends JFrame
-
{
-
-
private JLabel bookTitleLabel;
-
private JLabel itemNumberLabel;
-
private JLabel bookUnitsLabel;
-
private JLabel bookPriceLabel;
-
private JLabel inventoryValueLabel;
-
private JLabel bookAuthorLabel;
-
private JLabel bookRestockingFeeLabel;
-
private JLabel totalInventoryValueLabel;
-
-
// Labels constructor adds JLabels to JFrame
-
public Labels()
-
{
-
super( "Inventory Program" );
-
setLayout( new FlowLayout() ); // set frame layout
-
-
// JLabel constructor
-
bookTitleLabel = new JLabel("Book Title:");
-
bookTitleLabel.setToolTipText("Book Title Label");
-
add( bookTitleLabel ); // add bookTitleLabel to JFrame
-
-
itemNumberLabel = new JLabel("Item Number:");
-
itemNumberLabel.setToolTipText("Item Number Label");
-
add( itemNumberLabel ); // add itemNumberLabel to JFrame
-
-
bookUnitsLabel = new JLabel("Book Units:");
-
bookUnitsLabel.setToolTipText("Book Units Label");
-
add( bookUnitsLabel ); // add bookUnitsLabel to JFrame
-
-
bookPriceLabel = new JLabel("Book Price:");
-
bookPriceLabel.setToolTipText("Book Price Label");
-
add( bookPriceLabel ); // add bookPriceLabel to JFrame
-
-
inventoryValueLabel = new JLabel("Book Inventory Value:");
-
inventoryValueLabel.setToolTipText("Inventory Value Label");
-
add ( inventoryValueLabel ); // add inventoryValueLabel to JFrame
-
-
bookAuthorLabel = new JLabel("Book Author:");
-
bookAuthorLabel.setToolTipText("Book Author Label");
-
add( bookAuthorLabel ); // add bookAuthorLabel to JFrame
-
-
bookRestockingFeeLabel = new JLabel("Restocking Fee:");
-
bookRestockingFeeLabel.setToolTipText("Restocking Fee Label");
-
add( bookRestockingFeeLabel ); // add bookRestockingFeeLabel to JFrame
-
-
totalInventoryValueLabel = new JLabel("Total Inventory Value:");
-
totalInventoryValueLabel.setToolTipText("Total Inventory Label");
-
add( totalInventoryValueLabel ); // add totalInventoryValueLabel to JFrame
-
} // end Label constructor
-
} // end class Labels
7 1750
Hello,
I'm currently working on the dreaded Inventory Program. I'm on part four and just learning GUIs. I need to modify the Inventory Program to use a GUI. In the GUI, it should display the information in my array one line at a time as well as the value of my entire inventory, additional feature and restocking fee. I was able to create the GUI, but I can't populate it with my array items. Any tips on getting my array information to display on the GUI would be wonderful! I am able to compile the file, but get an empty GUI.
Any assistance is greatly appreciated!!!!!
Here is my array code and the dispaly call to the GUI in my Main class:
...
Hi!
I would add a setItem(...) method to the GUI, which set's the contents of those JLabels. It can get it's information from your Inventory and write it into the Labels with setText("Blabla: " + valueToBeSet).
Otherwise, you'll just have to keep on trying.
Greetings,
Nepomuk
Thank you! I will try that and let you know!
I've been playing around with this for hours and I'm still stuck. Can you post an example of how to add a setItem method to the GUI?
Thanks again!
I've been playing around with this for hours and I'm still stuck. Can you post an example of how to add a setItem method to the GUI?
Thanks again!
Well then what code do you have now and where are you stuck with it?
I've been playing around with this for hours and I'm still stuck. Can you post an example of how to add a setItem method to the GUI?
Thanks again!
Please don't beg for code; if you read the API documentatio you'd find lots and
lots of code examples and explanations. Try to think in detail about the problem
you're supposed to be working on. It's the only way to slaughter the problem and
master the Java language.
kind regards,
Jos
Here is my current code. I am trying to get my array items to display in the GUI. The file compiles, but I get the following error message when the GUI tries to lauch.
Exception in thread "main" java.lang.NullPointerException
at Labels.<init>(Labels.java:36)
at Inventory_Program4.<init>(Inventory_Program4.java: 98)
at Inventory_Program4.main(Inventory_Program4.java:12 1)
I know that the following line is giving me the grief (bookTitleLabel.setText("The Book Title is: " + myBook[i].getBookTitle());). I'm trying to display the array item, but not doing it correctly.
I am so lost on how to incorporate my array information into the GUI. I apologize if I sound like I'm begging for code, that was not my intention. Thanks for all of your help!
The following code is where I created my GUI: - import java.awt.FlowLayout;
-
import javax.swing.JFrame;
-
import javax.swing.JLabel;
-
import javax.swing.SwingConstants;
-
import javax.swing.Icon;
-
import javax.swing.ImageIcon;
-
-
public class Labels extends JFrame
-
{
-
-
private Book2 myBook[];
-
-
-
private JLabel bookTitleLabel;
-
private JLabel itemNumberLabel;
-
private JLabel bookUnitsLabel;
-
private JLabel bookPriceLabel;
-
private JLabel inventoryValueLabel;
-
private JLabel bookAuthorLabel;
-
private JLabel bookRestockingFeeLabel;
-
private JLabel totalInventoryValueLabel;
-
-
// Labels constructor adds JLabels to JFrame
-
public Labels()
-
{
-
super( "Inventory Program" );
-
setLayout( new FlowLayout() ); // set frame layout
-
-
-
-
for (int i = 0; i<5; i++)
-
{
-
-
// JLabel constructor
-
bookTitleLabel = new JLabel();
-
bookTitleLabel.setText("The Book Title is: " + myBook[i].getBookTitle());
-
bookTitleLabel.setToolTipText("Book Title Label");
-
add( bookTitleLabel ); // add bookTitleLabel to JFrame
-
-
itemNumberLabel = new JLabel("Item Number:");
-
itemNumberLabel.setToolTipText("Item Number Label");
-
add( itemNumberLabel ); // add itemNumberLabel to JFrame
-
-
bookUnitsLabel = new JLabel("Book Units:");
-
bookUnitsLabel.setToolTipText("Book Units Label");
-
add( bookUnitsLabel ); // add bookUnitsLabel to JFrame
-
-
bookPriceLabel = new JLabel("Book Price:");
-
bookPriceLabel.setToolTipText("Book Price Label");
-
add( bookPriceLabel ); // add bookPriceLabel to JFrame
-
-
inventoryValueLabel = new JLabel("Book Inventory Value:");
-
inventoryValueLabel.setToolTipText("Inventory Value Label");
-
add ( inventoryValueLabel ); // add inventoryValueLabel to JFrame
-
-
bookAuthorLabel = new JLabel("Book Author:");
-
bookAuthorLabel.setToolTipText("Book Author Label");
-
add( bookAuthorLabel ); // add bookAuthorLabel to JFrame
-
-
bookRestockingFeeLabel = new JLabel("Restocking Fee:");
-
bookRestockingFeeLabel.setToolTipText("Restocking Fee Label");
-
add( bookRestockingFeeLabel ); // add bookRestockingFeeLabel to JFrame
-
-
totalInventoryValueLabel = new JLabel("Total Inventory Value:");
-
totalInventoryValueLabel.setToolTipText("Total Inventory Label");
-
add( totalInventoryValueLabel ); // add totalInventoryValueLabel to JFrame
-
}
-
} // end Label constructor
-
} // end class Labels
The following code is my array in my Main class: - // constructor
-
public Inventory_Program4()
-
{
-
double totalInventoryValue = 0.0;
-
-
// Specify how many items are in the array
-
// instantiate Book2 object
-
myBook = new Book2[5];
-
myBook[0] = new Book2("The Key Triology", 1, 25, 7.99, "Nora Roberts");
-
myBook[1] = new Book2("The Shinning", 2, 15, 5.99, "Stephen King");
-
myBook[2] = new Book2("Wild Acre", 3, 7, 4.99, "Phillipa Gregory");
-
myBook[3] = new Book2("Dark Paradise", 4, 2, 12.99, "Tami Hoag");
-
myBook[4] = new Book2("Dollhouse Murders", 5, 18, 2.95, "Betty Ren Wright");
-
-
// call method sort book inventory for display
-
sortBookInventory();
-
-
for (int i = 0; i<5; i++)
-
{
-
-
// display the book title
-
System.out.println("The book title is: " + myBook[i].getBookTitle());
-
-
// display the item number
-
System.out.println("The item number is: " + myBook[i].getItemNumber());
-
-
// display the number of units in stock
-
System.out.println("The number of units in stock is: " + myBook[i].getBookUnits());
-
-
// display the price per book
-
System.out.printf("The price of the book is: $%.2f\n", myBook[i].getBookPrice());
-
-
// display the value of the inventory
-
System.out.printf("The value of the inventory is: $%.2f\n", myBook[i].inventoryValue());
-
-
// display the book author
-
System.out.println("The book author is: " + myBook[i].getBookAuthor());
-
-
// display the restocking fee
-
System.out.println("Restock Fee: " + myBook[i].inventoryValue() * 0.05);
-
-
// calculate total inventory value with restocking fee added
-
totalInventoryValue += (myBook[i].inventoryValue() * 1.05);
-
-
// insert blank line
-
System.out.println();
-
-
-
} // end for
-
-
// display the total value of the inventory with the restocking fee
-
System.out.printf("The total value of the book inventory including the restocking fee is: $%5.2f.\n", totalInventoryValue);
-
-
// display the total value of the inventory excluding the restocking fee
-
System.out.println("The total value of the book inventory is: " + totalInventoryValue());
-
-
// calling GUI to display contents
-
Labels labels = new Labels(); // create Labels
-
labels.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
-
labels.setSize(275, 180); // set frame size
-
labels.setVisible( true ); // display frame
-
-
} // end constructor
private Book2 myBook[];
Does not create an array at all.
myBook at this point is null. So when the code reaches that point in that Labels constructor and tries to do myBook[i].getWhatever it tells you that you are derefencing a null variable. The exception trace does tell you what is wrong as you can see.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David |
last post by:
On every web browser except Safari, this website works great. (Well,
by "every" I mean Mozilla, Netscape, and Internet Explorer, for Mac
and Windows).
The site is: http://www.ruleofthirds.com
...
|
by: Ryan Ternier |
last post by:
I'm having an issue with an SQL insert statement. It's a very simple
statement, and it's causing too much fuss.
strSQL = "INSERT INTO tblFieldLayouts(TypeID, FieldID, OrderID, Hidden)
VALUES("...
|
by: laurenq uantrell |
last post by:
I'm trying to return an integer from the following table that returns
the number of unique cities:
tblEmployees
Name City
John Boston
Frank New York
Jim Omaha
Betty ...
|
by: NuB |
last post by:
The validation controls are giving me a headache. here is what i'm trying to
do and so far what I've tried has not worked.
I need to hide my grid if the page is not valid
how can i accomplish...
|
by: James Ma |
last post by:
Now I am debugging a buggy VB.NET function like following
Function foo()
Try
‘ many loops and codes and nested try catch here!
……
Catch (ex as Exception)
Msgbox ex.message
Finally
…
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |