Hello. I currently program on a 13" MacBook pro with retina display.
I was writing a sample program to test out a problem I am recently encountering in my other projects and of course I managed to cause this certain error in my test project. This is a Java app that uses JFrame and draws 4 circle objects, one of which is targeted and is able to "pass" a circle ball object to one of the nearest other 3 circles. It is meant to be similar to soccer or futbol.
Anyways, during development, I happen to not use a constructor to initialize a new object and I just left it blank when I ran the application in my JDE. Some factor of this caused an NPE and it's a deadly hidden one.
Do to instinct I start erasing previous code in counter-developed order to see if I could get this error to go away, and hopefully find what's causing it. The problem is this error will stick with me to the very beginning of my program's history... and I mean to the implementation of the JFrame. I don't know why I am getting an error on code that I was not before. Please get me a hint as to where this null value may be in my test project...
Error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.awt.RepaintArea.paint(RepaintArea.java:249)
at apple.awt.ComponentModel.handleEvent(ComponentMode l.java:263)
World.java
import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.concurrent.CopyOnWriteArrayList; import javax.swing.JFrame; import audboy.project.objects.Ball; import audboy.project.player.FieldPlayer; public class World extends JFrame implements KeyListener { /** * World.java */ private static final long serialVersionUID = -5860009404824002249L; //generated private Image image; private Graphics graphics; private CopyOnWriteArrayList<FieldPlayer> teamOne = new CopyOnWriteArrayList<FieldPlayer>(); private FieldPlayer teamOneP1, teamOneP2, teamOneP3, teamOneP4; private FieldPlayer currentPossesion; private Ball ball; private boolean playerGoingUp = false; private boolean playerGoingDown = false; private boolean playerGoingLeft = false; private boolean playerGoingRight = false; public World(){ super("Pass"); setSize(800, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true); addKeyListener(this); teamOneP1 = new FieldPlayer(50, 50, true); teamOneP2 = new FieldPlayer(50, 300, false); teamOneP3 = new FieldPlayer(150, 300, false); teamOneP4 = new FieldPlayer(250, 300, false); teamOne.add(teamOneP1); teamOne.add(teamOneP2); teamOne.add(teamOneP3); teamOne.add(teamOneP4); currentPossesion = teamOneP1; ball = new Ball(currentPossesion.getxPos(), currentPossesion.getyPos()); } public void paint(Graphics g) { image = createImage(getWidth(), getHeight()); graphics = image.getGraphics(); paintComponent(graphics); g.drawImage(image, 0, 0, this); } public void paintComponent(Graphics g){ //temp bg g.setColor(Color.GREEN); g.fillRect(0, 0, 800, 400); for(FieldPlayer fp : teamOne){ if(fp.isBeingControlled()){ fp.update(this); } fp.paint(g); } ball.paint(g); ball.update(this, currentPossesion.getxPos(), currentPossesion.getyPos(), currentPossesion); } public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_W) { playerGoingUp = true; } else if(e.getKeyCode() == KeyEvent.VK_S) { playerGoingDown = true; } else if(e.getKeyCode() == KeyEvent.VK_A) { playerGoingLeft = true; } else if(e.getKeyCode() == KeyEvent.VK_D) { playerGoingRight = true; } } public void keyReleased(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_W) { playerGoingUp = false; } else if(e.getKeyCode() == KeyEvent.VK_S) { playerGoingDown = false; } else if(e.getKeyCode() == KeyEvent.VK_A) { playerGoingLeft = false; } else if(e.getKeyCode() == KeyEvent.VK_D) { playerGoingRight = false; } else if(e.getKeyCode() == KeyEvent.VK_SPACE){ currentPossesion = currentPossesion.getNearestPlayer(this); ball.setBeingPassed(true); } } public void keyTyped(KeyEvent e) {}; public boolean isPlayerGoingUp() { return playerGoingUp; } public void setPlayerGoingUp(boolean playerGoingUp) { this.playerGoingUp = playerGoingUp; } public boolean isPlayerGoingDown() { return playerGoingDown; } public void setPlayerGoingDown(boolean playerGoingDown) { this.playerGoingDown = playerGoingDown; } public boolean isPlayerGoingLeft() { return playerGoingLeft; } public void setPlayerGoingLeft(boolean playerGoingLeft) { this.playerGoingLeft = playerGoingLeft; } public boolean isPlayerGoingRight() { return playerGoingRight; } public void setPlayerGoingRight(boolean playerGoingRight) { this.playerGoingRight = playerGoingRight; } public Image getImage() { return image; } public void setImage(Image image) { this.image = image; } public Graphics getGraphics() { return graphics; } public void setGraphics(Graphics graphics) { this.graphics = graphics; } public CopyOnWriteArrayList<FieldPlayer> getTeamOne() { return teamOne; } public void setTeamOne(CopyOnWriteArrayList<FieldPlayer> teamOne) { this.teamOne = teamOne; } public FieldPlayer getTeamOneP1() { return teamOneP1; } public void setTeamOneP1(FieldPlayer teamOneP1) { this.teamOneP1 = teamOneP1; } public FieldPlayer getTeamOneP2() { return teamOneP2; } public void setTeamOneP2(FieldPlayer teamOneP2) { this.teamOneP2 = teamOneP2; } public FieldPlayer getTeamOneP3() { return teamOneP3; } public void setTeamOneP3(FieldPlayer teamOneP3) { this.teamOneP3 = teamOneP3; } public FieldPlayer getTeamOneP4() { return teamOneP4; } public void setTeamOneP4(FieldPlayer teamOneP4) { this.teamOneP4 = teamOneP4; } public FieldPlayer getCurrentPossesion() { return currentPossesion; } public void setCurrentPossesion(FieldPlayer currentPossesion) { this.currentPossesion = currentPossesion; } public Ball getBall() { return ball; } public void setBall(Ball ball) { this.ball = ball; } public static long getSerialversionuid() { return serialVersionUID; } public static void main(String[] args){ new World(); } }
FieldPlayer.java
import java.awt.Color; import java.awt.Graphics; import audboy.project.World; public class FieldPlayer { private int xPos, yPos; private int speed = 3; private final int WIDTH = 30; private final int HEIGHT = 30; private boolean beingControlled; public FieldPlayer(int xPos, int yPos, boolean beingControlled){ this.xPos = xPos; this.yPos = yPos; this.beingControlled = beingControlled; } public void paint(Graphics g){ g.setColor(Color.WHITE); g.fillOval(xPos, yPos, WIDTH, HEIGHT); if(beingControlled){ g.setColor(Color.BLACK); g.drawOval(xPos - 1, yPos - 1, WIDTH + 1, HEIGHT + 1); } } public void update(World world){ final double PERCENT_RATIO = 1; if(world.isPlayerGoingUp()){ if(world.isPlayerGoingUp() && world.isPlayerGoingLeft()){ yPos -= speed * PERCENT_RATIO; xPos -= speed; } else if(world.isPlayerGoingUp() && world.isPlayerGoingRight()) { yPos -= speed * PERCENT_RATIO; xPos += speed; } else { yPos -= speed * PERCENT_RATIO; } } else if(world.isPlayerGoingDown()){ if(world.isPlayerGoingDown() && world.isPlayerGoingRight()){ yPos += speed * PERCENT_RATIO; xPos += speed; } else if(world.isPlayerGoingDown() && world.isPlayerGoingLeft()){ yPos += speed * PERCENT_RATIO; xPos -= speed; } else { yPos += speed * PERCENT_RATIO; } } if(world.isPlayerGoingRight() && !world.isPlayerGoingUp()){ if(world.isPlayerGoingDown()){ //donothing } else { xPos += speed; } } else if(world.isPlayerGoingLeft() && !world.isPlayerGoingUp()){ if(world.isPlayerGoingDown()){ //donothing } else { xPos -= speed; } } } public FieldPlayer getNearestPlayer(World world){ FieldPlayer returnPlayer = world.getTeamOne().get(0); for(FieldPlayer p : world.getTeamOne()){ if(getDistance(xPos, yPos, p.getxPos(), p.getyPos()) < getDistance(xPos, yPos, returnPlayer.getxPos(), returnPlayer.getyPos())){ returnPlayer = p; } } return returnPlayer; } private int getDistance(int x1, int y1, int x2, int y2){ int intOne = Math.abs(x2 - x1); int intTwo = Math.abs(y2 - y1); return intOne + intTwo; } public int getxPos() { return xPos; } public void setxPos(int xPos) { this.xPos = xPos; } public int getyPos() { return yPos; } public void setyPos(int yPos) { this.yPos = yPos; } public int getWIDTH() { return WIDTH; } public int getHEIGHT() { return HEIGHT; } public boolean isBeingControlled() { return beingControlled; } public void setBeingControlled(boolean beingControlled) { this.beingControlled = beingControlled; } }
Ball.java
import java.awt.Color; import java.awt.Graphics; import audboy.project.World; import audboy.project.player.FieldPlayer; public class Ball { private double xPos, yPos; private final int WIDTH = 10; private final int HEIGHT = 10; private int speed = 7; private boolean beingPassed = false; public Ball(double xPos, double yPos){ this.xPos = xPos; this.yPos = yPos; } public void paint(Graphics g){ g.setColor(Color.BLACK); g.fillOval((int) xPos, (int) yPos, WIDTH, HEIGHT); } public void update(World world, int x, int y, FieldPlayer player){ if(beingPassed){ moveTowards(xPos, yPos, x, y); } else { xPos = player.getxPos(); yPos = player.getyPos(); } if(isNear(xPos, player.getxPos()) && isNear(yPos, player.getyPos())){ beingPassed = false; } } private void moveTowards(double xPos2, double yPos2, int tx2, int ty2) { double angle = Math.atan2(ty2-yPos2, tx2-xPos2); double xVel = speed * Math.cos(angle); double yVel = speed * Math.sin(angle); xPos += xVel; yPos += yVel; } private boolean isNear(double int1, double int2){ //fixes flinches while holding the ball return Math.abs(int1 - int2) < 4; } public double getxPos() { return xPos; } public void setxPos(double xPos) { this.xPos = xPos; } public double getyPos() { return yPos; } public void setyPos(double yPos) { this.yPos = yPos; } public boolean isBeingPassed() { return beingPassed; } public void setBeingPassed(boolean beingPassed) { this.beingPassed = beingPassed; } public int getWIDTH() { return WIDTH; } public int getHEIGHT() { return HEIGHT; } }