0

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)

package javaapplication54;

import java.awt.*;  // import the java awt package
import java.awt.event.*;  // Import the java event package
import javax.swing.*; // Import the java swing package
import java.util.*; // Import the java utility package
import java.io.*; // Import the java input output package
import java.awt.Image;
import javax.swing.ImageIcon;





public class Inventory6 {

    // main method begins
    public static void main(String[] args) {
      
      

        Movie dvd = null;
        Inventory inventory = new Inventory();        

        dvd = new Movie(2356, "Legally Blonde", 25, 14.99f, "G");
        System.out.println(dvd);
        inventory.addMovie(dvd);                    
        
        dvd = new Movie(5684, "National Tressure", 3, 12.00f, "G");
        System.out.println(dvd);
        inventory.addMovie(dvd);                    
        
        dvd = new Movie(5564, "Sweet Home Alabama", 15, 15.75f, "G");
        System.out.println(dvd);
        inventory.addMovie(dvd);                    

        dvd = new Movie(5562, "Shrek", 7, 9.99f, "G");
        inventory.addMovie(dvd);                    
        System.out.println(dvd);

      
                          

        inventory.printInventory();
          new InventoryGUI(inventory);
          
    } // end main

} // end Inventory6






// Extends DVD class from the base class DVD
class DVD {
    private int itemNo;
    private String title;
    private int inStock;
    private float unitPrice;

    DVD() {
        
    }

    DVD(int itemNo, String title, int inStock, float unitPrice) {
        this.itemNo    = itemNo;
        this.title     = title;
        this.inStock   = inStock;
        this.unitPrice = unitPrice;
      
    }

    public int getItemNo()      { return itemNo; }
    public String getTitle()    { return title; }
    public int getInStock()     { return inStock; }
    public float getUnitPrice() { return unitPrice; }
  
    public float value() {
        return inStock * unitPrice;
    }

    @Override
    public String toString() {
        return String.format("itemNo=%2d   title=%-22s   inStock=%3d   price=$%7.2f   value=$%8.2f",
                              itemNo, title, inStock, unitPrice, value());
    }

    void addDVD() {
       
    }

    void closeFile() {
        
    }

    void openFile() {
        
    }

} // end DVD
class Inventory {
    private static int itemNo;

    static int getitemNo() {
         return itemNo;
         
    }
    // Setup an array of Movies
    private final int INVENTORY_SIZE = 100;
    private DVD[] items;
    private int numItems;
    
    Inventory() {
        items = new Movie[INVENTORY_SIZE];
        numItems = 0;
    }

    public int getNumItems() {
        return numItems;
    }

    public DVD getDVD(int n) {
        return items[n];
    }

    // Adds a Movie to the array of Movies. Adds to first empty slot found.
    public void addMovie(DVD item) {
        items[numItems] = item;    
        ++numItems;
    }

    
    public double value() {
        double sumOfInventory = 0.0;

        for (int i = 0; i < numItems; i++)
            sumOfInventory += items[i].value();
                        
        return sumOfInventory;
    }

    
    public void printInventory() {
        System.out.println("\n DVD Inventory\n");
      
        
        if (numItems <= 0) {
                   } else {
            for (int i = 0; i < numItems; i++)
                System.out.printf("%3d   %s\n", i, items[i]);
            System.out.printf("\nTotal value is $%,.2f\n\n", value());
        }
    }
    
} // end Inventory
class Movie extends DVD {
    // Holds movie Rating and adds restocking fee
    private String movieRating;

    // Constructor, calls the constructor of Movie first
    public Movie(int MovieID, String itemName, int units, float itemPrice, String Rating) {
        super(MovieID, itemName, units, itemPrice);    
        // Pass on the values needed for creating the Movie class first thing
        this.movieRating = Rating;        
    }

    



    // To set the rating manually
    public void setRating(String Rating) {
        movieRating = Rating;
    }

    // Get the rating
    public String getRating() {
        return movieRating;
    }

