Results 1 to 14 of 14
  1. #1
    scr106 is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default Background Image throws a null pointer exception and does not run

    logo.setIcon(resourceMap.getIcon("logo.icon")); // NOI18N
    logo.setName("logo"); // NOI18N

    is trying to access an image from a resources folder.

    The folder is com.test.suzy.dct.gui.rescources and contains the image as a png called logo.png

    The main package is called Gui and I am trying to create a panel type wizard- so there are five panels with five different classes. I am trying to get this code to work in a panel called Initial.java which is where this code lies- but when calling the main method it does not run and throw a null pointer at this line of code.

    Please any suggestions would help!! I am really stuck!!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,408
    Rep Power
    25

    Default

    calling the main method it does not run and throw a null pointer at this line of code.
    What variable is null?
    The posted code uses the variable: logo. Is it null?
    Or is the value returned by the resourceMap() method null?
    Or does getIcon() return a null?

    Check that the path and file names are correct.

  3. #3
    scr106 is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    The value returned by the method is null- I have a background image and I can see where it is and the resource map points to the correct location but the image does not display in output.

    Is there another way of loaing an image to a background in swing- another component instead of a label? any other advice on how to have the image would be great.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,408
    Rep Power
    25

    Default

    There must be something wrong with your paths or names. I haven't used .icon files. Try using a .jpg or .png

  5. #5
    scr106 is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    the file is a .png file- and the paths are exact?! is there any other way of having a background image?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,408
    Rep Power
    25

    Default

    Java Code:
    getIcon("logo.icon")
    Where is the extension: .png used?
    What does getIcon() return?

    Can you make a small complete program that compiles and executes and demonstrates your problem?
    Too much is hidden by your few lines of code.

  7. #7
    scr106 is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    it is taken from a resources folder which then has a folder called results.properties in this folder is the logo.png file

    logo.setIcon(resourceMap.getIcon("logo.icon")); // NOI18N
    logo.setName("logo"); // NOI18N
    logo.setBounds(480, 420, 210, 60);
    jLayer.add(logo, javax.swing.JLayeredPane.DEFAULT_LAYER);

    bg.setHorizontalAlignment(javax.swing.SwingConstan ts.RIGHT);
    bg.setIcon(resourceMap.getIcon("bg.icon")); // NOI18N
    bg.setVerticalAlignment(javax.swing.SwingConstants .TOP);
    bg.setName("jLabel1"); // NOI18N
    bg.setBounds(0, 0, 730, 500);
    jLayer.add(bg, javax.swing.JLayeredPane.DEFAULT_LAYER);

    I have two images in that folder one called logo and one called bg. Neither of them show.

    I cant really display more code-

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,408
    Rep Power
    25

    Default

    No way to make any suggestions to help you with so little code.
    For example what is returned by getIcon()?
    What is resourceMap?

    This statement works in my code:
    Java Code:
    	JButton	lb1 = new JButton( new ImageIcon( " images/middle.gif" ));  // ignore space in ""s 
           ...
    	lb1.setIcon(new ImageIcon( " images/right.gif" ));

  9. #9
    scr106 is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    where do you save the image to get it? and have you done this by dragging a swing component "button" onto a new panel- becasue when i do this- it generates the code which i cannot modify?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,408
    Rep Power
    25

    Default

    where do you save the image to get it
    My posted code does not save the image. It reads it directly from disk.

    have you done this by dragging a swing component "button" onto a new panel
    No. My posted code was from a simple program.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Quote Originally Posted by scr106 View Post
    where do you save the image to get it? and have you done this by dragging a swing component "button" onto a new panel- becasue when i do this- it generates the code which i cannot modify?
    Possibly don't use the gui builder then.
    Are you well-versed in Swing and the GUI builder?
    If not then stay well clear.

  12. #12
    scr106 is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    No I am not at all- this is my first time using it- but i have to stay using it!

    It has turned into a mess!

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    Which is waht happens with the GUI builder.
    I hate using it, and I understand Swing...

    It is not a newbie tool in any shape or form. The sooner you ditch it the better you'll do.

  14. #14
    scr106 is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Storing an image file into DB(Null pointer exception)
    By mithcool in forum Java Servlet
    Replies: 5
    Last Post: 06-27-2011, 05:30 PM
  2. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  3. Null pointer exception
    By izzy in forum New To Java
    Replies: 5
    Last Post: 03-22-2010, 05:19 PM
  4. Replies: 6
    Last Post: 09-03-2009, 11:40 AM
  5. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 02:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •