1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Complete Java Newbie

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

  1. Taharka

    Taharka New Member

    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


    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?

  3. Taharka

    Taharka New Member

    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!

    //*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
            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

    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.


Share This Page