我在修复代码时遇到了困难。它编译完美,除了我运行applet后得到的异常数量。我不断收到的错误信息是:
线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException 在WholePanel $ Canvas.paintComponent(WholePanel.java:82)
和众多其他来源不明的人。我已经查看了网站上的各种NullPointerException问题,但没有一个问题非常有帮助。
这是我的代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; // To use listener interfaces
import java.util.ArrayList;
public class WholePanel extends JPanel
{
private ArrayList <Rect> rectList;
private ArrayList <Rect> newList;
private boolean flag;
private Color currentColor;
private Canvas canvas;
private JComboBox colorList;
private JButton erase;
private JButton undo;
private JPanel buttonPanel;
private JPanel controlPanel;
private JSplitPane sp;
public WholePanel()
{
//Here we use black to draw a rectangle
currentColor = Color.black;
String colors[] = {"black", "red", "blue", "green", "orange"};
JComboBox<String> colorList = new JComboBox<String>(colors);
ColorListener colorListener = new ColorListener();
colorList.addActionListener(colorListener);
undo = new JButton("Undo");
undo.addActionListener(new ButtonListener());
erase = new JButton("Erase");
erase.addActionListener(new ButtonListener());
buttonPanel = new JPanel(new GridLayout(1,2));
buttonPanel.add(undo);
buttonPanel.add(erase);
controlPanel = new JPanel();
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
controlPanel.add(colorList);
controlPanel.add(buttonPanel);
canvas = new Canvas();
JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, controlPanel, canvas);
setLayout(new BorderLayout());
add(sp);
//To be completed
}
private class Canvas extends JPanel
{
//This method needs to be defined to draw in this panel
private Point startingPoint, endingPoint, movingPoint;
private Rect rectangle;
private int x, y;
public Canvas()
{
PointListener pointListener = new PointListener();
this.addMouseListener(pointListener);
this.addMouseMotionListener(pointListener);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
setBackground(Color.white);
for(int i = 0; i < rectList.size(); i++)
{
rectList.get(i).draw(page);
}
if(endingPoint != null)
{
startingPoint = null;
endingPoint = null;
movingPoint = null;
}
else if(movingPoint != null && x >= 0 && y >= 0)
{
page.setColor(currentColor);
page.drawRect(startingPoint.x, startingPoint.y, x, y);
}
//To be filled
}
} //End of Canvas class
private class PointListener implements MouseListener, MouseMotionListener
{
private Point startingPoint, endingPoint, movingPoint;
private Rect rectangle;
private int x, y;
public void mousePressed(MouseEvent event)
{
//Needs to be filled
startingPoint = event.getPoint();
}
public void mouseReleased(MouseEvent event)
{
//Needs to be filled
endingPoint = event.getPoint();
x = endingPoint.x - startingPoint.x;
y = endingPoint.y - startingPoint.y;
if(endingPoint != null && x >= 0 && y >= 0)
{
Rect rectangle = new Rect(startingPoint.x, startingPoint.y, x, y, currentColor);
rectList.add(rectangle);
}
canvas.repaint();
}
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseDragged(MouseEvent event)
{
//Needs to be filled
movingPoint = event.getPoint();
x = movingPoint.x - startingPoint.x;
y = movingPoint.y - startingPoint.y;
canvas.repaint();
}
public void mouseMoved(MouseEvent event) {}
} //end of PointListener
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
rectList.trimToSize();
rectList.remove(rectList.size() - 1);
repaint();
}
}
private class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
switch(colorList.getSelectedIndex())
{
case 'r':
currentColor = Color.red;
break;
case 'b':
currentColor = Color.blue;
break;
case 'g':
currentColor = Color.green;
break;
case 'o':
currentColor = Color.orange;
break;
default:
currentColor = Color.black;
}
}
}
} // end of Whole Panel Class
答案 0 :(得分:2)
rectList
和newList
都需要初始化
rectList = new ArrayList<Rect>();
newList = new ArrayList<Rect>();
此处另有NPE
来源 - 您shadowing变量colorList
JComboBox<String> colorList = new JComboBox<String>(colors);
应该是
colorList = new JComboBox<String>(colors);