0

I've got all my parts to inventory part 5 to compile successfully but when I go to run the program I get this message, I am unsure what to do. I know that it possibly means something to do with the image. I've checked the errors that came back and they don't match up to the number line in my code:

Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSouce.getConnection<URLImageSource.java:115>
at sun.awt.image.URLImageSource.getDecoder<URLImageSource.java:125>
at sun.awt.image.InputStreamImageSource.doFectch<InputStreamImageSource.java:263
at sun.awt.image.ImageFetcher.fectchloop<ImageFetcher.java:205>
at sun.awt.image.ImageFecther.run<ImageFetcher.java:169>

Don't know if the other parts are needed but this is the last section to my inventory part 5 that I ran to come up with that 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 
2
Contributors
1
Reply
11
Views
2 Years
Discussion Span
Last Post by stultuske
0

you have somewhere in your code an instance you're using (calling a method on or passing as a parameter) before it is instantiated, meaning it's still null,
but the part of the stacktrace you post isn't enough to see where in your class it's happening.

read the entire stacktrace until you come to a line where it says which line at inventoryTest it happens, and you'll be able to find which variable it is.

This question has already been answered. 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.