Help Finishing Inventory Part 6

Reply

Join Date: May 2008
Posts: 34
Reputation: twgood is an unknown quantity at this point 
Solved Threads: 0
twgood twgood is offline Offline
Light Poster

Help Finishing Inventory Part 6

 
0
  #1
Aug 13th, 2008
So far it looks pretty good, except in the GUI I can not get the Modify, Save, Delete, Add and Search buttons to work. They produce error codes :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javaapplication54.ModifyButtonHandler.actionPerformed(Inventory6.java:488)
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:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

  1. package javaapplication54;
  2.  
  3. import java.awt.*; // import the java awt package
  4. import java.awt.event.*; // Import the java event package
  5. import javax.swing.*; // Import the java swing package
  6. import java.util.*; // Import the java utility package
  7. import java.io.*; // Import the java input output package
  8. import java.awt.Image;
  9. import javax.swing.ImageIcon;
  10.  
  11.  
  12.  
  13.  
  14.  
  15. public class Inventory6 {
  16.  
  17. // main method begins
  18. public static void main(String[] args) {
  19.  
  20.  
  21.  
  22. Movie dvd = null;
  23. Inventory inventory = new Inventory();
  24.  
  25. dvd = new Movie(2356, "Legally Blonde", 25, 14.99f, "G");
  26. System.out.println(dvd);
  27. inventory.addMovie(dvd);
  28.  
  29. dvd = new Movie(5684, "National Tressure", 3, 12.00f, "G");
  30. System.out.println(dvd);
  31. inventory.addMovie(dvd);
  32.  
  33. dvd = new Movie(5564, "Sweet Home Alabama", 15, 15.75f, "G");
  34. System.out.println(dvd);
  35. inventory.addMovie(dvd);
  36.  
  37. dvd = new Movie(5562, "Shrek", 7, 9.99f, "G");
  38. inventory.addMovie(dvd);
  39. System.out.println(dvd);
  40.  
  41.  
  42.  
  43.  
  44. inventory.printInventory();
  45. new InventoryGUI(inventory);
  46.  
  47. } // end main
  48.  
  49. } // end Inventory6
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. // Extends DVD class from the base class DVD
  57. class DVD {
  58. private int itemNo;
  59. private String title;
  60. private int inStock;
  61. private float unitPrice;
  62.  
  63. DVD() {
  64.  
  65. }
  66.  
  67. DVD(int itemNo, String title, int inStock, float unitPrice) {
  68. this.itemNo = itemNo;
  69. this.title = title;
  70. this.inStock = inStock;
  71. this.unitPrice = unitPrice;
  72.  
  73. }
  74.  
  75. public int getItemNo() { return itemNo; }
  76. public String getTitle() { return title; }
  77. public int getInStock() { return inStock; }
  78. public float getUnitPrice() { return unitPrice; }
  79.  
  80. public float value() {
  81. return inStock * unitPrice;
  82. }
  83.  
  84. @Override
  85. public String toString() {
  86. return String.format("itemNo=%2d title=%-22s inStock=%3d price=$%7.2f value=$%8.2f",
  87. itemNo, title, inStock, unitPrice, value());
  88. }
  89.  
  90. void addDVD() {
  91.  
  92. }
  93.  
  94. void closeFile() {
  95.  
  96. }
  97.  
  98. void openFile() {
  99.  
  100. }
  101.  
  102. } // end DVD
  103. class Inventory {
  104. private static int itemNo;
  105.  
  106. static int getitemNo() {
  107. return itemNo;
  108.  
  109. }
  110. // Setup an array of Movies
  111. private final int INVENTORY_SIZE = 100;
  112. private DVD[] items;
  113. private int numItems;
  114.  
  115. Inventory() {
  116. items = new Movie[INVENTORY_SIZE];
  117. numItems = 0;
  118. }
  119.  
  120. public int getNumItems() {
  121. return numItems;
  122. }
  123.  
  124. public DVD getDVD(int n) {
  125. return items[n];
  126. }
  127.  
  128. // Adds a Movie to the array of Movies. Adds to first empty slot found.
  129. public void addMovie(DVD item) {
  130. items[numItems] = item;
  131. ++numItems;
  132. }
  133.  
  134.  
  135. public double value() {
  136. double sumOfInventory = 0.0;
  137.  
  138. for (int i = 0; i < numItems; i++)
  139. sumOfInventory += items[i].value();
  140.  
  141. return sumOfInventory;
  142. }
  143.  
  144.  
  145. public void printInventory() {
  146. System.out.println("\n DVD Inventory\n");
  147.  
  148.  
  149. if (numItems <= 0) {
  150. } else {
  151. for (int i = 0; i < numItems; i++)
  152. System.out.printf("%3d %s\n", i, items[i]);
  153. System.out.printf("\nTotal value is $%,.2f\n\n", value());
  154. }
  155. }
  156.  
  157. } // end Inventory
  158. class Movie extends DVD {
  159. // Holds movie Rating and adds restocking fee
  160. private String movieRating;
  161.  
  162. // Constructor, calls the constructor of Movie first
  163. public Movie(int MovieID, String itemName, int units, float itemPrice, String Rating) {
  164. super(MovieID, itemName, units, itemPrice);
  165. // Pass on the values needed for creating the Movie class first thing
  166. this.movieRating = Rating;
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173. // To set the rating manually
  174. public void setRating(String Rating) {
  175. movieRating = Rating;
  176. }
  177.  
  178. // Get the rating
  179. public String getRating() {
  180. return movieRating;
  181. }
  182.  
  183. // Overrides value() in Movie class by calling the base class version and
  184. // adding a 5% restocking fee on top
  185. @Override
  186. public float value() {
  187. return super.value() + restockingFee();
  188. }
  189.  
  190. // Simply gets the base class's value, and figures out the 5% restocking fee only
  191. public float restockingFee() {
  192. return super.value() * 0.05f;
  193. }
  194.  
  195. } // end Movie
  196. class ArcsJPanel extends JPanel
  197. {
  198.  
  199. @Override
  200. public void paintComponent( Graphics g )
  201. {
  202. super.paintComponent( g ); // call superclass's paintComponent
  203.  
  204. // draws larger 3d rectangle
  205. g.setColor( Color.RED );
  206. g.draw3DRect( 45, 15, 180, 40, true);
  207.  
  208. // draws small black square
  209. g.setColor( Color.BLACK );
  210. g.draw3DRect( 5, 15, 40, 40, true );
  211. g.fill3DRect( 5, 15, 40, 40, false );
  212.  
  213. //draws circle
  214. g.setColor( Color.RED );
  215. g.drawArc( 5, 15, 40, 40, 10, 360 );
  216.  
  217. //fills circle
  218. g.setColor( Color.WHITE);
  219. g.fillArc( 5, 15, 40, 40, 10, 360 );
  220.  
  221. // draws company name
  222. g.setColor( Color.BLACK );
  223. g.setFont( new Font( "Serif", Font.BOLD, 25 ) );
  224. g.drawString( "Estey's DVD Shack.", 10, 45 );
  225.  
  226. } // end method paintComponent
  227.  
  228. } // end class ArcsJPanel
  229.  
  230.  
  231. // GUI for the Inventory
  232. // Contains an inventory of DVD's and lets the user step through them one by one
  233. class InventoryGUI extends JFrame
  234. {
  235. // access inventory for DVD Collection
  236. private Inventory theInventory;
  237.  
  238. // index in the inventory of the currently displayed DVD.
  239. // the index starts at 0, goes to the number of DVDs in the inventory minus 1
  240. private int index = 0;
  241.  
  242. // GUI elements to display currently selected DVD information
  243. private final JLabel itemNumberLabel = new JLabel(" Item Number:");
  244. private JTextField itemNumberText;
  245.  
  246. private final JLabel prodnameLabel = new JLabel(" Product Name:");
  247. private JTextField prodnameText;
  248.  
  249. private final JLabel prodpriceLabel = new JLabel(" Price:");
  250. private JTextField prodpriceText;
  251.  
  252. private final JLabel numinstockLabel = new JLabel(" Number in Stock:");
  253. private JTextField numinstockText;
  254.  
  255. private final JLabel valueLabel = new JLabel(" Value:");
  256. private JTextField valueText;
  257.  
  258. private final JLabel movieRatingLabel = new JLabel(" Rated:");
  259. private JTextField movieRatingText;
  260.  
  261. private final JLabel totalValueLabel = new JLabel(" Inventory Total Value (5% restock fee included:");
  262. private JTextField totalValueText;
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272. private JPanel centerPanel;
  273. private JPanel buttonPanel;
  274.  
  275.  
  276.  
  277.  
  278.  
  279. // constructor for the GUI, in charge of creating all GUI elements
  280. InventoryGUI(Inventory inventory) {
  281. super("Movie Inventory");
  282. final Dimension dim = new Dimension(140, 20);
  283. final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
  284. JPanel jp;
  285.  
  286. // create the inventory object that will hold the product information
  287. theInventory = inventory;
  288.  
  289.  
  290. centerPanel = new JPanel();
  291. centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
  292.  
  293.  
  294.  
  295. buttonPanel = new JPanel();
  296. JButton firstButton = new JButton("First");
  297. firstButton.addActionListener((ActionListener) new firstButtonHandler());
  298. buttonPanel.add(firstButton);
  299.  
  300. JButton previousButton = new JButton("Previous");
  301. previousButton.addActionListener(new previousButtonHandler());
  302. buttonPanel.add(previousButton);
  303.  
  304. JButton nextButton = new JButton("Next");
  305. nextButton.addActionListener(new NextButtonHandler());
  306. buttonPanel.add(nextButton);
  307.  
  308. JButton lastButton = new JButton("Last");
  309. lastButton.addActionListener(new lastButtonHandler());
  310. buttonPanel.add(lastButton);
  311.  
  312. JButton addButton = new JButton("Add");
  313. addButton.addActionListener(new AddButtonHandler());
  314. buttonPanel.add(addButton);
  315.  
  316. JButton deleteButton = new JButton("Delete");
  317. deleteButton.addActionListener(new DeleteButtonHandler());
  318. buttonPanel.add(deleteButton);
  319.  
  320. JButton modifyButton = new JButton("Modify");
  321. modifyButton.addActionListener(new ModifyButtonHandler());
  322. buttonPanel.add(modifyButton);
  323.  
  324. JButton saveButton = new JButton("Save");
  325. saveButton.addActionListener(new SaveButtonHandler());
  326. buttonPanel.add(saveButton);
  327.  
  328. JButton searchButton = new JButton("Search");
  329. searchButton.addActionListener(new SearchButtonHandler());
  330. buttonPanel.add(searchButton);
  331.  
  332. centerPanel.add(buttonPanel);
  333.  
  334. jp = new JPanel(flo);
  335. itemNumberLabel.setPreferredSize(dim);
  336. jp.add(itemNumberLabel);
  337. itemNumberText = new JTextField(3);
  338. itemNumberText.setEditable(false);
  339. jp.add(itemNumberText);
  340. centerPanel.add(jp);
  341.  
  342. jp = new JPanel(flo);
  343. prodnameLabel.setPreferredSize(dim);
  344. jp.add(prodnameLabel);
  345. prodnameText = new JTextField(17);
  346. prodnameText.setEditable(false);
  347. jp.add(prodnameText);
  348. centerPanel.add(jp);
  349.  
  350. jp = new JPanel(flo);
  351. movieRatingLabel.setPreferredSize(dim);
  352. jp.add(movieRatingLabel);
  353. movieRatingText = new JTextField(17);
  354. movieRatingText.setEditable(false);
  355. jp.add(movieRatingText);
  356. centerPanel.add(jp);
  357.  
  358. jp = new JPanel(flo);
  359. prodpriceLabel.setPreferredSize(dim);
  360. jp.add(prodpriceLabel);
  361. prodpriceText = new JTextField(17);
  362. prodpriceText.setEditable(false);
  363. jp.add(prodpriceText);
  364. centerPanel.add(jp);
  365.  
  366. jp = new JPanel(flo);
  367. numinstockLabel.setPreferredSize(dim);
  368. jp.add(numinstockLabel);
  369. numinstockText = new JTextField(5);
  370. numinstockText.setEditable(false);
  371. jp.add(numinstockText);
  372. centerPanel.add(jp);
  373.  
  374.  
  375.  
  376. jp = new JPanel(flo);
  377. valueLabel.setPreferredSize(dim);
  378. jp.add(valueLabel);
  379. valueText = new JTextField(17);
  380. valueText.setEditable(false);
  381. jp.add(valueText);
  382. centerPanel.add(jp);
  383.  
  384. // add the overall inventory information to the panel
  385. jp = new JPanel(flo);
  386. totalValueLabel.setPreferredSize(dim);
  387. jp.add(totalValueLabel);
  388. totalValueText = new JTextField(17);
  389. totalValueText.setEditable(false);
  390. jp.add(totalValueText);
  391. centerPanel.add(jp);
  392.  
  393. // add the panel to the GUI display
  394. setContentPane(centerPanel);
  395.  
  396. repaintGUI();
  397.  
  398. setDefaultCloseOperation(EXIT_ON_CLOSE);
  399. setSize(550,500);
  400. setResizable(true);
  401. setLocationRelativeTo(null);
  402. setVisible(true);
  403. }
  404.  
  405. // (re)display the GUI with current product's information
  406. public void repaintGUI() {
  407. Movie temp = (Movie) theInventory.getDVD(index);
  408.  
  409. if (temp != null) {
  410. itemNumberText.setText("" + temp.getItemNo());
  411. prodnameText.setText(temp.getTitle());
  412. prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice()));
  413. movieRatingText.setText(temp.getRating());
  414. numinstockText.setText("" + temp.getInStock());
  415. valueText.setText(String.format("$%.2f", temp.value()));
  416. }
  417. totalValueText.setText(String.format("$%.2f", theInventory.value()));
  418. }
  419.  
  420. class NextButtonHandler implements ActionListener {
  421. public void actionPerformed(ActionEvent e) {
  422. int numItems = theInventory.getNumItems();
  423. index = (++index) % numItems;
  424. repaintGUI();
  425.  
  426. }
  427. }
  428. class previousButtonHandler implements ActionListener{
  429. public void actionPerformed (ActionEvent e){
  430. index--;
  431. if(index < 0) {
  432. index = theInventory.getNumItems() - 1;
  433. }
  434. repaintGUI();
  435.  
  436. }
  437. }
  438. class firstButtonHandler implements ActionListener{
  439. public void actionPerformed (ActionEvent e){
  440. index = theInventory.getNumItems() - 4;
  441. repaintGUI();
  442.  
  443. }
  444.  
  445. }
  446. class lastButtonHandler implements ActionListener{
  447. public void actionPerformed (ActionEvent e){
  448. index = theInventory.getNumItems() - 1;
  449. repaintGUI();
  450. }
  451.  
  452.  
  453. }
  454. class AddButtonHandler implements ActionListener {
  455. public void actionPerformed(ActionEvent e){
  456. repaintGUI();
  457. }
  458. }
  459. class SaveButtonHandler implements ActionListener {
  460. public void actionPerformed(ActionEvent e){
  461. int itemNo = Inventory.getitemNo();
  462. DVD dvd = new DVD(); //calls inventroy storage class
  463. dvd.openFile();
  464. int currentRecord = 0; // keeps track of number of cycles
  465.  
  466. do // cycles through the list adding them one at a time
  467. {
  468. dvd.addDVD();
  469. currentRecord = currentRecord + 1;
  470. index = (++index) % itemNo;
  471.  
  472. } while (currentRecord < itemNo); //ends while
  473.  
  474. dvd.closeFile(); //closes file
  475. }//end action
  476.  
  477. } // end class
  478.  
  479.  
  480. }
  481. class ModifyButtonHandler implements ActionListener {
  482. private Object prodName;
  483. private Object itemNo;
  484. private Object numinstockText;
  485. public void actionPerformed(ActionEvent e){
  486. if (prodName.getClass().equals("")) //traps for blank entry
  487. {
  488. JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
  489. repaintGUI();
  490. }
  491.  
  492.  
  493. if (itemNo.getClass().equals("")) //traps for blank entry
  494. {
  495. JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
  496. repaintGUI();
  497. }
  498.  
  499. try // traps for letters and blank entry
  500. {
  501.  
  502.  
  503.  
  504.  
  505. }
  506. catch (Exception d)
  507. {
  508. JOptionPane.showMessageDialog(null, "Recheck Entry use numbers for price and quantity.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
  509. repaintGUI();
  510. }
  511.  
  512.  
  513. String name; int number; double amount, price; String title; // declares variables
  514.  
  515. name = prodNameText();
  516. number = itemNumberText();
  517. amount = numinstockText();
  518. price = prodpriceText();
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526. repaintGUI();
  527. }
  528.  
  529. private int itemNumberText() {
  530. return itemNumberText();
  531. }
  532.  
  533. private double numinstockText() {
  534. return numinstockText();
  535. }
  536.  
  537. private String prodNameText() {
  538. return prodNameText();
  539. }
  540.  
  541. private double prodpriceText() {
  542. return prodpriceText();
  543. }
  544.  
  545. private void repaintGUI() {
  546. repaintGUI();
  547. }
  548.  
  549. } // end class
  550.  
  551.  
  552.  
  553.  
  554.  
  555. class DeleteButtonHandler implements ActionListener {
  556.  
  557. public void actionPerformed(ActionEvent e){
  558. repaintGUI();
  559.  
  560. }
  561.  
  562. private void repaintGUI() {
  563. repaintGUI();
  564. }
  565. }
  566.  
  567.  
  568.  
  569. class SearchButtonHandler implements ActionListener {
  570. private Object searchText;
  571.  
  572.  
  573. public void actionPerformed(ActionEvent e,String prodNameText,String search){
  574.  
  575.  
  576. String name = prodNameText();
  577. int number = itemNumberText();
  578. int units = numinstockText();
  579. int currentDVD = 0;
  580.  
  581. do // gets text and checks for a match
  582. {
  583.  
  584. repaintGUI();
  585. name = prodNameText();
  586. number = itemNumberText();
  587.  
  588.  
  589.  
  590. if(name.equalsIgnoreCase( search )) // stops on item
  591. repaintGUI();
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. }while(name.equalsIgnoreCase(search ) != true); //ends do while
  599.  
  600.  
  601. }//end action
  602. private int itemNumberText() {
  603. return itemNumberText();
  604.  
  605. }
  606.  
  607. private int numinstockText() {
  608. return numinstockText();
  609. }
  610. private String prodNameText() {
  611. return prodNameText();
  612. }
  613.  
  614. private void repaintGUI() {
  615.  
  616. }
  617.  
  618. public void actionPerformed(ActionEvent e) {
  619.  
  620. }
  621.  
  622.  
  623. } // end class
  624.  
  625. class InventoryStorage // creates the output file
  626. {
  627. private Formatter output; // object used to output text to file
  628.  
  629. public void openFile()
  630. {
  631. try
  632. {
  633. String strDirectoy ="C://data/";
  634.  
  635. // Create one directory
  636. boolean success = (new File(strDirectoy)).mkdir();
  637.  
  638. if (success)
  639. {
  640.  
  641. JOptionPane.showMessageDialog(null, "we had to create a directory named data in your C drive.","That's affirmative", JOptionPane.PLAIN_MESSAGE);
  642.  
  643. } //end if
  644.  
  645.  
  646. }//end try
  647.  
  648. catch (Exception e)
  649. {
  650.  
  651.  
  652. JOptionPane.showMessageDialog(null, "You do not have write access to the C: drive.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
  653.  
  654. }//end catch
  655.  
  656.  
  657.  
  658. try
  659. {
  660.  
  661. output = new Formatter( "C:/data/inventory.dat");
  662.  
  663. } // end try
  664.  
  665. catch ( SecurityException securityException ) //catches for write access and exits program
  666. {
  667. JOptionPane.showMessageDialog(null, "You do not have write access to this file.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
  668. System.exit( 1 );
  669.  
  670. } // end catch
  671.  
  672. catch ( FileNotFoundException filesNotFoundException ) //catches for write access and exits program
  673. {
  674. JOptionPane.showMessageDialog(null, "Please create a file named data in your c drive","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
  675. System.exit( 1 );
  676.  
  677. } // end catch
  678.  
  679.  
  680. JOptionPane.showMessageDialog(null, "file saved successfully.","That's affirmative", JOptionPane.PLAIN_MESSAGE);
  681.  
  682.  
  683.  
  684. } // end method openFile
  685.  
  686.  
  687.  
  688.  
  689.  
  690. public void closeFile() // close the file
  691. {
  692. if ( output != null )
  693. output.close();
  694.  
  695. } // end method closeFile
  696.  
  697. }// end InventoryStorage class
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 791
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Help Finishing Inventory Part 6

 
0
  #2
Aug 14th, 2008
The error is occurring on line 488 and is a NullPointerException, which means that something is being referenced on line 488 that hasn't been initialised properly. Which line of code is number 488 (in Inventory.java file)?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC