Java Swing中的空指针异常[关闭]
Posted
技术标签:
【中文标题】Java Swing中的空指针异常[关闭]【英文标题】:Null Pointer Exception in Java Swing [closed] 【发布时间】:2020-06-21 15:23:13 【问题描述】:每当我尝试使用我在下面的代码中写入的任何按钮时,我都会不断收到此错误消息。奇怪的是我正在查看一段类似的代码,它并没有出现在那里,但我无法区分。
代码如下:
主窗口类
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class FinalPaintWindow extends JFrame
public static final int CANVAS_WIDTH = 640;
public static final int CANVAS_HEIGHT = 480;
***ActionListener actionListener = new ActionListener()
FinalDrawSpace finalDrawSpace = new FinalDrawSpace();
public void actionPerformed(ActionEvent e)
if (e.getSource() == clearBtn)
finalDrawSpace.clear();
else if (e.getSource() == blackBtn)
finalDrawSpace.black();
else if (e.getSource() == rectangleBtn)
finalDrawSpace.rectangle();
;***
private DrawCanvas FinalDrawSpace;
JButton clearBtn, blackBtn, rectangleBtn;
public FinalPaintWindow()
FinalDrawSpace = new DrawCanvas();
FinalDrawSpace.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
getContentPane().addComponentListener(new ComponentAdapter()
public void componentResized(ComponentEvent e)
Component c = (Component)e.getSource();
);
Container cp = getContentPane();
cp.add(FinalDrawSpace, BorderLayout.CENTER);
JPanel controls = new JPanel();
clearBtn = new JButton("Clear");
clearBtn.addActionListener(actionListener);
blackBtn = new JButton("Black");
blackBtn.addActionListener(actionListener);
rectangleBtn = new JButton("Rectangle");
rectangleBtn.addActionListener(actionListener);
controls.add(clearBtn);
controls.add(blackBtn);
controls.add(rectangleBtn);
cp.add(controls, BorderLayout.EAST);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setTitle("Final Paint");
setVisible(true);
private class DrawCanvas extends JPanel
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
setBackground(Color.WHITE);
public static void main (String[] args)
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new FinalPaintWindow();
);
图形类
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class FinalDrawSpace extends JComponent
public Image image;
private Graphics2D graphics;
private int newX;
private int newY;
private int oldX;
private int oldY;
public FinalDrawSpace()
setDoubleBuffered(false);
addMouseListener(new MouseAdapter()
public void mousePressed(MouseEvent e)
oldX = e.getX();
oldY = e.getY();
);
addMouseMotionListener(new MouseMotionAdapter()
public void mouseDragged(MouseEvent e)
newX = e.getX();
newY = e.getY();
if (graphics != null)
graphics.drawLine(oldX, oldY, newX, newY);
);
protected void paintComponent(Graphics g)
if (image == null)
image = createImage(getSize().width, getSize().height);
graphics = (Graphics2D) image.getGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
g.drawImage(image, 0, 0, null);
***public void clear()
graphics.setPaint(Color.white);
graphics.fillRect(0, 0, getSize().width, getSize().height);
graphics.setPaint(Color.black);
repaint();
public void rectangle()
graphics.drawRect(oldX, oldY, newX, newY);
public void black()
graphics.setPaint(Color.black);
***
带星号的区域是抛出 NullPointerException 的区域,我只是想不出办法解决它。有人能帮忙吗?
【问题讨论】:
你需要添加异常。 尽快发布堆栈跟踪以获得更好的帮助。 【参考方案1】:***ActionListener actionListener = new ActionListener()
FinalDrawSpace finalDrawSpace = new FinalDrawSpace();
问题在于,在 ActionListener 中,您总是会创建 FinalDrawSpace 类的新实例。由于您直接调用 clear() 方法,因此不会初始化“图形”变量,因为从不调用 paintComponent() 方法。
摆脱:
//FinalDrawSpace finalDrawSpace = new FinalDrawSpace();
现在 ActionListener 代码将引用 FinalPaintWindow 类的“finalDrawSapce”变量。
还有:
private DrawCanvas FinalDrawSpace;
应该是:
private drawCanvas finalDrawSpace;
因为变量名不应以大写字符开头。
【讨论】:
以上是关于Java Swing中的空指针异常[关闭]的主要内容,如果未能解决你的问题,请参考以下文章