Results 1 to 4 of 4
  1. #1
    JOHNINALBANY is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default 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));
    }

    }

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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

  3. #3
    JOHNINALBANY is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default Re: Class Instance initialization fails

    Sorry for the delay. Thanks for your valuable help.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Class Instance initialization fails

    Not a Java 2D problem. Moving to New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to check instance of a generic class?
    By chan_nguyen in forum New To Java
    Replies: 7
    Last Post: 09-08-2010, 04:20 AM
  2. create new instance of variable class
    By Fedor in forum New To Java
    Replies: 5
    Last Post: 04-12-2009, 08:13 PM
  3. create Instance of class in Javascript
    By TDMaster in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-09-2009, 04:26 PM
  4. Replies: 1
    Last Post: 11-17-2008, 09:32 AM
  5. Naming a class instance with a variable
    By pikalex88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2008, 06:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •