0

OK, first thing I want to say is: yes, I know there are a number of similar questions on setting a background image in java. However, my situation is a bit different since I got handed a Java project that was started by someone else. I believe that the previous programmer was fairly new to Java GUI programming and I myself am very new to Java GUI programming. Here is what the program will have to do:

  • Set up a background image which is a network map of our clients
  • Place bandwidth maps to respective clients which is acquired by a url
  • Refresh bandwidth graphs every 5 seconds or so

The only thing that the previous programmer did was set up 2 classes, one being the main and one setting up the background image which he had as a monkey from a url

Now, I decided to use MigLayout to organize a grid where I will place the bandwidth images in the correct arrangement. I was having trouble with that so I decided to start a little smaller and change the monkey background to the network map. I added the map jpg to the src library and thought that if I switched URL to File it would work. It didn't and I have been stuck trying to switch things around to get it to work but no cigar.

Below is the code for the setting up a background. This is my first time trying to post code I hope it works:... well I tried several times several ways and googled it several time but posting my code isnt working so I took a screen shot. Nothing is working for me today. enter image description here

The errors I get are:

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at bwMonBackground.<init>(bwMonBackground.java:21)
    at bwMonBackgroundMain.main(bwMonBackgroundMain.java:7)

EDIT: I uncommented out line 18. Here is the main:

public class bwMonBackgroundMain extends bwMonBackground{
      public static void main( String[] args )
        {
            bwMonBackground frame = new bwMonBackground();
            migLayout testing = new migLayout();
            testing.createLayout();
            frame.setVisible(true);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();

        }
}

EDIT 2: once i uncommented out line 18 in bwMonBackground.java I get the following errors:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(Unknown Source)
    at bwMonBackground.<init>(bwMonBackground.java:15)
    at bwMonBackgroundMain.main(bwMonBackgroundMain.java:7)
Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at bwMonBackground.<init>(bwMonBackground.java:20)
    at bwMonBackgroundMain.main(bwMonBackgroundMain.java:7)
exit_1
  • 1,240
  • 4
  • 13
  • 32
  • The exception is because something threw an IOException and you didn't even print the stacktrace. So `image` is still null when you create your label. See what's causing your IOException first. – Thomas Aug 07 '12 at 19:56
  • 1
    Something tells me that it is FileNotFoundException. – evg Aug 07 '12 at 20:03

4 Answers4

2

Basically, the error is saying that the File can not be found (you are specifying a null reference to the ImageIcon constructor). Make sure it exists in the root location of the application's execution context.

Now, to your actual problem.

You will want to setup a custom Component (such as a JPanel) and override the paintComponent method

Take a look at

You may also want to look at

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

I bet you have IOException and you just do nothing in catch, thats why image is null. And JLabel with icon is not really a background image, its icon and you cant place elements in it. So you need to read this: java swing background image

It contains pretty good answer with code sample, just change URL to a valid image and launch.

Community
  • 1
  • 1
evg
  • 1,308
  • 1
  • 7
  • 12
0

Can you show the source of bwMonBackgroundMain.java? Right now it just looks like it can't find your image file you are trying to add to the label

Spencer H
  • 450
  • 1
  • 6
  • 17
0

An IOException is being thrown and you cant see if because the stack trace is commented out. I assume that the IO exception is being thrown when attempting to read the file. Try printing the stack trace and then solving it from there.

Zoop
  • 646
  • 5
  • 7