JButton setIcon nullPointerException

时间:2013-11-16 18:59:49

标签: java image swing embedded-resource

我的代码:

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

add(stopBotting);

完整代码:

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

    }

}

当我打开gui时,我收到此错误:

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

我只是不明白,即使我设置了图像的完整路径,我也得到了nullPointerException。我找到了很多答案,并尝试了所有这些,但我还没有得到它的工作。如果重要,我会使用Eclipse

也失败了:

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

    }

}

4 个答案:

答案 0 :(得分:2)

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

user.dir在这里没用。

  1. 这是一个实际的文件路径,而String使用的getResource(..)应该是String,表示相对于类路径的路径。
  2. /不加后缀,因此有必要明确添加它以形成有效路径。
  3. 请改为使用:

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

答案 1 :(得分:1)

看起来img必须为null。尝试在/和“VisualBot”之间添加user.dir

答案 2 :(得分:1)

该行:

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

生成NullPointerException。所以可能:System.getProperty("user.dir")返回null,因为您没有在运行时配置中定义user.dir或者属性已定义,但它指向不正确的目录,并且没有具有此名称的文件。

答案 3 :(得分:1)

System.getProperty("user.dir"):将返回末尾没有名称分隔符的用户目录:my/example/dir。不要直接使用\/,而是在使用File.seperator后添加分隔符:系统相关的默认名称分隔符,为方便起见,表示为字符串。