【发布时间】:2019-05-04 03:12:35
【问题描述】:
每次我运行我的代码时,我都会在代码顶部列出这个错误。
我的程序没有颜色改变工作正常,但自从我添加了改变数字颜色的能力后,我无法让它在我的一生中工作。任何帮助表示赞赏。这个程序对我的 Java 类来说是个问题。
java.lang.NullPointerException
at ColorChange.init(ColorChange.java:29)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)"
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorChange extends JApplet implements ItemListener,
ActionListener
{
private JRadioButton blackRb, blueRb, orangeRb, greenRb, yellowRb, redRb,
pinkRb, grayRb;
private Color currentColor=Color.magenta;
private ButtonGroup colorSelect;
private JTextField numberTF;
private JLabel label;
private JButton okB;
private int num=0;
public void init() {
Container c=getContentPane();
c.setLayout(null);
setSize(500,300);
label=new JLabel("Enter number");
label.setLocation(200,40);
label.setSize(100,30);
numberTF=new JTextField(1);
numberTF.setLocation(300,40);
okB.setLocation(400,40);
okB.setSize(50,30);
okB.addActionListener(this);
c.add(label);
c.add(numberTF);
c.add(okB);
blackRb=new JRadioButton("Black");
blueRb=new JRadioButton("Blue");
orangeRb=new JRadioButton("Orange");
greenRb=new JRadioButton("Green");
yellowRb=new JRadioButton("Yellow");
redRb=new JRadioButton("Red");
pinkRb=new JRadioButton("Pink");
grayRb=new JRadioButton("Grey");
blackRb.setSize(100,30);
blueRb.setSize(100,30);
orangeRb.setSize(100,30);
greenRb.setSize(100,30);
yellowRb.setSize(100,30);
redRb.setSize(100,30);
pinkRb.setSize(100,30);
grayRb.setSize(100,30);
blackRb.setLocation(200,70);
blueRb.setLocation(200,100);
orangeRb.setLocation(200,130);
greenRb.setLocation(200,160);
yellowRb.setLocation(400,70);
redRb.setLocation(400,100);
pinkRb.setLocation(400,130);
grayRb.setLocation(400,160);
blackRb.addActionListener(this);
blueRb.addActionListener(this);
orangeRb.addActionListener(this);
greenRb.addActionListener(this);
yellowRb.addActionListener(this);
redRb.addActionListener(this);
pinkRb.addActionListener(this);
grayRb.addActionListener(this);
c.add(blackRb);
c.add(blueRb);
c.add(orangeRb);
c.add(greenRb);
c.add(yellowRb);
c.add(redRb);
c.add(pinkRb);
c.add(grayRb);
colorSelect=new ButtonGroup();
colorSelect.add(blackRb);
colorSelect.add(blueRb);
colorSelect.add(orangeRb);
colorSelect.add(greenRb);
colorSelect.add(yellowRb);
colorSelect.add(redRb);
colorSelect.add(pinkRb);
colorSelect.add(grayRb);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(currentColor);
switch (num)
{
case 0:
g.fillRect(50,25,125,25);
g.fillRect(50,50,25,175);
g.fillRect(50,200,125,25);
g.fillRect(150,25,25,175);
break;
case 1:
g.fillRect(75,25,75,25);
g.fillRect(100,50,50,125);
g.fillRect(50,175,150,25);
break;
case 2:
g.fillRect(50,25,125,25);
g.fillRect(150,50,25,50);
g.fillRect(50,100,125,25);
g.fillRect(50,175,125,25);
g.fillRect(50,125,25,50);
break;
case 3:
g.fillRect(150,50,25,175);
g.fillRect(50,50,100,25);
g.fillRect(50,125,100,25);
g.fillRect(50,200,100,25);
break;
case 4:
g.fillRect(50,25,25,75);
g.fillRect(50,100,100,25);
g.fillRect(150,25,25,175);
break;
case 5:
g.fillRect(50,25,125,25);
g.fillRect(50,50,25,50);
g.fillRect(50,100,125,25);
g.fillRect(50,175,125,25);
g.fillRect(150,125,25,50);
break;
case 6:
g.fillRect(50,25,125,25);
g.fillRect(50,50,25,50);
g.fillRect(50,100,125,25);
g.fillRect(50,175,125,25);
g.fillRect(150,125,25,50);
g.fillRect(50,125,25,50);
break;
case 7:
g.fillRect(50,25,125,25);
g.fillRect(150,50,25,150);
break;
case 8:
g.fillRect(50,25,125,25);
g.fillRect(50,50,25,50);
g.fillRect(50,100,125,25);
g.fillRect(50,175,125,25);
g.fillRect(150,125,25,50);
g.fillRect(50,125,25,50);
g.fillRect(150,50,25,50);
break;
case 9:
default:
g.fillRect(50,25,125,25);
g.fillRect(50,50,25,50);
g.fillRect(50,100,125,25);
g.fillRect(50,175,125,25);
g.fillRect(150,125,25,50);
g.fillRect(150,50,25,50);
break;
}
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==blackRb)
currentColor=Color.black;
else if(e.getSource()==blueRb)
currentColor=Color.blue;
else if(e.getSource()==orangeRb)
currentColor=Color.orange;
else if(e.getSource()==greenRb)
currentColor=Color.green;
else if(e.getSource()==yellowRb)
currentColor=Color.yellow;
else if(e.getSource()==redRb)
currentColor=Color.red;
else if(e.getSource()==pinkRb)
currentColor=Color.pink;
else if(e.getSource()==grayRb)
currentColor=Color.gray;
repaint();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Ok"))
num=Integer.parseInt(numberTF.getText());
repaint();
}
}
【问题讨论】:
-
1) 见What is a stack trace, and how can I use it to debug my application errors? & What is a Null Pointer Exception, and how do I fix it? 2) 使用逻辑一致的缩进代码行和块的形式。缩进是为了让代码流更容易理解!大多数 IDE 都有专门用于格式化代码的键盘快捷键。 3) Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。 ..
-
.. 因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 4) 见Java Plugin support deprecated 和Moving to a Plugin-Free Web。
-
BTW - 代码的第 29 行似乎是
label.setSize(100,30);,这对于NullPointerException来说似乎不合逻辑,因为上一行的label.setLocation(200,40);有效。您确定显示的 exact 代码会产生此错误吗?例如,它没有遗漏package声明?
标签: java nullpointerexception japplet