Java coding Inventory Program Part 5

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spopivich
    New Member
    • May 2014
    • 1

    Java coding Inventory Program Part 5

    This is my first class to java programming (java coding is all new to me) and I have my part 5 assignment due today and I am absolutely clueless to why I am getting this error but I know it has to do something with my image I just do not know how to fix my problem. I was able to compile successfully but it came back with an error message when I go to run the program. What the message reads:
    Uncaught error fetching image:
    java.lang.NullP ointerException
    at sun.awt.image.U RLImageSouce.ge tConnection<URL Image Source.java:115 >
    at sun.awt.image.U RLImageSource.g etDecoder<URLIm ageSo urce.java:125>
    at sun.awt.image.I nputStreamImage Source.doFectch <Inpu tStreamImageSou rce.java:263>
    at sun.awt.image.I mageFetcher.fec tchloop<ImageFe tcher .java:205>
    at sun.awt.image.I mageFecther.run <ImageFetcher.j ava:1 69>

    Not sure if all parts are needed that I have compiled. Here is the last part that I have compiled that gave me the error message.

    Code:
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.Dimension;
    import java.net.URL;
    
    public class inventoryTest extends JFrame
    {
    
    //create inventory for the dvds
    Inventory2 ProductInventory2;
    
    // index in the supply inventory of the currently displayed dvd. starts at 0, goes to the number of dvds in the inventory - 1
    int index = 0;
    
    // GUI elements to display currently selected dvds information
    private final JLabel nameLabel = new JLabel(" DVD Name: ");
    private JTextField nameText;
    
    private final JLabel numberLabel = new JLabel(" Product Number: ");
    private JTextField numberText;
    
    private final JLabel brandLabel = new JLabel(" Brand: ");
    private JTextField brandText;
    
    private final JLabel priceLabel = new JLabel(" Price: ");
    private JTextField priceText;
    
    private final JLabel quantityLabel = new JLabel(" Quantity: ");
    private JTextField quantityText;
    
    private final JLabel valueLabel = new JLabel(" Value: ");
    private JTextField valueText;
    
    private final JLabel restockFeeLabel = new JLabel(" Restocking Fee: ");
    private JTextField restockFeeText;
    
    private final JLabel totalValueLabel = new JLabel(" Inventory Total: ");
    private JLabel totalValueText;
    
    // go to the next dvd in the list
    private Action nextAction = new AbstractAction("Next") {
    public void actionPerformed(ActionEvent evt) {
    
    // check to see if there is a next dvd
    if (index == ProductInventory2.getSize() - 1) {
    // if we're at the last dvd in the list, then we can't go any further
    // so go to the front of the list
    index = 0;
    } else {
    index++;
    }
    
    repaint();
    }
    };
    private JButton nextButton = new JButton(nextAction);
    
    // go to the previous dvd in the list
    private Action previousAction = new AbstractAction("Previous") {
    public void actionPerformed(ActionEvent evt) {
    
    // if we're at the first dvd, then go to the last dvd in the list
    if (index == 0) {
    index = ProductInventory2.getSize() - 1;
    } else {
    index--;
    }
    
    repaint();
    }
    };
    private JButton previousButton = new JButton(previousAction);
    
    // go to the first dvd in the list
    private Action firstAction = new AbstractAction("First") {
    public void actionPerformed(ActionEvent evt) {
    
    index = 0;
    
    repaint();
    }
    };
    private JButton firstButton = new JButton(firstAction);
    
    // go to the last dvd in the list
    private Action lastAction = new AbstractAction("Last") {
    public void actionPerformed(ActionEvent evt) {
    
    index = ProductInventory2.getSize() - 1;
    
    repaint();
    }
    };
    private JButton lastButton = new JButton(lastAction);
    
    public void addProductToInventory2(DVD temp)
    {
    ProductInventory2.addDVD(temp);
    repaint();
    }
    public void sortProductInventory2()
    {
    ProductInventory2.sortInventory2();
    repaint();
    }
    
    public inventoryTest(int maximum_number_of_dvd)
    {
    // create the inventory object that will hold the dvd information
    ProductInventory2 = new Inventory2(maximum_number_of_dvd);
    
    // setup the GUI
    // add the next dvd button to the top of the GUI
    // setup a panel to collect all the buttons in a FlowLayout
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(firstButton);
    buttonPanel.add(previousButton);
    buttonPanel.add(nextButton);
    buttonPanel.add(lastButton);
    getContentPane().add(buttonPanel, BorderLayout.NORTH);
    
    // setup the logo for the GUI
    URL url = this.getClass().getResource("logo.jpg");
    Image img = Toolkit.getDefaultToolkit().getImage(url);
    // scale the image so that it'll fit in the GUI
    Image scaledImage = img.getScaledInstance(205, 300, Image.SCALE_AREA_AVERAGING);
    // create a JLabel with the image as the label's Icon
    Icon logoIcon = new ImageIcon(scaledImage);
    JLabel companyLogoLabel = new JLabel(logoIcon);
    
    // add the logo to the GUI
    getContentPane().add(companyLogoLabel, BorderLayout.WEST);
    
    // product information
    // setup a panel to collect all the components.
    JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4));
    
    centerPanel.add(nameLabel);
    nameText = new JTextField("");
    nameText.setEditable(false);
    centerPanel.add(nameText);
    
    centerPanel.add(numberLabel);
    numberText = new JTextField("");
    numberText.setEditable(false);
    centerPanel.add(numberText);
    
    centerPanel.add(brandLabel);
    brandText = new JTextField("");
    brandText.setEditable(false);
    centerPanel.add(brandText);
    
    centerPanel.add(priceLabel);
    priceText = new JTextField("");
    priceText.setEditable(false);
    centerPanel.add(priceText);
    
    centerPanel.add(quantityLabel);
    quantityText = new JTextField("");
    quantityText.setEditable(false);
    centerPanel.add(quantityText);
    
    centerPanel.add(valueLabel);
    valueText = new JTextField("");
    valueText.setEditable(false);
    centerPanel.add(valueText);
    
    centerPanel.add(restockFeeLabel);
    restockFeeText = new JTextField("");
    restockFeeText.setEditable(false);
    centerPanel.add(restockFeeText);
    
    // add the overall inventory information to the panel
    centerPanel.add(totalValueLabel);
    totalValueText = new JLabel("");
    centerPanel.add(totalValueText);
    
    // add the panel to the center of the GUI's window
    getContentPane().add(centerPanel, BorderLayout.CENTER);
    
    repaint();
    }
    
    // repaint the GUI with new Product information
    public void repaint() {
    
    DVD temp = ProductInventory2.getDVD(index);
    
    if (temp != null) {
    nameText.setText( temp.getName() );
    numberText.setText( ""+temp.getNumber() );
    brandText.setText( temp.getBrandName() );
    priceText.setText( String.format("$%.2f", temp.getPrice()) );
    quantityText.setText( ""+temp.getQuantity() );
    valueText.setText( String.format("$%.2f", temp.getPrice() * temp.getQuantity() ) );
    restockFeeText.setText( String.format("$%.2f", temp.getRestockFee() ) );
    }
    
    totalValueText.setText( String.format("$%.2f", ProductInventory2.getTotalValueOfAllInventory() ) );
    
    }
    
    public static void main(String args[])
    {
    
    // create a new GUI object that will hold a maximum of 4 office supplies
    inventoryTest DVD_GUI = new inventoryTest (4);
    
    // Add the dvds to the inventory
    DVD_GUI.addProductToInventory2(new DVD("Sony" , "101","Korn" , 20, 10.00));
    DVD_GUI.addProductToInventory2(new DVD("BMG" , "201","TLC" , 20, 12.00));
    DVD_GUI.addProductToInventory2(new DVD("Song", "301","Creed", 10, 13.00));
    DVD_GUI.addProductToInventory2(new DVD("BMG", "401","Drake" , 15, 14.00));
    
    // sort the dvd by name
    DVD_GUI.sortProductInventory2();
    
    // Get the GUI to show up on the screen
    DVD_GUI.setDefaultCloseOperation( EXIT_ON_CLOSE );
    DVD_GUI.pack();
    DVD_GUI.setSize(700, 350);
    DVD_GUI.setResizable(false);
    DVD_GUI.setLocationRelativeTo( null );
    DVD_GUI.setVisible(true);
    
    return;
    
    }
    } // End inventoryTest class
    Last edited by Rabbit; May 18 '14, 11:19 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    I can see only GUI commands (Swing) inside the listed sourcecode. They cannot cause the error.
    And I can see access to objects (like DVD_GUI) where the source code is missing, but that could lead to the error.
    So just list the source code that calls URLImageSouce.g etConnection().
    Because there you must define a URL. And if you have given null instead of a valid URL, then it will crash when trying to open this connection with
    url.openConnect ion() inside URLImageSouce.g etConnection() method.

    Comment

    Working...