Hey guys,
I'm using a Point array to transfer to my panel class that draws the image. The reason I'm using coords is because it shows where the character is in the tileset for directions

Ie. (20,5) is right step facing up.

anyway its coming up with null pointer and I cant figure it out! Here it is:

public void charSetup(){
		//Forward
		character[0].x = 55; <--These give null pointer!
		character[0].y = 150;
		character[1].x = 105;
		character[1].y = 150;
		character[2].x = 5;
		character[2].y = 150;
ETC....
}

Thanks PO

Recommended Answers

All 6 Replies

character[0] wasn't initialized . . that's all I can tell you from the information given.

character[0] wasn't initialized . . that's all I can tell you from the information given.

I declare up here:

public class Character extends Sprite {
	Point[] character = new Point[13];

When you define an array, the array elements are given their default values which depends on the type of elements the array holds. For booleans it is false, for integers it is 0, for reference types it is... :)

When you define an array, the array elements are given their default values which depends on the type of elements the array holds. For booleans it is false, for integers it is 0, for reference types it is... :)

Would it be a null?

I did some reading and changed the method of setting the points to the proper way:

public void charSetup(){
		//Forward
		character[0].setLocation(55, 150);
		character[1].setLocation(105, 150);
		character[2].setLocation(5, 150);

Still getting errors though :(

What he means is that you established an array, but each element of the array is still null. You need to create instances of Point at each element before you can access any members.

> Would it be a null?

Yes, it would be a null. In such cases, it helps to think of arrays as just another class containing a list of values rather than a provision built in the language itself.

class ArrayOfInts {
  // for every ArrayOfInts object created, i will be 0 by default
  private int i;
}

class ArrayOfBooleans {
  // for every ArrayOfBooleans object created, i will be false by default
  private boolean i;
}

class ArrayOfPoints {
  // for every ArrayOfPoints object created, i will be null by default
  private Point i;
}

I'd personally work with a fixed size ArrayList of points and if needed, invoke the toArray method to get the underlying array. Much easier and cleaner IMO.

List<Point> points = new ArrayList<Point>(13);
points.add(new Point(12,12));
// and so on
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.