Jump to content
  • Advertisement
Sign in to follow this  
Yarduza

[java] NullPointerException problem

This topic is 4883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am building a backgammon game using a linked list, but for some reason, when i try to run in it gives me the nullPointerException (java.lang.nullPointerException) and i dont know why. I use eclipse compiler and it indicates the problem where i add elements to the list. The code looks like this: public Board(){ luach = new Makom[24]; eatList = new Makom(); outListBlack = new Makom(); outListWhite= new Makom(); } public void setLuach(){ for(int c=0;c<5;c++){ this.luach[1].addSachkan(new Soldier(0,"white")); } for(int d=0;d<5;d++){ this.luach[12].addSachkan(new Soldier(12,"black")); } for( int f=0;f<3;f++){ this.luach[4].addSachkan(new Soldier(4,"black")); } for( int e=0;e<3;e++){ this.luach[16].addSachkan(new Soldier(16,"white")); } for( int h=0;h<5;h++){ this.luach[6].addSachkan(new Soldier(6,"black")); } for( int r=0;r<5;r++){ this.luach[18].addSachkan(new Soldier(18,"white")); } for( int o=0;o<2;o++){ this.luach[11].addSachkan(new Soldier(11,"white")); } for( int m=0;m<2;m++){ this.luach[23].addSachkan(new Soldier(23,"black")); } } Explanation: soldier is the backgammon soldier. as i start the game i arrange the board in the starting position by adding with a loop the soldiers into thier places. for some reason he gives me the nullPointerException and i dont know why. more explanation: the game includes 5 classes, the Makom class which represents the triangles in the backgammon board, the Soldier class which represents the soldiers in the game. the Cubes class which represents the cubes. the board class that represents the board and includes an Makom class array (has 24 places). the Game class that extends the JPanel and implements the MouseMotionListener. I will be very happy to get an answer because i am trying to solve this problem for some days now.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You've likely not created the Makom objects in the luach array.

The line:

Makom[] luach = new Makom[24];

Defines a variable called "luach" as a reference to an array of Makom references, creates a 24-length array of Makom reference, and points the luach variable at it. However, these 24 references all start off null. You'll need one more step before you have a 24-length array of references pointing to instantiated Makom objects:

for(int i = 0 ; i < luach.length ; i++) luach = new Makom();

[Edited by - Sneftel on June 1, 2005 1:36:04 PM]

Share this post


Link to post
Share on other sites
You mean luach[x]= new Makom.
I knew I should have posted this topic here, you just solved a 2 weeks problem, i just didnt know where the problem was!!
I always, but always fall on the simple things, i cant believe i didn't see it, sometimes it is really frustrating.
anyway,thanks!!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes, if your loop variable is named x, use luach[x] to access the correct reference.

It may be a simple fix, but forgetting to initialise the objects in an array of references is one of the most common errors made by new Java developers - common enough to point the finger at Sun for not making it clear enough rather than the coder for making the mistake in the first place.

Thanks Sneftel for fixing my broken formatting! :-)

--cfmdobbie

Share this post


Link to post
Share on other sites
I don't know if this if helpful or not, but I will put my i++ in here:

Try using an IDE with a debugger. I use Eclipse, but there are lots of others that are good (I've heard good things about NetBeans). That way you can step into your code line by line and see what is going on with each and every variable.

Another low-tech way is to just use println statements. Log4j is a good way of adding logging statements that allows you to turn the display of debugging statements on or off with a very, very small performance hit.

Programs are complicated and game programs doubly so. Find tools that work for you and use them. True, you do have to take the time to learn how to use them, but ultimatly they will save you time.

Good luck!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!