1

Hello I have a little game I made for practice. I am able to put a JLabel on the screen but, when I run the ActionListener actionPerformed third if statement it stops the game. Also i have no idea what to change for the "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" Error. Thanks :) Here's the code:

package me.mikail.movement;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class Player extends JComponent implements ActionListener, KeyListener{

Timer time = new Timer(10, this);
private int x = 0, xSpeed = 3, y = 0, ySpeed = 4, stickX = 0, stickY = 400;
private JFrame frame;
private int oneup = 0;
private JLabel info;

public static void main(String[] args){
    Player p = new Player();
    JFrame frame = new JFrame("Player Practice");
    JLabel info = new JLabel(" ");
    frame.setSize(500,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(p);
    frame.getContentPane().setBackground(Color.BLACK);
    frame.setResizable(false);
    frame.addKeyListener(p);
    info.setOpaque(true);
    info.setBackground(Color.WHITE);
    frame.add(info, BorderLayout.SOUTH);
}

public void paintComponent(Graphics g){
    super.paintComponents(g);
    //Ball
    g.setColor(Color.BLUE);
    g.fillRect(x, y, 30, 30);
    //Stick
    g.setColor(Color.RED);
    g.fillRect(stickX, stickY, 75, 10);
    time.start();
}
public void points(){
    int counter[] = {0,1,2,3,4,5,6,7,8,9,10};
    for(int i = 0; i<=oneup; i++){
        info.setText("You have "+counter[i]+" point(s)");
    }
    frame.add(info, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
    //Y's 0 is on the top
    //X's 0  is on the left
    if(x<0){
        xSpeed = -(xSpeed);
    }
    if(x>=470){
        xSpeed = -(xSpeed);
    }
    //when block hits stick
    if(y==stickY){
        if(x>=(stickX)&&x<=(stickX+75)){
            ySpeed = -(ySpeed);
            oneup = (1+oneup);
            points();
        }else{
        }
    }
    if(y<0){
        ySpeed = -(ySpeed);
    }
    if(y>=450){
        ySpeed = -(ySpeed);
    }
    x = (x + xSpeed);
    y = (y + ySpeed);
    repaint();

}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if(key == KeyEvent.VK_A|| key == KeyEvent.VK_LEFT){
        stickX = (stickX - 50);
    }else if(key == KeyEvent.VK_D|| key ==  KeyEvent.VK_RIGHT){
        stickX = (stickX + 50);
    }else{
        System.out.println("Why did you type that?");
    }
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}
}
Mikail
  • 11
  • 2
  • Hint: you are never assigning anything to the instance fields `frame` and `info`, instead you only assign things to the (static) method local variables with the same names in `main`. – Mark Rotteveel Dec 12 '15 at 20:35

1 Answers1

2
for(int i = 0; i<=i; i++){
    info.setText("You have "+counter[i]+" point(s)");
}

- i<=i is always true, no matter the i's value. This for loop will never end...and while doing this, it will probably never get to actually display the text either.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
  • I changed the for loop to i<1000 but it still doesn't work? The error I'm guessing happens with the third if statement(It doesn't show were the error comes from) – Mikail Dec 12 '15 at 18:10