Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:99)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:113)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.j
ava:240)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

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;



class Inventory {

	String number; // item number
	String name; // item name
	int quantity; // quanity of item
	double price; // item price

	public Inventory(String Number, String Name,  int Quantity, double Price)
	{
		number = Number;
		name = Name;
		quantity = Quantity;
		price = Price;
	}
    //Method set & get product name
	public void setName(String Name)
	{
		name = Name;
	}
	public String getName()
	{
		return name;
	}

    //Method set & get product number
	public void setNumber(String Number)
	{
		number = Number;
	}
	public String getNumber()
	{
		return number;
	}


    //Method set & get quantity of product
	public void setQuantity(int Quantity)
	{
		quantity = Quantity;
	}
	public int getQuantity()
	{
		return quantity;
	}


    //Method set & get product price
	public void setPrice(double Price)
	{
		price = Price;
	}
	public double getPrice()
	{
		return price;
	}

    //Method calculate inventory value
	public double getInventoryValue()
	{
		return price * quantity;
	}


	public String toString()
	{
		return "Item Name: "+name + "\nItem Number: "+number+"\nItem Price: $"+price+"\nQuantity in Stock: "+quantity + "\nInventory Value: $"+getInventoryValue();
	}
} // end Inventory Class



class Product extends Inventory {

	String brand;	// Subclass to add type of book
	double restockFee;	// Restock fee added to the inventory value

	// constructor initializes inventory information
	public Product(String brand, double restockFee, String Number, String Name,  int Quantity,  double Price)
	{
		super(Number, Name, Quantity, Price);
		this.brand = brand;
		this.restockFee = restockFee;
	}

	public Product(String brand, String Number, String Name,  int Quantity, double Price)
	{
		super(Number, Name, Quantity, Price);
		this.brand = brand;
		// default 5% restocking fee
		this.restockFee = 0.05;
	}

	public void setBrandName(String brand)
	{
		brand = brand;
	}
	public String getBrandName()
	{
		return brand;
	}


	public double getRestockFee() // Figures out restocking fee
	{
		return super.getInventoryValue() * restockFee;
	}

	public double getInventoryValue() // Total inventory value plus restocking fee
	{
		return  super.getInventoryValue() + (super.getInventoryValue() * restockFee);
	}

	public String toString()
	{
		StringBuffer sb = new StringBuffer ("\nBrand: ").append(brand).append("\n");
		sb.append (super.toString());


		return sb.toString();
	}

} // End Product Class


class Inventory2
{

	// store book inventory
	Product[] inventory;

	// store number of books added to inventory
	int number_of_products_in_inventory = 0;

	// create an inventory capable of holding maximum_number_of_products different products
	public Inventory2(int maximum_number_of_products)
	{
		inventory = new Product[maximum_number_of_products];
	}

	// add the books to the end of the inventory
	public void addProduct(Product products_to_add_to_inventory)
	{
		inventory[number_of_products_in_inventory] = products_to_add_to_inventory;
		number_of_products_in_inventory++;
	}
	// return the number of products in the inventory
	public Product getProduct(int index)
	{
		return inventory[index];
	}

	// return number of products in inventory
	public int getSize()
	{
		return number_of_products_in_inventory;
	}

	// run through all the books in the inventory, and add up their value.
	// this includes the restocking fee
	public double getTotalValueOfAllInventory()
	{
		double tot = 0.00;

		for(int i = 0; i < number_of_products_in_inventory; i++)
		{
			tot += inventory[i].getInventoryValue();
		}
		return tot;
	}

	// sort the books in the inventory by name
	public void sortInventory2()
	{
		// Sorting Inventory Information
		for(int j = 0; j < number_of_products_in_inventory - 1; j++)
		{
		    for(int k = 0; k < number_of_products_in_inventory - 1; k++)
		    {
		        if(inventory[k].getName().compareToIgnoreCase(inventory[k+1].getName()) > 0)
		        {
		            Product temp = inventory[k];
		            inventory[k] = inventory[k+1];
		            inventory[k+1] = temp;
		        }
		    }
		}
	}
} // End Inventory2 class

public class Inventory5 extends JFrame
{

	//create inventory for the books
	Inventory2 ProductInventory2;

	// index in the supply inventory of the currently displayed books. starts at 0, goes to the number of books in the inventory - 1
	int index = 0;


