Results 1 to 16 of 16
  1. #1
    Nexus is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default Having problems finding how to solve this Nullpointer Exception.

    Hi, I'm kinda new ( ~ 1 year experience), I've been working on this side-scroller game project, and I've tried to fix this null pointer exception for ages, yet no luck so far.
    here's Eclipse's console error report:

    java.lang.NullPointerException
    at GesturGame.Board.checkCollisions(Board.java:144)
    at GesturGame.Board.actionPerformed(Board.java:69)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    and here is the board class which it adresses:

    Java Code:
    package GesturGame;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import javax.swing.*;
    public class Board extends JPanel implements ActionListener, Runnable
    {
    	public Image img;
    
    	Timer time;			//timer, uppdaterare för skärmen
    	StopWatch s;
    	Thread animator;
    
    	Gestur p;			//spelare
    	Enemy f;		//fiender
    	Enemy f2;
    	Enemy f3;
    	Enemy f4;
    	Enemy f5;
    	Enemy f6;
    	Enemy f7;
    	KnifeB KB;
    	KnifeB KB2;
    	Goal gl;
    
    	int x = 700;	// x för fiender
    	int y = 210;	//y för fiender
    	int points = 0; // poäng
    	static int v = 172;
    
    	boolean lost = false;	//låsvariabler
    	boolean done2 = false;
    	static Font font = new Font("SanSerif", Font.BOLD, 24); //fontdeklarering för kniv-o-metern
    	public Board()
    	{
    		p = new Gestur();
    		addKeyListener(new AL());
    		setFocusable(true); // förenkling för keylistenern så att den fatta t.ex piltangenterna utan knapp ID
    
    		ImageIcon i = new ImageIcon("C:/Users/ANDERSS/Desktop/JavaSpel/bg.png");//bakgrundsbild
    		img = i.getImage();
    
    		time = new Timer(5, this); //skapar timer mrd 5 millisekundersintervall för skärmuppdateringar
    		time.start();				//startar ovan
    		
    		s = new StopWatch();
    		s.start();
    
    		KB = new KnifeB(650,245);
    		KB2 = new KnifeB(1100,245);
    		gl = new Goal(1300, 235);
    		f = new Enemy(x, y, "C:/Users/ANDERSS/Desktop/JavaSpel/enemy3.png"); //bildadress och kordinat för vikingar
    		f2 = new Enemy(x, y, "C:/Users/ANDERSS/Desktop/JavaSpel/enemy4.png");
    		f3 = new Enemy(x, y, "C:/Users/ANDERSS/Desktop/JavaSpel/enemybow.png");
    		f4 = new Enemy(x, y, "C:/Users/ANDERSS/Desktop/JavaSpel/enemy2.png");
    		f5 = new Enemy(x, y, "C:/Users/ANDERSS/Desktop/JavaSpel/enemy.png");
    		f6 = new Enemy(x, y, "C:/Users/ANDERSS/Desktop/JavaSpel/enemy4.png");
    		f7 = new Enemy(x, y, "C:/Users/ANDERSS/Desktop/JavaSpel/enemy3.png");
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		try
    		{
    			if (p.getR() == 1)
    			{
    				resetAll();
    			}
    			checkCollisions(); // som namnet säger, kollar kollisioner
    		}
    		catch(NullPointerException err)
    		{
    			err.printStackTrace();
    		}
    		
    		final ArrayList<Knife> Knifes = p.getKnifes();
    		for (int w = 0; w < Knifes.size(); w++)
    		{
    			Knife m = (Knife) Knifes.get(w);
    			if (m.getVis() == true)
    			{
    				if(p.getFacingleft() == false)
    				{
    					m.move();
    				}
    				if(p.getFacingleft() == true)
    				{
    					m.moveLeft();
    				}
    			}
    			else
    			{
    				Knifes.remove(w);
    			}
    		}
    		p.move();
    		if (p.x > 400)
    		{
    			f.move(p.getdx(), p.getLeft());
    		}
    		if (p.x> 500)
    		{
    			f2.move(p.getdx(), p.getLeft());
    		}
    		if (p.x> 600)
    		{
    			f3.move(p.getdx(), p.getLeft());
    		}
    		if (p.x> 800)
    		{
    			f4.move(p.getdx(), p.getLeft());
    		}
    		if (p.x> 1200)
    		{
    			f5.move(p.getdx(), p.getLeft());
    		}
    		if (p.x> 900)
    		{
    			f6.move(p.getdx(), p.getLeft());
    		}
    		if (p.x> 1100)
    		{
    			f7.move(p.getdx(), p.getLeft());
    		}
    		if (p.x> 875)
    		{
    			KB.move(p.getdx(), p.getLeft());
    		}
    		if (p.x> 905)
    		{
    			KB2.move(p.getdx(), p.getLeft());
    		}
    		if (p.x> 1300)
    		{
    			gl.move(p.getdx(), p.getLeft());
    		}
    		repaint();
    	}
    	public void checkCollisions()
    	{
    		Rectangle r1 = new Rectangle(f.getX(),f.getY(),f.getWidth(),f.getHeight()); //fiendernas kollisionsrects
    		Rectangle r2 = new Rectangle(f2.getX(),f2.getY(),f2.getWidth(),f2.getHeight());
    		Rectangle r3 = new Rectangle(f3.getX(),f3.getY(),f3.getWidth(),f3.getHeight());
    		Rectangle r4 = new Rectangle(f4.getX(),f4.getY(),f4.getWidth(),f4.getHeight());
    		Rectangle r5 = new Rectangle(f5.getX(),f5.getY(),f5.getWidth(),f5.getHeight());
    		Rectangle r6 = new Rectangle(f6.getX(),f6.getY(),f6.getWidth(),f6.getHeight());
    		Rectangle r7 = new Rectangle(f7.getX(),f7.getY(),f7.getWidth(),f7.getHeight());
    		Rectangle d = new Rectangle(p.getLeft(),p.getY(),p.getWidth(),p.getHeight()); //spelarens rect
    		Rectangle Bkn = new Rectangle(KB.getX(),f.getY(),f.getWidth(),f.getHeight()); // knivlådans rect
    		Rectangle Bkn2 = new Rectangle(KB2.getX(),f.getY(),f.getWidth(),f.getHeight()); // knivlådans rect
    		Rectangle glR = new Rectangle(gl.getX(),f.getY(),f.getWidth(),f.getHeight()); // knivlådans rect
    		
    		final ArrayList<Knife> Knifes = new ArrayList<Knife>(p.getKnifes());
    		for (int w = 0; w < Knifes.size(); w++)	//rect-intersect checker för mördningsbara vikingar
    		{
    			Knife m = (Knife) Knifes.get(w);
    			Rectangle m1 = new Rectangle(m.getBounds());
    			if (r1.intersects(m1) && f.Alive())
    			{
    				f.isAlive = false;
    				m.visible = false;
    				points = points + 10;
    			}
    			else if (r2.intersects(m1)&& f2.Alive())
    			{
    				f2.isAlive = false;
    				m.visible = false;
    				points = points + 10;
    			}
    			else if (r3.intersects(m1)&& f3.Alive())
    			{
    				f3.isAlive = false;
    				m.visible = false;
    				points = points + 10;
    			}
    			else if (r4.intersects(m1)&& f4.Alive())
    			{
    				f4.isAlive = false;
    				m.visible = false;
    				points = points + 10;
    			}
    			else if (r5.intersects(m1)&& f5.Alive())
    			{
    				f5.isAlive = false;
    				m.visible = false;
    				points = points + 10;
    			}
    			else if (r6.intersects(m1)&& f6.Alive())
    			{
    				f6.isAlive = false;
    				m.visible = false;
    				points = points + 10;
    			}
    			else if (r7.intersects(m1)&& f7.Alive())
    			{
    				f7.isAlive = false;
    				m.visible = false;
    				points = points + 10;
    			}
    		}
    		if(f.isAlive == false)
    			r1.setLocation(1000, 10000);
    		if(f2.isAlive == false)
    			r2.setLocation(1000, 10000);
    		if(f3.isAlive == false)
    			r3.setLocation(1000, 10000);
    		if(f4.isAlive == false)
    			r4.setLocation(1000, 10000);
    		if(f5.isAlive == false)
    			r5.setLocation(1000, 10000);
    		if(f6.isAlive == false)
    			r6.setLocation(1000, 10000);
    		if(f7.isAlive == false)
    			r7.setLocation(1000, 10000);
    		if (d.intersects(r1) || d.intersects(r2) || d.intersects(r3) ||  d.intersects(r4) || d.intersects(r5) || d.intersects(r6) || d.intersects(r7)) // "dör" om man kommer i kontakt med fienden
    		{
    			lost = true;
    			p.setR(1);
    		}
    		if(d.intersects(Bkn) && KB.got == false)
    		{
    			Bkn.setLocation(1000,1000);
    			int kn = (int)(Math.random()*7) + 1;
    			points = points + kn;
    			p.ammo = p.ammo + kn;
    			KB.visible = false;
    			System.out.println("You found a box with " + kn + " additional knifes!");
    			KB.got = true;
    		}
    		if(d.intersects(Bkn2) && KB2.got == false)
    		{
    			Bkn2.setLocation(1000,1000);
    			int kn = (int)(Math.random()*7) + 1;
    			points = points + kn;
    			p.ammo = p.ammo + kn;
    			KB2.visible = false;
    			System.out.println("You found a box with " + kn + " additional knifes!");
    			KB2.got = true;
    		}
    		if(d.intersects(glR) && gl.got == false)
    		{
    			glR.setLocation(1000,1000);
    			gl.visible = false;
    			System.out.println("You made it! Congratulations!"); // om detta spel var en riktig fullversion hade spelaren börjat på en ny nivå när denna sker
    			gl.got = true;
    		}
    	}
    	public void paint(Graphics g) // målar ut allt på skärmen
    	{
    		/*if(lost) 							// skriver ut fail om man nuddar fienden
     		{
    			System.out.println("You walked into an angry viking Lol, you lost");
    		}*/
    		if (p.dy == 1 && done2 == false)
    		{
    			done2 = true;
    			animator = new Thread(this);
    			animator.start();
    		}
    		super.paint(g); // återimplementerare för graphics, som en sorts buffer
    
    		Graphics2D g2d = (Graphics2D) g;
    
    		if ((p.getX() - 590) % 2400 == 0)		// sak för att bakgrundsbilden ska loopas, behöver då att samma bild kan visas på två olika kordinater, och samtidigt flyter mot vänster som de ska i scrollers
    		{
    			p.nx = 0;
    		}
    		if ((p.getX() - 1790) % 2400 == 0)
    		{
    			p.nx2 = 0;
    		}
    		g2d.drawImage(img, 685 - p.getnX2(), 0, this);
    
    		if (p.getX() > 590)
    		{
    			g2d.drawImage(img, 685 - p.getnX(), 0, this);
    		}
    		g2d.drawImage(p.getImage(), p.left, v, this);
    		if (p.getdx() == -1)
    		{
    			g2d.drawImage(img, 685 - p.getnX2(), 0, this);
    			g2d.drawImage(p.getImage(), p.left, v, this);
    		}														// bakgrundsloop slut
    
    		ArrayList<Knife> Knifes = new ArrayList<Knife>(Gestur.getKnifes());		//utritning av kastade knivar
    		for (int w = 0; w < Knifes.size(); w++)
    		{
    			Knife m = (Knife) Knifes.get(w);
    			if (m.getVis())
    			{
    				g2d.drawImage(m.getImg(),m.getX(), m.getY(), this);
    			}
    		}
    		g2d.setFont(font);										//egenskaper för kniv-o-meter
    		g2d.setColor(Color.BLUE);
    		g2d.drawString("Knives remaining: " + p.ammo, 20, 20);
    		g2d.drawString("Time: " + s.getElapsedTimeSecs(),20,40);
    		g2d.drawString("Score: " + points,500, 20);
    		if(gl.got == true)
    		{
    			g2d.drawString("You finished this level!",250, 160);
    		}
    		if(lost == true)
    		{
    			g2d.drawString("You are injured! press [<--] to restart",200, 160);
    		}
    		if (p.x > 400) // om spelarens x övesrtiger 400 spawnas fiende 1
    		{
    			if (f.Alive() == true)
    			{
    				g2d.drawImage(f.getImage(), f.getX(), f.getY(), this);
    			}
    		}
    		if (p.x > 500) // om spelarens x övesrtiger 500 spawnas fiende 2
    		{
    			if (f2.Alive() == true)
    			{
    				g2d.drawImage(f2.getImage(), f2.getX(), f2.getY(), this);
    			}
    		}
    		if (p.x > 600) // om spelarens x övesrtiger 500 spawnas fiende 3
    		{
    			if (f3.Alive() == true)
    			{
    				g2d.drawImage(f3.getImage(), f3.getX(), f3.getY(), this);
    			}
    		}
    		if(p.x > 875)
    		{
    			if(KB.visible == true)
    			{
    				g2d.drawImage(KB.getImg(), KB.getX(), KB.getY(), this);
    			}
    		}
    		if(p.x > 935)
    		{
    			if(KB2.visible == true)
    			{
    				g2d.drawImage(KB2.getImg(), KB2.getX(), KB2.getY(), this);
    			}
    		}
    		if(p.x > 1100)
    		{
    			if(gl.visible == true)
    			{
    				g2d.drawImage(gl.getImg(), gl.getX(), gl.getY(), this);
    			}
    		}
    		if (p.x > 200) // om spelarens x övesrtiger 500 spawnas fiende 3
    		{
    			if (f4.Alive() == true)
    			{
    				g2d.drawImage(f4.getImage(), f4.getX(), f4.getY(), this);
    			}
    		}
    		if (p.x > 700) // om spelarens x övesrtiger 500 spawnas fiende 3
    		{
    			if (f5.Alive() == true)
    			{
    				g2d.drawImage(f5.getImage(), f5.getX(), f5.getY(), this);
    			}
    		}
    		if (p.x > 700) // om spelarens x övesrtiger 500 spawnas fiende 3
    		{
    			if (f6.Alive() == true)
    			{
    				g2d.drawImage(f6.getImage(), f6.getX(), f6.getY(), this);
    			}
    		}
    		if (p.x > 700) // om spelarens x övesrtiger 500 spawnas fiende 3
    		{
    			if (f7.Alive() == true)
    			{
    				g2d.drawImage(f7.getImage(), f7.getX(), f7.getY(), this);
    			}
    		}
    	}
    	private void resetAll() 
    	{
    		p.x = 75;  //start x kordinat
    		p.left = 150; // längd från vänstra hördnet vid start
    		p.nx = 0;		// nx och nx2 är för bakgrundsbildloopen baserat på spelarens position
    		p.nx2= 685;
    		p.facingleft = false; //ifall spelaren vänder sig mot vänster
    		p.y = 172;
    		p.ammo = 2;
    		x = 700;	// x för fiender
    		y = 210;	//y för fiender
    		points = 0; 
    		f.isAlive = true;
    		f.x = 700;
    		f.y = 210;
    		f2.isAlive  = true;
    		f2.x = 700;
    		f2.y = 210;
    		f3.isAlive  = true;
    		f3.x = 700;
    		f3.y = 210;
    		f4.isAlive  = true;
    		f4.x = 700;
    		f4.y = 210;
    		f5.isAlive  = true;
    		f5.x = 700;
    		f5.y = 210;
    		f6.isAlive  = true;
    		f6.x = 700;
    		f6.y = 210;
    		f7.isAlive  = true;
    		f7.x = 700;
    		f7.y = 210;
    		KB.visible = true;
    		KB.x = 650;
    		KB.y = 245;
    		KB2.visible = true;
    		KB2.x = 1100;
    		KB2.y = 245;
    		gl.visible = true;
    		gl.x = 1300;
    		gl.y = 235;
    		KB.got = false;
    		KB2.got = false;
    		gl.got = false;
    		lost = false;
    		s.stop();
    		s.start();
    		p.setR(0);
    	}
    	private class AL extends KeyAdapter // actionlistener för tangenbord
    	{
    		public void keyReleased(KeyEvent e)
    		{
    			p.keyReleased(e);
    		}
    		public void keyPressed(KeyEvent e)
    		{
    			p.keyPressed(e);
    		}
    	}
    	boolean h = false;		//för att hoppningsanimationen ska funka nedan
    	boolean done = false;
    	public void cycle()
    	{
    		if (h == false) // h, och done är booleans för att kolla om hopet nått högsta punkten och när landningen är färdiggjord
    		{
    			v--;
    			p.y = v-50;
    		}
    		if (v == 15)
    		{
    			h = true;
    		}
    		if (h == true && v <= 172)
    		{
    			v++;
    			p.y = v-50;
    			if (v == 172)
    			{
    				done = true;
    				p.y = v;
    			}
    		}
    	}
    	public void run()
    	{
    		long beforeTime, timeDiff, sleep;
    		beforeTime = System.currentTimeMillis();
    		while (done == false)
    		{
    			cycle();
    			timeDiff = System.currentTimeMillis() - beforeTime;
    			sleep = 10 - timeDiff;
    			if (sleep < 0)
    			{
    				sleep = 2;
    			}
    			try
    			{
    				Thread.sleep(sleep);
    			}
    			catch (InterruptedException e)
    			{}
    			beforeTime = System.currentTimeMillis();
    		}
    		done = false;
    		h = false;
    		done2 = false;
    	}
    }
    I'm kinda lost now so any help would be greatly appreciated. :)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    at GesturGame.Board.checkCollisions(Board.java:144)
    at GesturGame.Board.actionPerformed(Board.java:69)
    Look in your code at line 144 in checkCollisions (its called from line 69)

    What variables are used on that line? Which one is null?
    If you can not tell by looking, add a println before line 144 to print out all the variables used on line 144.
    The print out should show you which variable is null.

  3. #3
    Nexus is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Look in your code at line 144 in checkCollisions (its called from line 69)

    What variables are used on that line? Which one is null?
    If you can not tell by looking, add a println before line 144 to print out all the variables used on line 144.
    The print out should show you which variable is null.

    This is line 144:

    Rectangle r3 = new Rectangle(f3.getBounds());


    This is the declaration of f3:

    Enemy f3;

    ---
    public Board()
    {
    ---

    f3 = new Enemy("C:/Users/ANDERSS/Desktop/JavaSpel/enemybow.png");


    and this is the enemy class with the used variables within checkCollisions in bold:

    Java Code:
    package GesturGame;
    
    import java.awt.*;
    import javax.swing.ImageIcon;
    public class Enemy
    {
    	Image img;
    	int x, y;
    	boolean isAlive = true;
    	public Enemy(String loc)
    	{
    		[COLOR="red"][B]x = 700;
    		y = 210;[/B][/COLOR]
    		ImageIcon l = new ImageIcon(loc);
    		img = l.getImage();
    	}
    	public int getX() //kollar x kordinat
    	{
    		return x;
    	}
    	public int getY() //kollar y kordinat
    	{
    		return y;
    	}
    	public int getWidth() 
    	{
    		return 63;
    	}
    	public int getHeight() //kollar y kordinat
    	{
    		return 78;
    	}
    	public boolean Alive() // om den är vid liv
    	{
    		return isAlive;
    	}
    	public Image getImage() // visar bild
    	{
    		return img;
    	}
    	public void move(int dx, int left) // rör sig mot spelaren när spelaren går framåt
    	{
    		if (dx == 1 && !((left + dx )< 150))
    		{
    			x = x - dx;
    		}
    	}
    	public Rectangle [B][COLOR="red"]getBounds()[/COLOR][/B]	//intersect recten boundas
    	{
    		return new Rectangle(x,y,[COLOR="red"][B] 63, 78[/B][/COLOR]);
    	}
    }
    Last edited by Nexus; 06-05-2011 at 07:53 PM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    Did you print out all the variables used on line 144?
    The only value of interest to your problem is the one that the variable has in line 144.
    If the variable get a value later, it doesn't help.

  5. #5
    Nexus is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    i printlined the f3.getBounds(),r3 as well as f3 which are allt that's used on the line 144, this is what appeared on the console.

    for System.out.println(f3.getBounds());

    java.awt.Rectangle[x=700,y=210,width=63,height=78]
    for System.out.println(r3);

    java.awt.Rectangle[x=700,y=210,width=63,height=78]
    and lastly for for System.out.println(f3);

    GesturGame.Enemy@d1e604
    As i expected, the r3 and the f3.getBounds() showed the same thing (but no nulls), f3 however seems to point at a location in the ram(as far as i understand), which is the very opposite of a nullpointer?
    Last edited by Nexus; 06-05-2011 at 08:15 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    Interesting. You don't get a null pointer exception when you print out values.
    But you always get a null pointer exception without the printlns.

    I have no idea how that can be happening.

    EDIT: The code is called on a Timer event. Perhaps there is a window of time when it is called without the variables initialized.

    Your print outs would be better if you added an something to identify what info is being printed:
    System.out.println("f3=" + f3);

    If you want something more interesting to print out for f3, add a toString method to the class that returns an identifying String.
    Last edited by Norm; 06-05-2011 at 08:19 PM.

  7. #7
    Nexus is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Alright, I followed your example:
    --------------------------------------
    f3.getBounds()= java.awt.Rectangle[x=700,y=210,width=63,height=78]
    --------------------------------------
    f3= GesturGame.Enemy@be2358
    --------------------------------------
    r3= java.awt.Rectangle[x=700,y=210,width=63,height=78]
    (Saw you edit, testing)

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    What about the null pointer???

  9. #9
    Nexus is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    I am fairly certain that I've found the null pointer, the timer was set to trigger every 5 milliseconds and this:
    time = new Timer(5, this);
    , thanks to you mentioning it being a possible cause of the it, I decided to experiment with the timer by setting it to trigger every millisecond and null:
    time = new Timer(1, null);
    surprisingly enough the null pointer exception error stopped appearing and the program works as intended again. Thanks!

    EDIT: Now it works flawlessly on Eclipse, but for some reason not on Textpad? I don't get the null pointer or any error, but the screen is frozen, it does however register keyEvents from my keyboard.

    EDIT2: controlling that all variables are declared before being used, just to be certain.
    Last edited by Nexus; 06-05-2011 at 09:02 PM.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    That sounds more like a work around than a solution. The initialization of all variables should take place before you try to use them.

  11. #11
    Nexus is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    I checked the variables, they all seem to be created before being called.

    I tried having a larger timer delay, this seemed to work without a freeze or errors on Textpad as well, I guess that with a delay of 5 ms, the computer just can't keep up with the memory pointing for the Rectangle declaring, and thus, gives a nullpointer, because it seems to work at delays above 18 ms.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    they all seem to be created before being called.
    if that was true, you would not get a NPE.

  13. #13
    Nexus is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    I know that NPE's occur when the memory points towards null, if I call something undeclared or wrongly declare a non-primitive without a memory reference, I've double and triple-checked this. but then how come I don't get this specific NPE at all once my timer is given longer trigger delays, with no other modifications to my code?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    Have you found which variable is null when you get the NPE?
    Given that you can back track to see how it is not being given a value.

    Your timer causes the code to be executed in different orders. One or more orders of execution leaves a variable null.

  15. #15
    Nexus is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Yes I did, it was apparently the initial declaring of rectangles getting a null pointer(even though they look just fine ( Rectangle r = new Rectangle(f.getBounds()); for example),
    I saw in this the console by reducing the timer delay as much as I could, plus printing all variables within checkCollisions().

    Thanks for the support.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

Similar Threads

  1. Nullpointer Exception with BufferedWriter?
    By Solarsonic in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 12:08 AM
  2. Nullpointer Exception???
    By kipcorn91 in forum AWT / Swing
    Replies: 5
    Last Post: 10-28-2010, 11:19 PM
  3. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 04:44 AM
  4. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 01:44 PM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 03:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •