alvitawa
alvitawa

Reputation: 393

JButton setIcon nullPointerException

My 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);

Full 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();

    }

}

When I open the gui I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at darkorbit.GuiBotting.<init>(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...

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

Also failed:

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();

    }

}

Upvotes: 0

Views: 1069

Answers (4)

Mariusz Jamro
Mariusz Jamro

Reputation: 31653

The line:

 ImageIcon img = new ImageIcon(getClass().getResource(System.getProperty("user.dir") + "VisualBot/resources/play.png"));

produces NullPointerException. So probably: System.getProperty("user.dir") returns null as you don't have user.dir defined in your runtime configuration OR the property is defined but it points to incorrect directory and is no file with such name.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168825

ImageIcon img = new ImageIcon(getClass().getResource(
    System.getProperty("user.dir") + "VisualBot/resources/play.png"));

The user.dir is not useful here.

  1. It is an actual file path, whereas the String used by getResource(..) is supposed to be a String that represents a path relative to the class-path.
  2. It is not suffixed by / so it would be necessary to explicitly add it in order to form a valid path.

Try instead with:

ImageIcon img = new ImageIcon(getClass().getResource(
    "/VisualBot/resources/play.png"));

Upvotes: 2

Sage
Sage

Reputation: 15418

System.getProperty("user.dir"): will return the user directory without a name separator at the end : my/example/dir. Instead of directly using \ or /, try adding a separator after it using File.seperator : The system-dependent default name-separator character, represented as a string for convenience.

Upvotes: 1

Robert
Robert

Reputation: 139

It looks like img must be null. Try adding / between user.dir and "VisualBot"

Upvotes: 1

Related Questions