    // GUI elements to display currently selected books information
	private final JLabel nameLabel = new JLabel(" Product 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 item in the list
	private Action nextAction  = new AbstractAction("Next") {
		public void actionPerformed(ActionEvent evt) {

		// check to see if there is a next book
		if (index == ProductInventory2.getSize() - 1) {
		    // if we're at the book in the list, then we can't go any further
		    // go to front of list
		    index = 0;
		} else {
		    index++;
		}

		repaint();
		}
    };
	private JButton nextButton = new JButton(nextAction);

    // go to previous book in the list
    private Action previousAction  = new AbstractAction("Previous") {
	    public void actionPerformed(ActionEvent evt) {

		// if we're at the first book, then go to the last book in the list
		if (index == 0) {
		    index = ProductInventory2.getSize() - 1;
		} else {
		    index--;
		}

		repaint();
	    }
    };
    private JButton previousButton = new JButton(previousAction);

    // go to the first book in 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 book in 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(Product temp)
	{
		ProductInventory2.addProduct(temp);
		repaint();
	}
	public void sortProductInventory2()
	{
		ProductInventory2.sortInventory2();
		repaint();
	}

	public Inventory5(int maximum_number_of_products)
	{
		// create the inventory object that will hold the book information
		ProductInventory2 = new Inventory2(maximum_number_of_products);

		// setup the GUI
		// add the next book button to the top of the GUI
		// setup panel to collect buttons in FlowLayout
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(firstButton);
		buttonPanel.add(previousButton);
		buttonPanel.add(nextButton);
	    buttonPanel.add(lastButton);
		getContentPane().add(buttonPanel, BorderLayout.NORTH);


        // setup logo for GUI
		URL url = this.getClass().getResource("logo.jpg");
		   Image img = Toolkit.getDefaultToolkit().getImage(url);
		// scale image to fit in GUI
		Image scaledImage = img.getScaledInstance(205, 300, Image.SCALE_AREA_AVERAGING);
		// create JLabel with image as the label's Icon
		Icon logoIcon = new ImageIcon(scaledImage);
		JLabel companyLogoLabel = new JLabel(logoIcon);

		// add logo to GUI
		getContentPane().add(companyLogoLabel, BorderLayout.WEST);

        // book 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 inventory information to 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() {

		Product temp = ProductInventory2.getProduct(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 new GUI object that will hold a maximum of 6 books
		Inventory5 Product_GUI = new Inventory5 (6);

		// Add the office supplies to the inventory
		Product_GUI.addProductToInventory2(new Product("HardBack", "1","Harry Potter" , 27, 15.99));
		Product_GUI.addProductToInventory2(new Product("HardBack", "2","Eragon" , 15, 10.99));
		Product_GUI.addProductToInventory2(new Product("SoftBack", "3","Letters to my Daughter", 14, 24.99));
		Product_GUI.addProductToInventory2(new Product("HardBack", "4","Twlight" , 20, 22.99));
        Product_GUI.addProductToInventory2(new Product("SoftBack", "5", "Eldest", 35, 11.99));
        Product_GUI.addProductToInventory2(new Product("SoftBack", "6", "The Da Vinci Code", 24, 9.00));


		// sort the books by name
		Product_GUI.sortProductInventory2();

		// Get GUI to show on screen
		Product_GUI.setDefaultCloseOperation( EXIT_ON_CLOSE );
		Product_GUI.pack();
		Product_GUI.setSize(700, 350);
		Product_GUI.setResizable(false);
		Product_GUI.setLocationRelativeTo( null );
		Product_GUI.setVisible(true);

		return;

	}
}  // End Inventory5 class

Recommended Answers

All 5 Replies

Somewhere in your code you are passing a null value when you can't. If I had to guess I would say your problem occurs somewhere near these lines of code:

URL url = this.getClass().getResource("logo.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);

Correct, and, as I said in your other thread, it is because the image file (as referenced there) doesn't exist. Read the API docs for the getResource method carefully and completely.

And if the image file could not exist, you should catch the java.lang.NullPointerException

try{
...
}
catch(java.lang.NullPointerException e)
{
...
}

Somewhere in your code you are passing a null value when you can't. If I had to guess I would say your problem occurs somewhere near these lines of code:

URL url = this.getClass().getResource("logo.jpg");
Image img = Toolkit.getDefaultToolkit().getImage(url);

Thank you for your help, but now I am getting this error that I do not understand.

java: 327: cannot find symbol
symbol: variable img
location: class inventory 5
image scaledImage = img.getScaledInstance(205, 300.SCALE_AREA_AVERAGING);

// setup logo for GUI
		ImageIcon ic = new ImageIcon("logo.jpg");
		   JLabel label = new JLabel(ic);
		// scale image to fit in GUI
		Image scaledImage = img.getScaledInstance(205, 300, Image.SCALE_AREA_AVERAGING);
		// create JLabel with image as the label's Icon
		Icon logoIcon = new ImageIcon(scaledImage);
		JLabel companyLogoLabel = new JLabel(logoIcon);

You defined your imageicon as ic and then try to use it as img.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.