Copying a 2D array to another one

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

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

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 -

l.getMap(level);

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

Thanks,
-Nathan

EDIT - Source + Jar

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.

leveltest isn’t really set. You need to do.


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.

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

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.

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. :slight_smile:

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.

Still doesn’t work after initialized it.

-Nathan

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)

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

Actually, I’m lazy, I’d rather just you give me the stack trace then download and run something. :slight_smile:
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.

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.

GUYS The stack trace has NOTHING to do with his code :stuck_out_tongue:

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

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

did you initialize ‘l’

so like

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

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


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.