Forums Register Login

NullPointerException error

+Pie Number of slices to send: Send
So, I've got code that compiles fine, but gives a NullPointerException error when I try to run it. Specifically, it says:

Exception in thread "main" java.lang.NullPointerException
at mrchen.ButtonDrawPanel.<init>(rktest.java:884)
at mrchen.rktest.<init>(rktest.java:21)
at mrchen.rktest.main(rktest.java:901)

This happened when I changed my code from using a 2D array of strings


to a 2D array of "Square" objects:


Of course, I made a bunch of other changes to support this as well (method calls to get the String out of the object, for instance), and everything compiled fine, but like I said - when I run it, I get this NullPointerException error.

Any suggestions of common code errors that produce this sort of thing?

Thanks,
Mike
+Pie Number of slices to send: Send
When you create any array with objects in it, those objects will not be automatically initialized; basically, after creating that array, you have a 2D array with each of the 81 values being null.

Just initialize them all in a nested loop, and your error should be gone.
+Pie Number of slices to send: Send
What i would think is have you really had some objects into an array? Because what you have posted is just an array declaration -a placeholder.

The following code shows how an array should really be dealt in a program. Its for an 1D array of Square objects.



You can even initialize them in a loop provided if all them are initiated with similar kinda objects.
+Pie Number of slices to send: Send
Rob and Raghavan -

Thanks for the help with this!

I had just realized that this was the problem (i.e. Square array with no Square objects), but I'm glad to have confirmation from more experienced hands.

Much appreciated,
Mike
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 605 times.
Similar Threads
Jumping Problem
java.lang.NoSuchMethodError
expanding a multidimensional array?
CORBA COMM_FAILURE
CORBA COMM_FAILURE
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:37:21.