1

I attempted to adapt a class I had found on the web for a dynamic array of ints for a dynamic array of "Entities," but now I am getting a "NullPointerException."

The code raising the exception is:

public void initialize()
{
    buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
    Entities.put(Entities.getCurrentPos()+1, new Entity(100, 100, Color.green));
    Entities.put(Entities.getCurrentPos()+1, new Entity(400, 400, Color.blue));
}

The relevant parts of DynArrayEntities class:

...

private Entity[] data;  // An array to hold the data.
private int currentpos = 0;

...

public void put(int position, Entity value) {

    if (position >= data.length) {

        int newSize = 2 * data.length;
            if (position >= newSize)
                newSize = 2 * position;
        Entity[] newData = new Entity[newSize];
        System.arraycopy(data, 0, newData, 0, data.length);
        data = newData;
    }

    data[position] = value;
    currentpos++;

}

....

public int getCurrentPos() {
    return currentpos;
}

Thanks in advance for your help!

5
  • 3
    In case of exceptions in Java, the stacktrace is pretty important. It tells you the invocation history in detail and points the first line where this exception has been caused. Without knowing the invocation and where the first line of the trace points in your code, it's hard -if not impossible- to give a reliable answer based on only stripped code which doesn't represent an SSCCE (sscce.org). So, in the future, please post the stacktrace as well. Or put some effort in yourself to learn how to read/interpret the stacktrace. Commented Feb 15, 2010 at 23:54
  • Looks like a beginner programming assignment. Most people would just use a built-in class from Java. Commented Feb 16, 2010 at 0:17
  • Heh. Didn't know there was a built-in Java class for this. :D I'm afraid I suck at googling... Commented Feb 16, 2010 at 0:59
  • As well as the stack trace, we need to see the source code that declares Entities. Commented Feb 16, 2010 at 1:00
  • the best way to learn about the standard classes (and you NEED to do that) is to do the relevant Sun tutorials (or equivalent) and browse through the J2SE javadocs. Commented Feb 16, 2010 at 1:03

4 Answers 4

3
...

private Entity[] data= new Entity[0];  // Or some value > 0
...

otherwise data is null the first time you access it in the method.

Sign up to request clarification or add additional context in comments.

2 Comments

It would be typical for an implementation like this to include a constructor with a "capacity" argument to determine the initial size of the array. See Java's ArrayList class.
Yep, it appears I didn't initialize the array properly. Thanks!
2

you're doing

position >= data.length

before initializing data

1 Comment

Yep, you and Carsten both spotted the same mistake of mine. Thanks!
0

(Is Entities a field? In that case you should call it entities. See http://java.sun.com/docs/codeconv/)

You should tell exactly that on which line the NPE is thrown. If it's in the initialize() method's second line, then probably the Entities field is null. If it's in the put() method, then probably it's because the data field is null.

1 Comment

Sorry for not being clear about the error. Thanks for your help!
0

Are you doing this just as a learning exercise? If not, why not use java.util.Vector, which provides a dynamic array for any Object?

1 Comment

I see! I guess I'll use a Vector to hold all the Entities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.