我是StackOverFlow的新手。我有几年Java经验,但我似乎无法在此代码中找到触发Null指针异常的行。进口都已准备就绪。
public class BallShooter extends JFrame{
private JFrame ballshooter;
private BallShooter bs;
private MenuPanel mp;
private MainPanel mep;
private GamePanel gp;
private Balls balls;
private CardLayout card;
private int[] leaderboard;
private boolean ballHitWall;
public BallShooter()
{
mep = new MainPanel();
mp = new MenuPanel();
gp = new GamePanel();
ballshooter = new JFrame();
ballshooter.setLocation(0, 0);
ballshooter.setSize(800, 700);
ballshooter.setDefaultCloseOperation(EXIT_ON_CLOSE);
ballshooter.setBackground(Color.GRAY);
ballshooter.setResizable(false);
ballshooter.getContentPane().add(mep);
ballshooter.setVisible(true);
card = (CardLayout)(mep.getLayout());
}
public static void main(String [] args)
{
BallShooter balls = new BallShooter();
}
class MainPanel extends JPanel
{
public MainPanel()
{
setSize(800,700);
setVisible(true);
setBackground(Color.GRAY);
setLayout(new CardLayout());
add(mp); <-- line 52
add(gp);
}
}
class MenuPanel extends JPanel implements ActionListener
{
private JButton startGame;
private JButton leaderboard;
private JButton instructions;
public MenuPanel()
{
setLayout(null);
setSize(800,700);
setBackground(Color.GRAY);
startGame = new JButton("Start the GAME.");
leaderboard = new JButton("Go to LEADERBOARD.");
instructions = new JButton("Instructions.");
startGame.addActionListener(this);
leaderboard.addActionListener(this);
instructions.addActionListener(this);
startGame.setBounds(300,100,200,150);
leaderboard.setBounds(300,250,200,150);
instructions.setBounds(300,400,200,150);
add(startGame);
add(leaderboard);
add(instructions);
}
public void actionPerformed(ActionEvent e) {
String in = e.getActionCommand();
if(in.equals("Start the GAME."))
{
card.next(mep);
}
}
}
class GamePanel extends JPanel implements ActionListener
{
private JButton stats;
private int pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9,pos10,pos11,pos12,pos13,pos14,pos15,pos16,pos17,pos18,pos19,pos20 ;
private boolean onePos,twoPos,threePos,fourPos,fivePos,sixPos,sevenPos,eightPos,ninePos,tenPos,elevenPos,twelvePos,thirteenPos,fourteenPos,fifteenPos,sixteenPos,seventeenPos,eighteenPos,ninteenPos,twentyPos = true;
private int x,y;
public GamePanel()
{
balls = new Balls();
onePos = true;
setSize(800,700);
setBackground(Color.GRAY);
setLayout(null);
if(onePos)
{
pos1 = 1;
x = 100;
y = 100;
balls.setBounds(x,y, 50,50);
gp.add(balls);
}
}
public void actionPerformed(ActionEvent e) {}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.WHITE);
g.drawRect(100, 100, 600, 500);
}
}
class Balls extends JPanel {
private int color;
private int vX, vY;
private int ballW = 50, ballH = 50;
private int x,y;
public Balls()
{
setSize(50,50);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
color = (int)(Math.random()*4+1);
if(color == 1)
{
g.setColor(Color.YELLOW);
g.drawOval(0, 0, ballW, ballH);
}
else if(color == 2)
{
g.setColor(Color.GREEN);
g.drawOval(0, 0, ballW, ballH);
}
else if(color == 3)
{
g.setColor(Color.RED);
g.drawOval(0, 0, ballW, ballH);
}
else if(color == 4)
{
g.setColor(Color.BLUE);
g.drawOval(0, 0, ballW, ballH);
}
}
}
}
我不知道发生了什么。它只是飞过我的脑袋。错误:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1097)
at java.awt.Container.add(Container.java:417)
at BallShooter$MainPanel.<init>(BallShooter.java:52)
at BallShooter.<init>(BallShooter.java:24)
at BallShooter.main(BallShooter.java:42)
如果有人能帮助我,那将非常感激。 编辑首次发布,因此格式化时出错。类标题也没有进入代码部分和最后一个括号。
答案 0 :(得分:3)
一般规则 - 当您正在阅读此类堆栈跟踪时,请查找与您的某个类相关的第一行,而不是JDK或库类。在这种情况下,问题可以在BallShooter.java的第52行找到。
当该行运行时,mp
为null
,因此您尝试向容器添加空值,因此例外。
要解决此问题,请先创建另外两个面板,然后再创建MainPanel
。
答案 1 :(得分:0)
您正在初始化非静态嵌套类MainPanel
,该类使用mp
类中的gp
和BallShooter
字段。已初始化(请参阅#add
构造函数中调用MainPanel
的行)。在您创建了所有菜单之后尝试调用mep#add
,或者考虑#init
方法(甚至是不同的层次结构/设计)的内容。