Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: The endless Null torcher...

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default The endless Null torcher...

    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;
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: The endless Null torcher...

    In World you have two references to a field, 'teamTwo' that has not be declared.

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: The endless Null torcher...

    Quote Originally Posted by GregBrannon View Post
    In World you have two references to a field, 'teamTwo' that has not be declared.
    That's really weird I'm not sure how those copied over, but I removed them before I posted this code. It doesn't contain them and the error is still there.

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: The endless Null torcher...

    Post the complete stack trace, there must be more than the two lines.

  5. #5
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: The endless Null torcher...

    Quote Originally Posted by PhHein View Post
    Post the complete stack trace, there must be more than the two lines.
    There is, it is all references to EventQue and component handling classes within the API. Google java NPE repaint 249 and I'm sure you could find the exact same stack trace.

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: The endless Null torcher...

    As a first step you should run your code from the EDT.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: The endless Null torcher...

    Your design is unconventional, there's a lot of code, and it will take someone more time than usual to unravel the tangle. For one, you shouldn't call repaint() inside the paint() method - think about what that does - but that's not the source of the error.

    The way you're treating (or forcing) Ball and FieldPlayer to be like JComponents or graphics objects (outside a container) when they're not is odd and I'm not sure entirely wrong, but it doesn't look right. It's an unconventional approach that will take some time to sort out. The source of the error you're seeing is in the Java code that draws and updates the Swing graphical components, because it's expecting to find something that's not there - not sure what. I know that's vague, because I haven't figured it out and probably won't have time to today, but it has something to do with the design of the Ball and FieldPlayer classes.

    Because it's interesting, I may spend time with it later, not today.

  8. #8
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: The endless Null torcher...

    Thank you for your input. I'll be looking around at the swing class in the API and seeing what I can find... It is very vague but ANY help is much appreciated

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: The endless Null torcher...

    Ahh. My pea brain has enough trouble remembering the right way to do things that I don't always remember why the wrong way is bad or recognize it when I see it, but I had an epiphany during my workout today.

    I believe you've demonstrated why we're advised to never override the paint() method unless we really know what we're doing. Instead, we're told to ONLY override the paintComponent() method. "But JFrame doesn't have a paintComponent() method," you might say. Yes, that's why we typically see graphics and animations done on another component, like a JPanel, that is then added to the JFrame.

    The Custom Painting Tutorial, should explain these points in more detail. You'll also see that Step 1 shows how to start a Swing GUI on the EDT. Come back if you need more info.

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: The endless Null torcher...

    The Java tutorials, 2D Graphics
    Several tutorials on that page, includes sample code and running examples

  11. #11
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: The endless Null torcher...

    Ahhhhhh I see now. This will take some reworking for sure but my app does need to be running on the EDT. Thanks for the help

Similar Threads

  1. how to add endless scrolling to tumblr theme
    By imsa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 1st, 2013, 11:12 PM
  2. Replies: 7
    Last Post: June 19th, 2012, 07:06 AM
  3. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM
  4. drawImage() causes endless calls to paintComponent()
    By repaint_forever in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 10th, 2011, 12:15 PM
  5. !=null
    By ss7 in forum Java Theory & Questions
    Replies: 11
    Last Post: October 31st, 2009, 02:48 PM