Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Copying a 2D array to another one  (Read 2620 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2012-01-19 00:47:34 »

For getting the correct level in the game I'm working on, I want to have and integer "level" and add one to it when the level is completed. Then in my "Levels" class, I have it set it to the appropriate level. This isn't working though. Here's some code to explain -

Player class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
int[][] map = new int[6][8]; //initialize map, where level is stored
int level = 1;

//
//later on
//

public void checkCollision()
   {
      if (px == maxPosX) px = 8;
      if (px == minPosX) px = 0;
      if (py == maxPosY) py = 6;
      if (py == minPosY) py = 0;
         
      if (starting)
      {
         l.getMap(level);
         
         for (int x = 0; x < 9; x++)
         {
            for (int y = 0; y < 7; y++)
            {
               System.out.println(x + " x, " + y + " y");
               
               playerData = map[py][px];
               mapData = map[y][x];
           
               if (mapData == 0)
               {
                  px = x;
                  py = y;
               }
               
               if (mapData == 2)
               {
                  Boulder b = new Boulder(m, this);
                  b.setX(x);
                  b.setY(y);
                  m.boulders.add(b);
               }
               
               if (mapData == 3)
               {
                  Pit pi = new Pit(m, this);
                  pi.setX(x);
                  pi.setY(y);
                  m.pits.add(pi);
               }
               
               if (mapData == 4)
               {
                  Grass gr = new Grass(m, this);
                  gr.setX(x);
                  gr.setY(y);
                  m.grass.add(gr);
               }
            }
         }
         starting = false;
      }
   }


Levels class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
package main;

public class Levels
{
   Player p;
   
   int[][] leveltest = {
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 0, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,} //
   };
   
   int[][] level1 = {
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
         {4, 4, 4, 1, 3, 3, 4, 4, 4,}, //
         {4, 4, 4, 2, 2, 2, 4, 4, 4,}, //
         {4, 4, 1, 3, 0, 3, 4, 4, 4,}, //
         {4, 4, 1, 2, 3, 2, 4, 4, 4,}, //
         {4, 4, 4, 4, 1, 1, 4, 4, 4,}, //
         {4, 4, 4, 4, 4, 4, 4, 4, 4,} //
   };
   
   public void getMap(int level)
   {
     
     
      if (level == 1) p.map = leveltest;
      if (level == 2) p.map = level1;
   }
}


I get a nullpointer exception on this line -
1  
l.getMap(level);


Any help as to why this is happening would be appreciated.

Thanks,
-Nathan

EDIT - Source + Jar

Check out my website!
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #1 - Posted 2012-01-19 01:16:10 »

It's hard to know for sure without seeing the actual stack trace.  Possibilities, depending on what line it's actually pointing to:

1. "l" is null.
2. Your Player "p" instance in Levels is null.
Offline Shazer2

Junior Duke


Medals: 3


Aspiring developer.


« Reply #2 - Posted 2012-01-19 01:16:23 »

leveltest isn't really set. You need to do.
1  
2  
3  
4  
5  
6  
7  
8  
9  
int[][] leveltest = new int[width][height] {
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 0, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,} //
   };


You will need to do the same for level1 too.

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #3 - Posted 2012-01-19 01:23:35 »

leveltest isn't really set. You need to do.
1  
2  
3  
4  
5  
6  
7  
8  
9  
int[][] leveltest = new int[width][height] {
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 0, 1,}, //
         {1, 1, 1, 1, 1, 1, 1, 1, 1,} //
   };


You will need to do the same for level1 too.

This isn't true.  Even if it were, it wouldn't cause an NPE at the line mentioned.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #4 - Posted 2012-01-19 01:26:43 »

Check the original post; I updated with the full source and a batch+jar so you can see the error in command prompt.

Check out my website!
Offline Shazer2

Junior Duke


Medals: 3


Aspiring developer.


« Reply #5 - Posted 2012-01-19 01:27:47 »

Look at his getMap method. If level is 1 the map is set to leveltest.

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #6 - Posted 2012-01-19 03:13:01 »

Look at his getMap method. If level is 1 the map is set to leveltest.

Yes, but leveltest isn't null.  And even if it were, setting a variable to null doesn't cause an NPE.  There is a possibility that p==null, however, as I suggested earlier.
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #7 - Posted 2012-01-19 03:20:06 »

Check the original post; I updated with the full source and a batch+jar so you can see the error in command prompt.

Just pasting the stack trace here would probably be sufficient.  Again, what line is the NPE being thrown from?  If it's really one like you show in your post, then it can only be "l" that is null.  If it's actually complaining about one of the two lines *inside* getMap(), then it's got to be "p" that is null.

NPE's are fun and easy to fix, because the stack trace points right to the problem.  Just see which variable you're de-referencing.  It must be null.  If you're de-referencing more than one, it's still usually easy to figger out which one is the culprit.  And if it isn't, well, that's what debuggers are for.   Smiley
Offline Cero
« Reply #8 - Posted 2012-01-19 03:21:35 »

Player p is never instanciated
you cannot call method on an obejct that still points to null.

Offline BoBear2681

JGO Coder


Medals: 19



« Reply #9 - Posted 2012-01-19 03:22:29 »

Player p is never instanciated
you cannot call method on an obejct that still points to null.

To be fair, it's package-private, so he could be setting it elsewhere.  That's why I couldn't decide which of the two possibilities it was.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #10 - Posted 2012-01-19 03:27:10 »

Player p is never instanciated
you cannot call method on an obejct that still points to null.

Still doesn't work after initialized it.

-Nathan

Check out my website!
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #11 - Posted 2012-01-19 03:39:06 »

So, what stack trace are you getting again?
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #12 - Posted 2012-01-19 03:46:41 »

So, what stack trace are you getting again?

Might be easier for you to download the zip I posted on the first post.

But here it is -

Exception in thread "Thread-3" java.lang.NullPointerException
   at main.Player.checkCollision(Player.java:114)
   at main.Player.update(Player.java:180)
   at main.Main.logicUpdates(Main.java:167)
   at main.Main.tick(Main.java:159)
   at main.Main.run(Main.java:130)
   at java.lang.Thread.run(Unknown Source)

Check out my website!
Offline Cero
« Reply #13 - Posted 2012-01-19 04:19:06 »

Player p is never instanciated
you cannot call method on an obejct that still points to null.

Still doesn't work after initialized it.

-Nathan

what did you do ?  p was null so it couldnt work

edit: change int[][] map = new int[6][8]; to just int[][] map;
may work
the size is wrong, you give it a 9x7 array which doesnt fit

Offline BoBear2681

JGO Coder


Medals: 19



« Reply #14 - Posted 2012-01-19 04:23:05 »

So, what stack trace are you getting again?

Might be easier for you to download the zip I posted on the first post.

But here it is -

Exception in thread "Thread-3" java.lang.NullPointerException
   at main.Player.checkCollision(Player.java:114)
   at main.Player.update(Player.java:180)
   at main.Main.logicUpdates(Main.java:167)
   at main.Main.tick(Main.java:159)
   at main.Main.run(Main.java:130)
   at java.lang.Thread.run(Unknown Source)


Actually, I'm lazy, I'd rather just you give me the stack trace then download and run something.  Smiley
My point was just to read what the stack trace is telling you.  NPE's are easy to fix.  What's line 114 of Player.java?  If it's indeed "l.getMap(level)", then l must be null.  Just trace back to see why l isn't set.

what did you do ?  p was null so it couldnt work

As I mentioned, he could have been setting it in another class, since it was package private.  Or not, as it appears that l is what's null.
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #15 - Posted 2012-01-19 06:05:04 »

GUYS The stack trace has NOTHING to do with his code Tongue

@OP
Check line 114 on checkCollision method, what ever object references you call methods on at that line are null Wink

Offline Shane75776
« Reply #16 - Posted 2012-01-19 06:18:56 »

well, on your player class at line 17 I noticed that you are doing l.getMap(level);

did you initialize 'l'

so like
1  
Level l = new Level();

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #17 - Posted 2012-01-20 04:48:35 »

well, on your player class at line 17 I noticed that you are doing l.getMap(level);

did you initialize 'l'

so like
1  
Level l = new Level();


Yes, I did. I have been going at this all day and I just don't get it. Is there anyone who could possibly just download the source and test it out? Maybe you'll see something I can't.

-Nathan

Check out my website!
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2012-01-20 06:18:45 »

Check line 114 on checkCollision method, what ever object references you call methods on at that line are null Wink

Offline BoBear2681

JGO Coder


Medals: 19



« Reply #19 - Posted 2012-01-20 13:49:52 »

GUYS The stack trace has NOTHING to do with his code Tongue

@OP
Check line 114 on checkCollision method, what ever object references you call methods on at that line are null Wink

I did.  That's exactly what my previous reply told him.  I've also confirmed that line 114 is indeed the "l.getMap(level)" line.

@OP: As I said, NPE's are literally the easiest bugs you'll ever fix.  You just need to work backwards through your logic until you find something being null that shouldn't be...

Look at line 114 of Player.java.  Note that it's just

1  
l.getMap(level);


Here, "l" is the only object you're de-referencing, so it must be null.  So think, where is "l" getting set in the Player class?  It appears to be done in the constructor.  So go through your code and find all places where you instantiate a new Player.  See what you're passing in for the third argument.  Then you should be able to spot your problem.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #20 - Posted 2012-01-20 19:25:26 »

I fixed it! Thanks Smiley

Check out my website!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (34 views)
2014-10-27 03:28:38

TehJavaDev (27 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (59 views)
2014-10-16 15:22:06

Norakomi (48 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!