Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By kneitzel

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    GenierO is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    I'm Using Ubuntu distribution & NetBeans 8.0
    After searching, I couldn't deal with this problem !

    Java Code:
    
    package frame5;
    import java.awt.Color;
    import javax.swing.*;
    
    public class Frame5 extends JFrame{
    
        public Frame5(){
            ImageIcon icon = new ImageIcon(getClass().getResource("/frame5/fire.jpg"));
            ImageIcon icon1 = new ImageIcon(getClass().getResource("/frame5/lucianaa.jpg"));
            
            JButton b1 = new JButton();
            b1.setBounds(50, 50, 500, 300);
            b1.setIcon(icon);
            
            JLabel l1 = new JLabel();
            l1.setBounds(50, 350, 500, 200);
            l1.setIcon(icon1);
            
            setLayout(null);
            
            getContentPane().add(b1);
            getContentPane().add(l1);
            getContentPane().setBackground(Color.BLACK);
        }
        public static void main(String[] args) {
            
            Frame5 window = new Frame5();
            window.setSize(800, 600);
            window.setLocationRelativeTo(window);
            window.setVisible(true);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        
    }
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
    	at frame5.Frame5.<init>(Frame5.java:9)
    	at frame5.Frame5.main(Frame5.java:27)
    Java Result: 1

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Hi,
    the getResoure call can return null if the given resource wasn't found.
    - So the solution is to make sure that the resource is available
    - You might optimize the code by splitting the getResource call and include if check if it is null (So you get a clean message if such a problem happens).

    With kind regards,

    Konrad
    GenierO likes this.

  3. #3
    GenierO is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Hi,
    Thanks for Replying :)

    I found that I wrote "lucianaa" instead of "luciana" ^_^
    Last edited by GenierO; 05-10-2014 at 01:20 PM.

Similar Threads

  1. Replies: 2
    Last Post: 09-30-2013, 07:23 AM
  2. Replies: 6
    Last Post: 04-19-2012, 08:10 PM
  3. Replies: 5
    Last Post: 12-21-2011, 07:44 PM
  4. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  5. Replies: 1
    Last Post: 07-16-2009, 11:35 AM

Posting Permissions

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