#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    Cant load the IMAGE


    what is wrong in my code? it compiles but then when i run it, it gives errors

    this is going to be the main class that i will use to access my sub_classes who contains the components to be added in this class

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class main_pane
    {   
        JPanel rootpane;
        
        public JPanel contentPANE()
        {
            rootpane = new JPanel();
            rootpane.setLayout(null);
            rootpane.add(components.home_bg());
            
            return rootpane;
        }
        
        
        private static void createGUI()
        {
            JFrame frame = new JFrame("[AI] [CS32FB1]");
            frame.setSize(600,400);
            frame.setLocationRelativeTo(null);
            frame.setResizable(true);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            main_pane contentpane = new main_pane();
            frame.setContentPane(contentpane.contentPANE());
        }
       
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    createGUI();
                    
                }
            });
        }
    }

    this class below who has no main method is my sub_class that contains the components that i will access using my main_class

    Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class components
    {    
        static ClassLoader loader1;
        static ImageIcon home;
        static JLabel BG_HOME;
        
        
        static ClassLoader loader2;
        static ImageIcon translate;
        
        
        static ClassLoader loader3;
        static ImageIcon search;
        
        public static JLabel home_bg()
        {
            loader1 = components.class.getClassLoader();
            home = new ImageIcon(loader1.getResource("home.jpg"));
           
            BG_HOME = new JLabel(home);
            BG_HOME.setLayout(null);
            BG_HOME.setBounds(0,0,600,400);
            return BG_HOME;
        }
    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    When you get an error message, it will be easier to help if you post the exact message.

    That said, have you verified that all classes and images are placed in the same folder?
    Depending on whether you use an IDE to compile, class files can have a different location than the java files.
    Last edited by MrFujin; February 13th, 2014 at 12:36 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by MrFujin
    When you get an error message, it will be easier to help if you post the exact message.

    That said, have you verified that all classes and images are placed in the same folder?
    Depending on whether you use an IDE to compile, class files can have a different location than the java files.


    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    	at components.home_bg(components.java:21)
    	at main_pane.contentPANE(main_pane.java:14)
    	at main_pane.createGUI(main_pane.java:29)
    	at main_pane.access$000(main_pane.java:6)
    	at main_pane$1.run(main_pane.java:38)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:694)
    	at java.awt.EventQueue$3.run(EventQueue.java:692)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    Sounds like that getResource returns null, i.e. can't find the image.

    Did you verified that the image and classes are in same folder?

IMN logo majestic logo threadwatch logo seochat tools logo