Results 1 to 6 of 6
Thread: Custom class in an array?
- 04-14-2009, 11:13 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 6
- Rep Power
- 0
-
You bet it's possible, and it's done often. Why not give it a try and see what happens?
- 04-14-2009, 11:24 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 6
- Rep Power
- 0
Well this is what I tried:
Originally Posted by brick.java And it's returning:
Originally Posted by BreakOut.java
--------------------Configuration: BreakOut - JDK version 1.6.0_13 <Default> - <Default>--------------------
java.lang.NullPointerException
at BreakOut.init(BreakOut.java:14)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:619)
Process completed.
-
Ah, you are now learning the difference between an array of primitive, say an int array, and an array of objects, say a brick array. With the primitive array, all you have to do is declare the array and use it -- it's ready immediately for your use.
With an object array on the other hand, you have to declare the array first, and then you have to initialize the objects in the array before you can use them. Think of creating an object array as if you were creating a parking lot. You have to fill it with cars first before you can use a car. So for this code:
try changing your init to:
and then see if it worksJava Code:public void init() { // initialize every brick in the block array for (int i = 0; i < block.length; i++) { block[i] = new brick(); } block[3].visible = true; block[4].x = 60; System.out.println(block[4].x); }
- 04-15-2009, 12:06 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 6
- Rep Power
- 0
Perfect, thanks a lot.
-
Similar Threads
-
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Importing custom class
By BigRed in forum EclipseReplies: 2Last Post: 02-14-2009, 02:12 PM -
Custom Class, Wrapper/Subclass?
By Moncleared in forum Advanced JavaReplies: 3Last Post: 02-09-2009, 11:08 PM -
Demonstration of the custom InputDialog class
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 07:56 PM -
Importing a Custom Class
By jfredrickson in forum New To JavaReplies: 3Last Post: 07-11-2007, 11:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks