8 Replies - 492 Views - Last Post: 18 May 2014 - 09:27 AM Rate Topic: -----

#1 spopivich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 23-April 14

Inventory part 5

Posted 17 May 2014 - 12:03 PM

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:
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


Is This A Good Question/Topic? 0
  • +

Replies To: Inventory part 5

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: Inventory part 5

Posted 17 May 2014 - 12:06 PM

What line does the error message refer to in your posted code?

What do you think the error message means? You've been asked these questions before..
Was This Post Helpful? 0
  • +
  • -

#3 spopivich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 23-April 14

Re: Inventory part 5

Posted 17 May 2014 - 12:18 PM

I've checked the the lines that the errors are on but it doesn't make any sense to me. The lines don't match up with what errors came back.

You say I've been asked this question once before would it be the very beginning to my coding that's not making my program run properly.
Was This Post Helpful? 0
  • +
  • -

#4 spopivich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 23-April 14

Re: Inventory part 5

Posted 17 May 2014 - 12:36 PM

I know that the error message has something to do with the image just don't know what it is
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,171
  • Joined: 05-April 11

Re: Inventory part 5

Posted 17 May 2014 - 12:41 PM

I believe you are have trouble right here
URL url = this.getClass().getResource("logo.jpg");


Taken from the Java docs:

Quote

Returns:
A URL object or null if no resource with this name is found

Your resource could possibly not be found
How have you added logo.jpg to the project? Try placing the image together with the .class file
Was This Post Helpful? 0
  • +
  • -

#6 spopivich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 23-April 14

Re: Inventory part 5

Posted 17 May 2014 - 01:03 PM

This is all new to me java coding and adding images, I get all overwhelmed when putting it together. I am not sure what you mean by try placing the image together with the .class file

Do you mean the very beginning part of my code?
Was This Post Helpful? 0
  • +
  • -

#7 spopivich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 23-April 14

Re: Inventory part 5

Posted 17 May 2014 - 01:41 PM

I am not wanting you to do my homework but is there anyway to show me an example of what I am going wrong
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,171
  • Joined: 05-April 11

Re: Inventory part 5

Posted 17 May 2014 - 05:18 PM

Where and how have you added the image to your project? Are you using an IDE such as Eclipse or Netbeans?
Java source files get compiled into .class files, which you can locate on your disk (depending on the build directory)
The image should be located in the same directory as your class file in this case
Was This Post Helpful? 0
  • +
  • -

#9 spopivich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 23-April 14

Re: Inventory part 5

Posted 18 May 2014 - 09:27 AM

I am using TextPad. The image I am using is from an example someone had and I am trying to get it to work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1