Results 1 to 2 of 2
  1. #1
    spopivich is offline Member
    Join Date
    May 2014
    Posts
    1
    Rep Power
    0

    Unhappy 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.NullPointerException
    at sun.awt.image.URLImageSouce.getConnection<URLImage Source.java:115>
    at sun.awt.image.URLImageSource.getDecoder<URLImageSo urce.java:125>
    at sun.awt.image.InputStreamImageSource.doFectch<Inpu tStreamImageSource.java:263>
    at sun.awt.image.ImageFetcher.fectchloop<ImageFetcher .java:205>
    at sun.awt.image.ImageFecther.run<ImageFetcher.java: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.

    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 spopivich; 05-19-2014 at 12:56 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Inventory Program Part 5

    Please place you code between [code][/code] tags.

    Notice this section of code from above.

    Java Code:
    // 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);
    You don't check to make certain you have retrieved an image before trying to scale it. I suggest you first print out your URL to make certain you have the correct location of the image. Then print out img to make certain it is not null. Employing a try catch block might also be helpful.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Help with inventory program part 5 PLEASE!
    By Exether in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 07:25 AM
  2. Inventory program part 3 (again prob yes)
    By Exether in forum New To Java
    Replies: 16
    Last Post: 07-26-2010, 06:06 AM
  3. Inventory Program Part 2 of 6
    By tlouvierre in forum New To Java
    Replies: 2
    Last Post: 05-28-2009, 02:30 AM
  4. Inventory Program Part 3 ~ please help!
    By marMcD in forum New To Java
    Replies: 13
    Last Post: 02-25-2009, 06:57 AM
  5. Inventory Program Part 3 - DUE TODAY (7/28/08)
    By ljk8950 in forum New To Java
    Replies: 7
    Last Post: 07-27-2008, 11:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •