Why does my code produce errors?

psyche

New Member
\[code\]* A simple panel for testing various parts of our game. * This is not part of the game. It's just for testing. */package game;import java.awt.*;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.util.Scanner;import javax.imageio.ImageIO;import javax.swing.JPanel;/** * A simple panel for testing various parts of our game. * This is not part of the game. It's just for testing. */public class TestPanel extends JPanel{ private static final long serialVersionUID = 1L; // Ignore this - It's just to get rid of a warning. // Instance variable(s). private Image backdrop; /** * Constructor - loads a background image */ public TestPanel () { try { ClassLoader myLoader = this.getClass().getClassLoader(); InputStream imageStream = myLoader.getResourceAsStream("resources/path_1.jpg"); backdrop = ImageIO.read(imageStream); // You will uncomment these lines when you need to read a text file. InputStream pointStream = myLoader.getResourceAsStream("resources/ path_1.txt"); Scanner s = new Scanner (pointStream); } catch (IOException e) { System.out.println ("Could not load: " + e); } } /** * This paint meethod draws the background image anchored * in the upper-left corner of the panel. */ public void paintComponent (Graphics g) { g.drawImage(backdrop, 0, 0, null); } /* Override the functions that report this panel's size * to its enclosing container. */ public Dimension getMinimumSize() { return new Dimension (600, 600); } public Dimension getMaximumSize() { return getMinimumSize(); } public Dimension getPreferredSize() { return getMinimumSize(); }}\[/code\]This code is aimed towards a videogame assignment I am working on for my Java course. This class is only used to test our code out. In the direction for the assignment, I was told to put code that is present within the try block, as show above. Apparently, the code should open a JPEG image that I have within a folder in my workspace. However, when I try the code, it only states:\[code\]Exception in thread "main" java.lang.NullPointerException at java.io.Reader.<init>(Unknown Source) at java.io.InputStreamReader.<init>(Unknown Source) at java.util.Scanner.<init>(Unknown Source) at game.TestPanel.<init>(TestPanel.java:43) at game.TestApplication.main(TestApplication.java:24)\[/code\]I am not fully clear on what inputStream and classLoaders do. So, if you have any basic information on either, that would be great. Also, I know the other methods below the constructor method have no code within them. The directions for my assignment have not stated what I should input into these methods.\[code\]enter code hereenter code here\[/code\]
 
Back
Top