473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.FlowLayout;
  5. import java.awt.BorderLayout;
  6. import java.awt.GridLayout;
  7. import java.awt.Image;
  8. import java.awt.Toolkit;
  9. import java.awt.Dimension;
  10. import java.net.URL;
  11.  
  12. public class inventoryTest extends JFrame
  13. {
  14.  
  15. //create inventory for the dvds
  16. Inventory2 ProductInventory2;
  17.  
  18. // index in the supply inventory of the currently displayed dvd. starts at 0, goes to the number of dvds in the inventory - 1
  19. int index = 0;
  20.  
  21. // GUI elements to display currently selected dvds information
  22. private final JLabel nameLabel = new JLabel(" DVD Name: ");
  23. private JTextField nameText;
  24.  
  25. private final JLabel numberLabel = new JLabel(" Product Number: ");
  26. private JTextField numberText;
  27.  
  28. private final JLabel brandLabel = new JLabel(" Brand: ");
  29. private JTextField brandText;
  30.  
  31. private final JLabel priceLabel = new JLabel(" Price: ");
  32. private JTextField priceText;
  33.  
  34. private final JLabel quantityLabel = new JLabel(" Quantity: ");
  35. private JTextField quantityText;
  36.  
  37. private final JLabel valueLabel = new JLabel(" Value: ");
  38. private JTextField valueText;
  39.  
  40. private final JLabel restockFeeLabel = new JLabel(" Restocking Fee: ");
  41. private JTextField restockFeeText;
  42.  
  43. private final JLabel totalValueLabel = new JLabel(" Inventory Total: ");
  44. private JLabel totalValueText;
  45.  
  46. // go to the next dvd in the list
  47. private Action nextAction = new AbstractAction("Next") {
  48. public void actionPerformed(ActionEvent evt) {
  49.  
  50. // check to see if there is a next dvd
  51. if (index == ProductInventory2.getSize() - 1) {
  52. // if we're at the last dvd in the list, then we can't go any further
  53. // so go to the front of the list
  54. index = 0;
  55. } else {
  56. index++;
  57. }
  58.  
  59. repaint();
  60. }
  61. };
  62. private JButton nextButton = new JButton(nextAction);
  63.  
  64. // go to the previous dvd in the list
  65. private Action previousAction = new AbstractAction("Previous") {
  66. public void actionPerformed(ActionEvent evt) {
  67.  
  68. // if we're at the first dvd, then go to the last dvd in the list
  69. if (index == 0) {
  70. index = ProductInventory2.getSize() - 1;
  71. } else {
  72. index--;
  73. }
  74.  
  75. repaint();
  76. }
  77. };
  78. private JButton previousButton = new JButton(previousAction);
  79.  
  80. // go to the first dvd in the list
  81. private Action firstAction = new AbstractAction("First") {
  82. public void actionPerformed(ActionEvent evt) {
  83.  
  84. index = 0;
  85.  
  86. repaint();
  87. }
  88. };
  89. private JButton firstButton = new JButton(firstAction);
  90.  
  91. // go to the last dvd in the list
  92. private Action lastAction = new AbstractAction("Last") {
  93. public void actionPerformed(ActionEvent evt) {
  94.  
  95. index = ProductInventory2.getSize() - 1;
  96.  
  97. repaint();
  98. }
  99. };
  100. private JButton lastButton = new JButton(lastAction);
  101.  
  102. public void addProductToInventory2(DVD temp)
  103. {
  104. ProductInventory2.addDVD(temp);
  105. repaint();
  106. }
  107. public void sortProductInventory2()
  108. {
  109. ProductInventory2.sortInventory2();
  110. repaint();
  111. }
  112.  
  113. public inventoryTest(int maximum_number_of_dvd)
  114. {
  115. // create the inventory object that will hold the dvd information
  116. ProductInventory2 = new Inventory2(maximum_number_of_dvd);
  117.  
  118. // setup the GUI
  119. // add the next dvd button to the top of the GUI
  120. // setup a panel to collect all the buttons in a FlowLayout
  121. JPanel buttonPanel = new JPanel();
  122. buttonPanel.add(firstButton);
  123. buttonPanel.add(previousButton);
  124. buttonPanel.add(nextButton);
  125. buttonPanel.add(lastButton);
  126. getContentPane().add(buttonPanel, BorderLayout.NORTH);
  127.  
  128. // setup the logo for the GUI
  129. URL url = this.getClass().getResource("logo.jpg");
  130. Image img = Toolkit.getDefaultToolkit().getImage(url);
  131. // scale the image so that it'll fit in the GUI
  132. Image scaledImage = img.getScaledInstance(205, 300, Image.SCALE_AREA_AVERAGING);
  133. // create a JLabel with the image as the label's Icon
  134. Icon logoIcon = new ImageIcon(scaledImage);
  135. JLabel companyLogoLabel = new JLabel(logoIcon);
  136.  
  137. // add the logo to the GUI
  138. getContentPane().add(companyLogoLabel, BorderLayout.WEST);
  139.  
  140. // product information
  141. // setup a panel to collect all the components.
  142. JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4));
  143.  
  144. centerPanel.add(nameLabel);
  145. nameText = new JTextField("");
  146. nameText.setEditable(false);
  147. centerPanel.add(nameText);
  148.  
  149. centerPanel.add(numberLabel);
  150. numberText = new JTextField("");
  151. numberText.setEditable(false);
  152. centerPanel.add(numberText);
  153.  
  154. centerPanel.add(brandLabel);
  155. brandText = new JTextField("");
  156. brandText.setEditable(false);
  157. centerPanel.add(brandText);
  158.  
  159. centerPanel.add(priceLabel);
  160. priceText = new JTextField("");
  161. priceText.setEditable(false);
  162. centerPanel.add(priceText);
  163.  
  164. centerPanel.add(quantityLabel);
  165. quantityText = new JTextField("");
  166. quantityText.setEditable(false);
  167. centerPanel.add(quantityText);
  168.  
  169. centerPanel.add(valueLabel);
  170. valueText = new JTextField("");
  171. valueText.setEditable(false);
  172. centerPanel.add(valueText);
  173.  
  174. centerPanel.add(restockFeeLabel);
  175. restockFeeText = new JTextField("");
  176. restockFeeText.setEditable(false);
  177. centerPanel.add(restockFeeText);
  178.  
  179. // add the overall inventory information to the panel
  180. centerPanel.add(totalValueLabel);
  181. totalValueText = new JLabel("");
  182. centerPanel.add(totalValueText);
  183.  
  184. // add the panel to the center of the GUI's window
  185. getContentPane().add(centerPanel, BorderLayout.CENTER);
  186.  
  187. repaint();
  188. }
  189.  
  190. // repaint the GUI with new Product information
  191. public void repaint() {
  192.  
  193. DVD temp = ProductInventory2.getDVD(index);
  194.  
  195. if (temp != null) {
  196. nameText.setText( temp.getName() );
  197. numberText.setText( ""+temp.getNumber() );
  198. brandText.setText( temp.getBrandName() );
  199. priceText.setText( String.format("$%.2f", temp.getPrice()) );
  200. quantityText.setText( ""+temp.getQuantity() );
  201. valueText.setText( String.format("$%.2f", temp.getPrice() * temp.getQuantity() ) );
  202. restockFeeText.setText( String.format("$%.2f", temp.getRestockFee() ) );
  203. }
  204.  
  205. totalValueText.setText( String.format("$%.2f", ProductInventory2.getTotalValueOfAllInventory() ) );
  206.  
  207. }
  208.  
  209. public static void main(String args[])
  210. {
  211.  
  212. // create a new GUI object that will hold a maximum of 4 office supplies
  213. inventoryTest DVD_GUI = new inventoryTest (4);
  214.  
  215. // Add the dvds to the inventory
  216. DVD_GUI.addProductToInventory2(new DVD("Sony" , "101","Korn" , 20, 10.00));
  217. DVD_GUI.addProductToInventory2(new DVD("BMG" , "201","TLC" , 20, 12.00));
  218. DVD_GUI.addProductToInventory2(new DVD("Song", "301","Creed", 10, 13.00));
  219. DVD_GUI.addProductToInventory2(new DVD("BMG", "401","Drake" , 15, 14.00));
  220.  
  221. // sort the dvd by name
  222. DVD_GUI.sortProductInventory2();
  223.  
  224. // Get the GUI to show up on the screen
  225. DVD_GUI.setDefaultCloseOperation( EXIT_ON_CLOSE );
  226. DVD_GUI.pack();
  227. DVD_GUI.setSize(700, 350);
  228. DVD_GUI.setResizable(false);
  229. DVD_GUI.setLocationRelativeTo( null );
  230. DVD_GUI.setVisible(true);
  231.  
  232. return;
  233.  
  234. }
  235. } // End inventoryTest class
May 18 '14 #1
1 2174
chaarmann
785 Expert 512MB
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.getConnection().
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.openConnection() inside URLImageSouce.getConnection() method.
May 20 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

109
by: zaidalin79 | last post by:
I have a java class that goes for another week or so, and I am going to fail if I can't figure out this simple program. I can't get anything to compile to at least get a few points... Here are the...
0
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past...
10
by: mistb2002 | last post by:
I am trying to modify my program to add and delete inventory items. I tried just adding but I am getting mutliple errors help. This is what my program currently looks like // Display The...
4
nexcompac
by: nexcompac | last post by:
Ok, I posted a similar post but now need to jump back into it. Here is what I have been able to clean up. I am using textpad and jbuilder. Still getting used to the whole java world and I am...
9
by: xxplod | last post by:
I am suppose to modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including...
11
by: hamiltongreg | last post by:
I am new to Java and am having problems getting my program to compile correctly. My assignment is as follows; Choose a product that lends itself to an inventory (for example, products at your...
5
by: cblank | last post by:
I'm having some trouble with my inventory program. Its due tom and my teacher is not wanting to help. He keeps giving me a soluction that is not related to my code. I have everything working except...
2
by: pinkf24 | last post by:
I cannot figure out how to add the following: Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.