    // Overrides value() in Movie class by calling the base class version and
    // adding a 5% restocking fee on top
    @Override
     public float value() {
        return super.value() + restockingFee();
    }

    // Simply gets the base class's value, and figures out the 5% restocking fee only
    public float restockingFee() {
        return super.value() * 0.05f;
    }

} // end Movie
class ArcsJPanel extends JPanel
{
    
    @Override
    public void paintComponent( Graphics g )
    {
        super.paintComponent( g ); // call superclass's paintComponent

        // draws larger 3d rectangle
        g.setColor( Color.RED );
        g.draw3DRect( 45, 15, 180, 40, true);
        
        // draws small black square
        g.setColor( Color.BLACK );
        g.draw3DRect( 5, 15, 40, 40, true );
        g.fill3DRect( 5, 15, 40, 40, false );
        
        //draws circle
        g.setColor( Color.RED );
        g.drawArc( 5, 15, 40, 40, 10, 360 );
        
        //fills circle
        g.setColor( Color.WHITE);
        g.fillArc( 5, 15, 40, 40, 10, 360 );

        // draws company name
        g.setColor( Color.BLACK );
        g.setFont( new Font( "Serif", Font.BOLD, 25 ) );
        g.drawString( "Estey's DVD Shack.", 10, 45 );

     } // end method paintComponent

} // end class ArcsJPanel


// GUI for the Inventory
// Contains an inventory of DVD's and lets the user step through them one by one
class InventoryGUI extends JFrame
{
    // access inventory for DVD Collection
    private Inventory theInventory;
    
    // index in the inventory of the currently displayed DVD.
    // the index starts at 0, goes to the number of DVDs in the inventory minus 1
    private int index = 0;
    
    // GUI elements to display currently selected DVD information
    private final JLabel itemNumberLabel = new JLabel("  Item Number:");
    private JTextField itemNumberText;
    
    private final JLabel prodnameLabel = new JLabel("  Product Name:");
    private JTextField prodnameText;
    
    private final JLabel prodpriceLabel = new JLabel("  Price:");
    private JTextField prodpriceText;
    
    private final JLabel numinstockLabel = new JLabel("  Number in Stock:");
    private JTextField numinstockText;
    
    private final JLabel valueLabel = new JLabel("  Value:");
    private JTextField valueText;
    
    private final JLabel movieRatingLabel = new JLabel("  Rated:");
    private JTextField movieRatingText;
    
    private final JLabel totalValueLabel = new JLabel("  Inventory Total Value (5% restock fee included:");
    private JTextField totalValueText;
    
  
    
    
      
    

    

    private JPanel centerPanel;
    private JPanel buttonPanel;





    // constructor for the GUI, in charge of creating all GUI elements
    InventoryGUI(Inventory inventory) {
        super("Movie Inventory");
        final Dimension dim = new Dimension(140, 20);
        final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
        JPanel jp;

        // create the inventory object that will hold the product information
        theInventory = inventory;        
        
      
        centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));

      
        
       buttonPanel = new JPanel();
        JButton firstButton = new JButton("First");    
        firstButton.addActionListener((ActionListener) new firstButtonHandler());
        buttonPanel.add(firstButton);

JButton previousButton = new JButton("Previous");
previousButton.addActionListener(new previousButtonHandler());
buttonPanel.add(previousButton);

JButton nextButton = new JButton("Next");
nextButton.addActionListener(new NextButtonHandler());
buttonPanel.add(nextButton);

JButton lastButton = new JButton("Last");
lastButton.addActionListener(new lastButtonHandler());
buttonPanel.add(lastButton);

JButton addButton = new JButton("Add");
addButton.addActionListener(new AddButtonHandler());
buttonPanel.add(addButton);

JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(new DeleteButtonHandler());
buttonPanel.add(deleteButton);

JButton modifyButton = new JButton("Modify");
modifyButton.addActionListener(new ModifyButtonHandler());
buttonPanel.add(modifyButton);

JButton saveButton = new JButton("Save");
saveButton.addActionListener(new SaveButtonHandler());
buttonPanel.add(saveButton);

JButton searchButton = new JButton("Search");
searchButton.addActionListener(new SearchButtonHandler());
buttonPanel.add(searchButton);

   centerPanel.add(buttonPanel);
            
        jp = new JPanel(flo);
        itemNumberLabel.setPreferredSize(dim);
        jp.add(itemNumberLabel);
        itemNumberText = new JTextField(3);
        itemNumberText.setEditable(false);
        jp.add(itemNumberText);
        centerPanel.add(jp);
        
        jp = new JPanel(flo);
        prodnameLabel.setPreferredSize(dim);
        jp.add(prodnameLabel);
        prodnameText = new JTextField(17);
        prodnameText.setEditable(false);
        jp.add(prodnameText);
        centerPanel.add(jp);
        
        jp = new JPanel(flo);
        movieRatingLabel.setPreferredSize(dim);
        jp.add(movieRatingLabel);
        movieRatingText = new JTextField(17);
        movieRatingText.setEditable(false);
        jp.add(movieRatingText);
        centerPanel.add(jp);
        
        jp = new JPanel(flo);
        prodpriceLabel.setPreferredSize(dim);
        jp.add(prodpriceLabel);
        prodpriceText = new JTextField(17);
        prodpriceText.setEditable(false);
        jp.add(prodpriceText);
        centerPanel.add(jp);
        
        jp = new JPanel(flo);
        numinstockLabel.setPreferredSize(dim);
        jp.add(numinstockLabel);
        numinstockText = new JTextField(5);
        numinstockText.setEditable(false);
        jp.add(numinstockText);  
        centerPanel.add(jp);
        
        
        
        jp = new JPanel(flo);
        valueLabel.setPreferredSize(dim);
        jp.add(valueLabel);
        valueText = new JTextField(17);
        valueText.setEditable(false);
        jp.add(valueText);
        centerPanel.add(jp);
        
        // add the overall inventory information to the panel
        jp = new JPanel(flo);
        totalValueLabel.setPreferredSize(dim);
        jp.add(totalValueLabel);
        totalValueText = new JTextField(17);
        totalValueText.setEditable(false);
        jp.add(totalValueText);
        centerPanel.add(jp);

        // add the panel to the GUI display
        setContentPane(centerPanel);

        repaintGUI();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(550,500);
        setResizable(true);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    // (re)display the GUI with current product's information
    public void repaintGUI() {
        Movie temp = (Movie) theInventory.getDVD(index);
        
        if (temp != null) {
            itemNumberText.setText("" + temp.getItemNo());    
            prodnameText.setText(temp.getTitle());
            prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice()));
            movieRatingText.setText(temp.getRating());
            numinstockText.setText("" + temp.getInStock());
            valueText.setText(String.format("$%.2f", temp.value()));
        }
        totalValueText.setText(String.format("$%.2f", theInventory.value()));
    }

    class NextButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int numItems = theInventory.getNumItems();
            index = (++index) % numItems;
            repaintGUI();
            
        }
    }
   class previousButtonHandler implements ActionListener{
        public void actionPerformed (ActionEvent e){
            index--;
            if(index < 0) {
               index = theInventory.getNumItems() - 1;
            }
            repaintGUI();

         }
    }
    class firstButtonHandler implements ActionListener{
        public void actionPerformed (ActionEvent e){
           index = theInventory.getNumItems() - 4;
            repaintGUI();
            
        }
        
    }
   class lastButtonHandler implements ActionListener{
        public void actionPerformed (ActionEvent e){
            index = theInventory.getNumItems() - 1;
            repaintGUI();
        }

  
    }
class AddButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}
class SaveButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
    int itemNo = Inventory.getitemNo();
            DVD dvd = new DVD(); //calls inventroy storage class
            dvd.openFile();
            int currentRecord = 0; // keeps track of number of cycles

            do // cycles through the list adding them one at a time
            {
                dvd.addDVD();
                currentRecord = currentRecord + 1;
                index = (++index) % itemNo;

            } while (currentRecord < itemNo); //ends while

                dvd.closeFile(); //closes file
        }//end action

    } // end class


}
class ModifyButtonHandler implements ActionListener {
    private Object prodName;
    private Object itemNo;
    private Object numinstockText;
public void actionPerformed(ActionEvent e){
if (prodName.getClass().equals(""))  //traps for blank entry
            {
                JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
                repaintGUI();
            }


            if (itemNo.getClass().equals(""))  //traps for blank entry
            {
                JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
                repaintGUI();
            }

            try // traps for letters and blank entry
            {
            
                
                
                
                }
                catch (Exception d)
                {
                JOptionPane.showMessageDialog(null, "Recheck Entry use numbers for price and quantity.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
                repaintGUI();
                }


            String name; int number; double amount, price; String title; // declares variables
            
                          name = prodNameText();
            number = itemNumberText();
            amount = numinstockText();
            price = prodpriceText();
        
        

        

            
            
            repaintGUI();
        }

    private int itemNumberText() {
        return itemNumberText();
    }

    private double numinstockText() {
        return numinstockText();
    }

    private String prodNameText() {
        return prodNameText();
    }

    private double prodpriceText() {
        return prodpriceText();
    }

    private void repaintGUI() {
       repaintGUI();
    }

    } // end class
                    
                    



class DeleteButtonHandler implements ActionListener {
      
public void actionPerformed(ActionEvent e){
repaintGUI();

}

    private void repaintGUI() {
       repaintGUI();
    }
}



class SearchButtonHandler implements ActionListener {
    private Object searchText;

    
public void actionPerformed(ActionEvent e,String prodNameText,String search){
    

            String name = prodNameText();
            int number = itemNumberText();
            int units = numinstockText();
            int currentDVD = 0;
    
            do // gets text and checks for a match
            {    
                
                repaintGUI();
                name = prodNameText();
                number = itemNumberText();
                
            

                if(name.equalsIgnoreCase( search )) // stops on item
                    repaintGUI();

                

                
        

            }while(name.equalsIgnoreCase(search ) != true); //ends do while
      

        }//end action
    private int itemNumberText() {
       return itemNumberText();
       
    }

    private int numinstockText() {
        return numinstockText();
    }
    private String prodNameText() {
        return prodNameText();
    }

    private void repaintGUI() {
        
    }

    public void actionPerformed(ActionEvent e) {
       
    }

      
    } // end class
  
class InventoryStorage // creates the output file
{
    private Formatter output; // object used to output text to file

        public void openFile()
        {
            try
            {
                    String strDirectoy ="C://data/";

                    // Create one directory
                    boolean success = (new File(strDirectoy)).mkdir();
                    
                if (success)
                {

                          JOptionPane.showMessageDialog(null, "we had to create a directory named data in your C drive.","That's affirmative", JOptionPane.PLAIN_MESSAGE);

                   }   //end if


             }//end try

            catch (Exception e)
            {
              
                
                      JOptionPane.showMessageDialog(null, "You do not have write access to the C: drive.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);

                }//end catch
  


            try
            {
                
                          output = new Formatter( "C:/data/inventory.dat");

            } // end try

        catch ( SecurityException securityException ) //catches for write access and exits program
        {
            JOptionPane.showMessageDialog(null, "You do not have write access to this file.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
            System.exit( 1 );

        } // end catch

        catch ( FileNotFoundException filesNotFoundException ) //catches for write access and exits program
        {
            JOptionPane.showMessageDialog(null, "Please create a file named data in your c drive","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
            System.exit( 1 );

        } // end catch


        JOptionPane.showMessageDialog(null, "file saved successfully.","That's affirmative", JOptionPane.PLAIN_MESSAGE);



    } // end method openFile

        


    
    public void closeFile() // close the file
    {
        if ( output != null )
        output.close();

    } // end method closeFile

}// end InventoryStorage class
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by darkagn
0

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)?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.