Building an Array of Objects inside of another Object's Constructor.
Answered
- Need more info
- Answered
- Working on it
in
Programming Questions
•
3 years ago
Hey there,
I'm just starting on a project where I want to make a display object that constructs and array of pixel objects when it is created. I thought I had the concepts down but I get a "java.lang.NullPointerException". If anyone has run into something similar and knows the proper procedure could you let me know?
Thanks,
Chris
My Code (I've simplified it down to the offending structure)
- Display myDisplay = new Display();
- class Display {
- Pixel[] pixelArray;
- Display() {
- build();
- }
- void build() {
- for ( int i = 0; i < 4; i++ ) {
- pixelArray[i] = new Pixel();
- }
- }
- }
- class Pixel {
- Pixel() {
- }
- }
My Error
- Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException
- at processing.core.PApplet.main(PApplet.java:7254)
- Caused by: java.lang.NullPointerException
- at sketch_aug15a$Display.build(sketch_aug15a.java:32)
- at sketch_aug15a$Display.<init>(sketch_aug15a.java:27)
- at sketch_aug15a.<init>(sketch_aug15a.java:20)
- at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
- at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
- at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
- at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
- at java.lang.Class.newInstance0(Class.java:355)
- at java.lang.Class.newInstance(Class.java:308)
- at processing.core.PApplet.main(PApplet.java:7252)
1