Results 1 to 7 of 7

Thread: Beginners fault

  1. #1
    NGE
    NGE is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Question Beginners fault

    Hello everyone,

    My name is Norman I'm from Holland and I study Management Information Technology @ Hogeschool Arnhem Nijmegen. I'll make a good opening post in the right forum. But for now I've a beginners problem with a NullPointerException in a game I must create. I will explain it:

    Error:
    Java Code:
    Start ruimte zoeken en x,y is 10,0
    alles: 20,null
    nullpointerexeption fout
    java.lang.NullPointerException
    	at Bubblegame.Hangbubble.zoekRuimte(+137)
    	at Bubblegame.Hangbubble.<init>(+78)
    	at Bubblegame.Bubblegame.startGame(+52)
    	at Bubblegame.Bubblegame.menuAction(+61)
    	at phonegame.GameEngine$GameWindow.commandAction(+16)
    	at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
    	at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
    	at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
    	at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
    	at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(+186)
    	at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+57)
    Here's my Class
    Java Code:
    public class Hangbubble extends Bubble{
    [LEFT]
    	private int points;
    	public static int kleur;
    	private int x ;
    	private int y ;
    	private Bubblegame mijnspel;
    	
    	
    	public Hangbubble(Bubblegame mijnspel)
    	{	
    		this.mijnspel = mijnspel;
    		System.out.println("Klasse hangbubble wordt aangeroepen " + mijnspel);
    		points = 20;
    		x=10;
    		y=0;
    		
    		for (int i = 0; i< 23;i++ )
    		{
    		Bubble bb = new Bubble();	
    		bb.kleurkiezen(mijnspel);
    		setImage(bb.s);
    		
    		zoekRuimte();
    		bb.setPosition(x,y);
    		mijnspel.addGameItem(bb);	
    		}
    	}
    
    					
    
    		public void zoekRuimte()
    		{
    		int n=10;
    		System.out.println("Start ruimte zoeken en x,y is " + x + "," + y );
    		System.out.println("alles: " + points + "," + mijnspel );
    		
    		
    		if  (mijnspel == null)
    		{
    			System.out.println ("nullpointerexeption fout");
    		}
    		
    		while		(mijnspel.findItemAt(x,y,getFrameWidth(),getFrameHeight()) != null )  
    					{
    					x=x+n;
    					System.out.println("Iets gevonden");
    					zoekRuimte();
    					}
    			
    		if			(mijnspel.findItemAt(x,y,getFrameWidth(),getFrameHeight()) == null )   					//Kijkt of er bubble op de x-as zit
    					{
    					if			(mijnspel.findTilesAt (x,y,getFrameWidth(),getFrameHeight()) == 0 )		// Als er geen bubble op locatie ... zit dan wordt daar een bubble geplaatst
    								{
    								System.out.println("Coordinaten om te plaatsen" + getX() + "," + getY() );	
    								x=getX();
    								y=getY();
    								}
    					else 
    								{
    								x=10;
    								y=y+n;
    								
    								zoekRuimte();
    								}	
    		
    					}
    												   
    					
    				
    					
    		}[/LEFT]
    I found as you can see that my 'mijnspel' has a null value when i run the game. Only I did declared it in my constructor right? I don;t see where this error exists.

    Could anyone point me in the right direction, cause I'm trying hours and hours to solve it.
    I would be very thankfull!

    Norman

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    Only I did declared it in my constructor right?
    You must give it a non-null value to keep from getting a NullPointerException.
    Where do you give the variable at line: Bubblegame.Hangbubble.zoekRuimte(+137)
    a value?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default

    Yep, most likely (your own program says so) 'mijnspel' equals null. You pass it to your constructor so a null value is passed to it but you didn't show us the code where you are trying to create a new HangBubble object ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    NGE
    NGE is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    I create a new Hangbubble object in the Bubblegame class using this method

    Java Code:
    	public void startGame()
    	{
    		System.out.println ("Startgame wordt aangeroepen" );
    
    		setBackgroundImage("/images/background.png",true);					//Setbackgroundimage (mogelijkheid met randomselect
    		System.out.println("Achtergrond geplaatst");						//meerdere achtergronden)
    		
    	
    		Klok = new Controller(this);										
    		
    		for (int i=0; i <6;i++)											
    		{Hangbubble = new Hangbubble(mijnspel);}
    		System.out.println ("Hangbubbles geplaatst");
    And i don't understand the error code in the console because: Bubblegame.Hangbubble.zoekRuimte(+137)
    is at line 137? My whole Hangbubble class only contains 104 lines??? Where does it point to?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by NGE View Post
    I create a new Hangbubble object in the Bubblegame class using this method

    Java Code:
    	public void startGame()
    	{
    		System.out.println ("Startgame wordt aangeroepen" );
    
    		setBackgroundImage("/images/background.png",true);					//Setbackgroundimage (mogelijkheid met randomselect
    		System.out.println("Achtergrond geplaatst");						//meerdere achtergronden)
    		
    	
    		Klok = new Controller(this);										
    		
    		for (int i=0; i <6;i++)											
    		{Hangbubble = new Hangbubble(mijnspel);}
    		System.out.println ("Hangbubbles geplaatst");
    And i don't understand the error code in the console because: Bubblegame.Hangbubble.zoekRuimte(+137)
    is at line 137? My whole Hangbubble class only contains 104 lines??? Where does it point to?
    What is the value of 'mijnspel' in that method? b.t.w. are you sure you (re)compiled your latest source code? Are you running the latest/current version of your code? The JVM doesn't mention out of bounds line numbers.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    NGE
    NGE is offline Member
    Join Date
    Jun 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    What is the value of 'mijnspel' in that method? b.t.w. are you sure you (re)compiled your latest source code? Are you running the latest/current version of your code? The JVM doesn't mention out of bounds line numbers.

    kind regards,

    Jos
    Java Code:
    	public void startGame()
    	{
    		System.out.println ("Startgame wordt aangeroepen" );
    
    		setBackgroundImage("/images/background.png",true);					//Setbackgroundimage (mogelijkheid met randomselect
    		System.out.println("Achtergrond geplaatst");						//meerdere achtergronden)
    		
    	
    		Klok = new Controller(this);										
    		
    		for (int i=0; i <6;i++)											
    		{Hangbubble = new Hangbubble([COLOR="yellow"]this[/COLOR]);}
    		System.out.println ("Hangbubbles geplaatst");
    mijnspel references to Bubblegame and I found one error that I adjusted above. In the class bubblegame class I'd reference to bubblegame so that was double. I changed it to 'this' and now it seems to work.
    Yes, it was the latest/current version of my code. Up to the next thing to debug.

    But thank you very much all of you that helped! (But i don't think this would be the last question ) we'll see

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default

    Please dont' use that yelllow colour; it is hardly readable; putting it between code tags is enough thank you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 04-24-2011, 10:25 AM
  2. Help with fault tolerance for simple program?
    By harvest117 in forum New To Java
    Replies: 4
    Last Post: 03-03-2011, 08:59 PM
  3. Replies: 5
    Last Post: 10-28-2009, 01:59 PM
  4. segmentation fault error
    By Mrs. Deswal in forum Networking
    Replies: 0
    Last Post: 08-27-2009, 07:44 AM
  5. 'Incompatible operand' fault
    By fatnic388 in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 07:05 PM

Tags for this Thread

Posting Permissions

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