线程“AWT-EventQueue-1”中的异常 java.lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5086559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
提问by Kalvin
Hey guys I get the errors
嘿伙计们我得到了错误
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at P6.itemStateChanged(P6.java:215)
at javax.swing.AbstractButton.fireItemStateChanged(AbstractButton.java:2
023)
at javax.swing.AbstractButton$Handler.itemStateChanged(AbstractButton.ja
va:2325)
at javax.swing.DefaultButtonModel.fireItemStateChanged(DefaultButtonMode
l.java:440)
at javax.swing.JToggleButton$ToggleButtonModel.setSelected(JToggleButton
.java:255)
at javax.swing.ButtonGroup.setSelected(ButtonGroup.java:147)
at javax.swing.JToggleButton$ToggleButtonModel.setSelected(JToggleButton
.java:237)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.
java:272)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
In
在
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
public class P6 extends JApplet implements MouseListener, MouseMotionListener, ItemListener
{
private final int RED = 1;
private final int ORANGE = 2;
private final int YELLOW = 3;
private final int GREEN = 4;
private final int BLUE = 5;
private final int CYAN = 6;
private final int PINK = 7;
private final int MAGENTA =8;
private final int BLACK = 9;
private final int ARC = 1;
private final int LINE = 2;
private final int OVAL = 3;
private final int RECT = 4;
private final int POLY = 5;
private String[] colorNames = { "Red", "Orange", "Yellow", "Green", "Blue",
"Cyan", "Pink", "Magenta", "Black"};
private JComboBox colorComboBox;
private JRadioButton jrbArc;
private JRadioButton jrbLine;
private JRadioButton jrbOval;
private JRadioButton jrbRect;
private JRadioButton jrbPolygon;
private int color = 0;
private int shape = 0;
private int x1;
private int y1;
private int x2;
private int y2;
public void init()
{
Container c = getContentPane();
c.setLayout( new FlowLayout() );
c.setBackground( Color.orange );
JComboBox colorComboBox = new JComboBox(colorNames);
colorComboBox.addItemListener(this);
JRadioButton jrbArc = new JRadioButton("Arc");
jrbArc.addItemListener(this);
JRadioButton jrbLine = new JRadioButton("Line");
jrbLine.addItemListener(this);
JRadioButton jrbOval = new JRadioButton("Oval");
jrbOval.addItemListener(this);
JRadioButton jrbRect = new JRadioButton("Rectangle");
jrbRect.addItemListener(this);
JRadioButton jrbPolygon = new JRadioButton("Polygon");
jrbPolygon.addItemListener(this);
ButtonGroup btg = new ButtonGroup();
btg.add(jrbArc);
c.add(jrbArc);
btg.add(jrbLine);
c.add(jrbLine);
btg.add(jrbOval);
c.add(jrbOval);
btg.add(jrbRect);
c.add(jrbRect);
btg.add(jrbPolygon);
c.add(jrbPolygon);
c.add(colorComboBox);
addMouseMotionListener(this);
addMouseListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.white);
g.fillRect(20, 50, 500, 500);
g.setFont( new Font("Serif", Font.ITALIC+Font.BOLD, 18));
g.setColor(Color.black);
g.drawString("Start Here!", 10, 50);
g.drawString("End Here!", 510, 560);
switch (this.color)
{
case 1:
g.setColor(Color.red);
break;
case 2:
g.setColor(Color.orange);
break;
case 3:
g.setColor(Color.yellow);
break;
case 4:
g.setColor(Color.green);
break;
case 5:
g.setColor(Color.blue);
break;
case 6:
g.setColor(Color.cyan);
break;
case 7:
g.setColor(Color.pink);
break;
case 8:
g.setColor(Color.magenta);
break;
case 9:
g.setColor(Color.black);
break;
}
switch (shape)
{
case 1:
g.fillArc(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1, 0, 90);
g.fillArc(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1, 180,90);
break;
case 2:
g.drawLine(this.x1, this.y1, this.x2, this.y2);
break;
case 3:
g.fillOval(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1);
break;
case 4:
g.fillRect(this.x1, this.y1, this.x2 - this.x1, this.y2 - this.y1);
break;
case 5:
int[] arr1 = { this.x1, this.x2, this.x1, this.x2, this.x1};
int[] arr2 = { this.y1, this.y1, this.y2 ,this.y2, this.y1};
g.fillPolygon(arr1, arr2, 5);
break;
}
}
public void mousePressed( MouseEvent e)
{
this.x1 = e.getX();
this.y1 = e.getY();
}
public void mouseDragged( MouseEvent e)
{
this.x2 = e.getX();
this.y2 = e.getY();
}
public void mouseReleased( MouseEvent e)
{
this.x2 = e.getX();
this.y2 = e.getY();
}
public void mouseClicked( MouseEvent e)
{
}
public void mouseEntered( MouseEvent e)
{
}
public void mouseExited( MouseEvent e)
{
}
public void mouseMoved( MouseEvent e)
{
showStatus( " (" + e.getX() + ", " + e.getY() +")");
}
public void itemStateChanged(ItemEvent i)
{
if(this.jrbArc.isSelected())
this.shape = ARC;
else if( this.jrbLine.isSelected())
this.shape = LINE;
else if( this.jrbOval.isSelected())
this.shape = OVAL;
else if( this.jrbRect.isSelected())
this.shape = RECT;
else if( this.jrbPolygon.isSelected())
this.shape = POLY;
this.color = ( this.colorComboBox.getSelectedIndex() + 1 );
repaint();
}
}
I've been trying to figure this out, but no luck. Any help please?
我一直试图弄清楚这一点,但没有运气。请问有什么帮助吗?
Line 215 is:
第 215 行是:
if(this.jrbArc.isSelected())
回答by Bala R
instead of
代替
JRadioButton jrbArc = new JRadioButton("Arc");
you need
你需要
jrbArc = new JRadioButton("Arc");
And similar stuff for other controls too.
其他控件也有类似的东西。
回答by corsiKa
Always go to the first error/line mentioned. In this case, it's
始终转到提到的第一个错误/行。在这种情况下,它是
P6.java:215
What do you find at this line? Do a null check, and trace it back.
你在这条线上发现了什么?做一个空检查,并追溯它。
回答by Jochen Bedersdorfer
You have a naming conflict: jrbArc is a field and used as a local variable. You are not actually setting the field, thus the NullPointerException
您有命名冲突:jrbArc 是一个字段并用作局部变量。您实际上并未设置该字段,因此 NullPointerException