Results 1 to 4 of 4
- 11-13-2010, 04:05 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 17
- Rep Power
- 0
Class Instance initialization fails
The following statements trigger an error (Exception in thread "main" java.lang.NullPointerException) in the below class. Why?
Rect[0]=new Rectangle2D.Double(50,50,70,70);
Rect[1]=new Rectangle2D.Double(80,80,100,100);
Due to the above error I am forced to create the instances like this (see below code):
Rectangle2D[] Rect1 = {new Rectangle2D.Double(50,50,70,70),new Rectangle2D.Double(80,80,100,100)};
In the event of having many rectangles the above statement would be long and difficult to read. Is there a fix for this? I would like to initialize every single Rectangle like this Rect[i]= new Rectangle2D.Double(6,7,10,10);
Thanks
*****************************
********Code*****************
*****************************
class Frame extends JFrame {
Rectangle2D[] Rect;
Frame(){
//The following statements trigger an error:
//Error: Exception in thread "main" java.lang.NullPointerException
Rect[0]=new Rectangle2D.Double(50,50,70,70);
Rect[1]=new Rectangle2D.Double(80,80,100,100);
// The following statement works
//In the event of having many rectangles this would be difficult to read
Rectangle2D[] Rect1 = {new Rectangle2D.Double(50,50,70,70),new Rectangle2D.Double(80,80,100,100)};
setTitle("Test");
setSize(300,300);
add(new DrawFigures(Rect));
}
}
- 11-13-2010, 04:23 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
This just declares the array:
Java Code:MyType[] myArray;
You need to initialize it as well:
Java Code:MyType[] myArray = new MyType[25]; // if we want an array of 25 MyType objects
- 07-07-2012, 03:29 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 17
- Rep Power
- 0
Re: Class Instance initialization fails
Sorry for the delay. Thanks for your valuable help.
- 07-08-2012, 08:43 PM #4
Similar Threads
-
How to check instance of a generic class?
By chan_nguyen in forum New To JavaReplies: 7Last Post: 09-08-2010, 05:20 AM -
create new instance of variable class
By Fedor in forum New To JavaReplies: 5Last Post: 04-12-2009, 09:13 PM -
create Instance of class in Javascript
By TDMaster in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-09-2009, 05:26 PM -
Stack class/problem in stackSize Initialization and usage of pop() function
By Mazharul in forum New To JavaReplies: 1Last Post: 11-17-2008, 10:32 AM -
Naming a class instance with a variable
By pikalex88 in forum New To JavaReplies: 3Last Post: 09-30-2008, 07:27 PM
Bookmarks