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!!!!
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
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. } }
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