Complete Java Newbie

Discussion in 'Java' started by Taharka, Oct 8, 2011.

  1. Taharka

    Taharka New Member

    Joined:
    Oct 8, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Between jobs
    Location:
    Lancashire United Kingdom
    I am having a problem with this error below:

    Exception in thread "main"Java.lang.NullPointerException
    at Javax.swing.ImageIcon.<init>(Unknown Source>
    at BT62T63Gui.<init>(BT62T63.java:25)
    at AT63Gui.main(AT63Gui.java:8)

    Its for my first JButton program any advice will be greatly appreciated thanks!!!!
     
  2. ewaldhorn

    ewaldhorn New Member

    Joined:
    Feb 16, 2010
    Messages:
    36
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    Cape Town, South Africa
    Home Page:
    http://www.javak.co.za
    Hi,

    without seeing the code, it's a bit tough to guess what is wrong, but it could be that the icon image you are trying to put on the button could not be located by your application.

    How does the code look that creates the JButton?

    Regards,
    Ewald
     
  3. Taharka

    Taharka New Member

    Joined:
    Oct 8, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Between jobs
    Location:
    Lancashire United Kingdom
    Thanks mate! This is what the code looks like I have been learning Java online its great fun, but can be a bit frustrating. I actually think I learn more when i get things wrong!

    Code:
    //*java tutorial 62 JButton
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    
    public class BT62Gui extends JFrame{//* extends means inheriting everything from JFrame
        
        private JButton reg;
        private JButton custom;
        
        public BT62Gui(){ //* Constructor for the BT62Gui class this also shows a blank screen
            super("The Title");
            setLayout(new FlowLayout()); //* Sets the default layout
            
            reg = new JButton("reg button");//* Displays a button
            add(reg);
            
            Icon b = new ImageIcon(getClass().getResource("b.png")); //* For putting a button on screen.
            Icon x = new ImageIcon(getClass().getResource("b.png")); //* For putting a button on screen.
        }
        
        
    }
     
    Last edited by a moderator: Oct 12, 2011
  4. ewaldhorn

    ewaldhorn New Member

    Joined:
    Feb 16, 2010
    Messages:
    36
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    Cape Town, South Africa
    Home Page:
    http://www.javak.co.za
    The good news is that we all learn when we make mistakes!

    Right, so, when you create the new ImageIcon, and you are running an application, I suggest you change the code to the following:

    Icon b = new ImageIcon("b.png"); //* For putting a button on screen.

    Make sure the "b.png" file is located in the same folder you are going to execute the Java code in, that way, the Java Runtime will be able to locate and load the file.

    Try giving that a shot, Java is a tricky beast, but once you have the basics down, it becomes a great tool.

    Regards,
    Ewald
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice