Reputation: 3
I'm fairly new to StackOverFlow. I have a years experience with Java, but I can't seem to find an the line(s) in this code that triggers the Null pointer exception. Imports are all ready done.
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);
}
}
}
}
I have no idea what's going on. It's just flying over my head. The error:
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)
If anyone could help me with this, that would be greatly appreciated. EDIT First time posting, so there's an error with the formatting. The class header didn't go into the code section and the last bracket as well.
Upvotes: 0
Views: 173
Reputation: 79838
General rule - when you're reading a stack trace like this, look for the first line that's one of YOUR classes, as opposed to a JDK or library class. In this case, the problem can be found on line 52 of BallShooter.java.
When that line runs, mp
is null
, so you're trying to add a null to your container, hence the exception.
To fix this, create the other two panels first, and the MainPanel
last.
Upvotes: 3
Reputation: 11483
You're initializing the non-static nested class MainPanel
, which uses your mp
and gp
fields from the BallShooter
class before they're initialized (see the lines which call #add
in the MainPanel
constructor). Try calling mep#add
after you've created all the menus, or consider something along the lines of an #init
method (or even a different hierarchy/design).
Upvotes: 0