Note that there are some explanatory texts on larger screens.

plurals
  1. POJButton setIcon nullPointerException
    primarykey
    data
    text
    <p>My Code:</p> <pre><code>private JButton stopBotting = new JButton(); ImageIcon img = new ImageIcon(getClass().getResource(System.getProperty("user.dir") + "VisualBot/resources/play.png")); stopBotting.setIcon(img); add(stopBotting); </code></pre> <p>Full Code:</p> <pre><code>package bot; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; public class GuiBotting extends JFrame{ private int controlWidth = 300; private int controlHeight = 78; private JButton startBotting = new JButton(); private JButton stopBotting = new JButton(); private JButton pauseBotting = new JButton(); private GuiBotting(){ setVisible(true); setResizable(false); setSize(controlWidth, controlHeight); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setLayout(null); ImageIcon img = new ImageIcon(getClass().getResource(System.getProperty("user.dir") + "VisualBot/resources/play.png")); stopBotting.setIcon(img); eventStartBotting startBottingEvent = new eventStartBotting(); eventStopBotting stopBottingEvent = new eventStopBotting(); eventPauseBotting pauseBottingEvent = new eventPauseBotting(); startBotting.addActionListener(startBottingEvent); stopBotting.addActionListener(stopBottingEvent); pauseBotting.addActionListener(pauseBottingEvent); startBotting.setBounds(0, 0, 100, 50); stopBotting.setBounds(0, 0, 50, 50); pauseBotting.setBounds(50, 0, 50, 50); add(stopBotting); add(pauseBotting); stopBotting.setVisible(false); pauseBotting.setVisible(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent winEvt) { setVisible(false); dispose(); VisualBot.stopBotting(); GuiPreparation.openGui(); } }); } public class eventStartBotting implements ActionListener{ public void actionPerformed(ActionEvent startBottingActionEvenr){ System.out.println("Starting with botting..."); VisualBot.startBotting(); pauseBotting.setVisible(true); stopBotting.setVisible(true); } } public class eventStopBotting implements ActionListener{ public void actionPerformed(ActionEvent stopBottingActionEvent) { System.out.println("Stopping with botting..."); VisualBot.stopBotting(); dispose(); GuiPreparation.openGui(); } } public class eventPauseBotting implements ActionListener{ public void actionPerformed(ActionEvent pauseBottingActionEvent){ System.out.println("Pausing botting..."); VisualBot.pauseBotting(); stopBotting.setBounds(0, 0, 100, 50); stopBotting.setVisible(false); pauseBotting.setVisible(false); } } public static void openGui(){ GuiBotting guiBotting = new GuiBotting(); } } </code></pre> <p>When I open the gui I get this error:</p> <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.&lt;init&gt;(Unknown Source) at darkorbit.GuiBotting.&lt;init&gt;(GuiBotting.java:31) at darkorbit.GuiBotting.openGui(GuiBotting.java:98) at darkorbit.GuiPreparation$eventStartBotting.actionPerformed(GuiPreparation.java:255) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) etc... </code></pre> <p>I just don't understand, even when I set the full path of the image I get the nullPointerException. I found a lot of answers for this and tried all of them but I havent got it to work. I use Eclipse if that matters</p> <p>Also failed:</p> <pre><code>package bot; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; public class GuiBotting extends JFrame{ private int controlWidth = 300; private int controlHeight = 78; private JButton startBotting = new JButton(); private JButton stopBotting = new JButton(); private JButton pauseBotting = new JButton(); private GuiBotting(){ setVisible(true); setResizable(false); setSize(controlWidth, controlHeight); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setLayout(null); ImageIcon img = new ImageIcon(getClass().getResource(System.getProperty("user.dir") + "/VisualBot/resources/play.png")); stopBotting.setIcon(img); eventStartBotting startBottingEvent = new eventStartBotting(); eventStopBotting stopBottingEvent = new eventStopBotting(); eventPauseBotting pauseBottingEvent = new eventPauseBotting(); startBotting.addActionListener(startBottingEvent); stopBotting.addActionListener(stopBottingEvent); pauseBotting.addActionListener(pauseBottingEvent); startBotting.setBounds(0, 0, 100, 50); stopBotting.setBounds(0, 0, 50, 50); pauseBotting.setBounds(50, 0, 50, 50); add(stopBotting); add(pauseBotting); stopBotting.setVisible(false); pauseBotting.setVisible(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent winEvt) { setVisible(false); dispose(); VisualBot.stopBotting(); GuiPreparation.openGui(); } }); } public class eventStartBotting implements ActionListener{ public void actionPerformed(ActionEvent startBottingActionEvenr){ System.out.println("Starting with botting..."); VisualBot.startBotting(); pauseBotting.setVisible(true); stopBotting.setVisible(true); } } public class eventStopBotting implements ActionListener{ public void actionPerformed(ActionEvent stopBottingActionEvent) { System.out.println("Stopping with botting..."); VisualBot.stopBotting(); dispose(); GuiPreparation.openGui(); } } public class eventPauseBotting implements ActionListener{ public void actionPerformed(ActionEvent pauseBottingActionEvent){ System.out.println("Pausing botting..."); VisualBot.pauseBotting(); stopBotting.setBounds(0, 0, 100, 50); stopBotting.setVisible(false); pauseBotting.setVisible(false); } } public static void openGui(){ GuiBotting guiBotting = new GuiBotting(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload