Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Copying a 2D array to another one  (Read 749 times)
0 Members and 2 Guests are viewing this topic.
Offline StonePickaxes

Full Member
**

Posts: 204
Medals: 3


Nathan Kramber


« on: 2012-01-18 19: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

Full Member
**

Posts: 238
Medals: 8



« Reply #1 on: 2012-01-18 20: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

Jr. Member
**

Posts: 66
Medals: 3



« Reply #2 on: 2012-01-18 20: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! Go get 'em!
Offline BoBear2681

Full Member
**

Posts: 238
Medals: 8



« Reply #3 on: 2012-01-18 20: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

Full Member
**

Posts: 204
Medals: 3


Nathan Kramber


« Reply #4 on: 2012-01-18 20: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

Jr. Member
**

Posts: 66
Medals: 3



« Reply #5 on: 2012-01-18 20: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

Full Member
**

Posts: 238
Medals: 8



« Reply #6 on: 2012-01-18 22: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

Full Member
**

Posts: 238
Medals: 8



« Reply #7 on: 2012-01-18 22: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

JGO Neuromancer
****

Posts: 1050
Medals: 18



« Reply #8 on: 2012-01-18 22:21:35 »

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

Offline BoBear2681

Full Member
**

Posts: 238
Medals: 8



« Reply #9 on: 2012-01-18 22: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! Go get 'em!
Offline StonePickaxes

Full Member
**

Posts: 204
Medals: 3


Nathan Kramber


« Reply #10 on: 2012-01-18 22: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

Full Member
**

Posts: 238
Medals: 8



« Reply #11 on: 2012-01-18 22:39:06 »

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

Full Member
**

Posts: 204
Medals: 3


Nathan Kramber


« Reply #12 on: 2012-01-18 22: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

JGO Neuromancer
****

Posts: 1050
Medals: 18



« Reply #13 on: 2012-01-18 23: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

Full Member
**

Posts: 238
Medals: 8



« Reply #14 on: 2012-01-18 23: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.
Online ra4king

JGO Kernel
*****

Posts: 3153
Medals: 196


I'm the King!


« Reply #15 on: 2012-01-19 01: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

Full Member
**

Posts: 151
Medals: 3



« Reply #16 on: 2012-01-19 01: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();
Offline StonePickaxes

Full Member
**

Posts: 204
Medals: 3


Nathan Kramber


« Reply #17 on: 2012-01-19 23: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!
Online ra4king

JGO Kernel
*****

Posts: 3153
Medals: 196


I'm the King!


« Reply #18 on: 2012-01-20 01:18:45 »

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

Offline BoBear2681

Full Member
**

Posts: 238
Medals: 8



« Reply #19 on: 2012-01-20 08: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

Full Member
**

Posts: 204
Medals: 3


Nathan Kramber


« Reply #20 on: 2012-01-20 14:25:26 »

I fixed it! Thanks Smiley

Check out my website!
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.091 seconds with 19 queries.