gpt4 book ai didi

java - 空指针异常

转载 作者:行者123 更新时间:2023-12-01 20:25:12 25 4
gpt4 key购买 nike

我对 StackOverFlow 还很陌生。我有多年的 Java 经验,但我似乎无法在这段代码中找到触发空指针异常的行。导入已全部准备就绪。

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)

如果有人能帮助我解决这个问题,我将不胜感激。编辑第一次发帖,格式有问题。类头也没有进入代码部分和最后一个括号。

最佳答案

一般规则 - 当您阅读这样的堆栈跟踪时,请查找属于您的类的第一行,而不是 JDK 或库类。在本例中,问题可以在 BallShooter.java 的第 52 行找到。

当该行运行时,mpnull,因此您尝试向容器添加 null,因此出现异常。

要解决此问题,请首先创建其他两个面板,最后创建 MainPanel

关于java - 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43948412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com