Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Array?

Options
  • 23-04-2005 12:01am
    #1
    Closed Accounts Posts: 7


    Hi
    This is my problem.
    I have a constructor that recieves as an argument an array of interfaces.
    Now inorder for me to create an instance of this interface I have to use a Factory Class . It gives me a NullPointerException on the creation of the TaskData object.
    eg:
    private InterfaceTest [] it;
    InterFaceTestDefaultFactory _INFTDF1= new InterFaceTestDefaultFactory();
    InterFaceTest _inftdf1 = (InterFaceTest) _INTDF1.create();
    InterFaceTestDefaultFactory _INFTDF2= new InterFaceTestDefaultFactory();
    InterFaceTest _inftdf2= (InterFaceTest) _INTDF2.create();

    it[0]=_inftdf1;
    it[1]=_inftdf2;
    // HOW CAN I Initalize this array, when i have to use this InterfaceTestDefaultFactory() to create an instance of InterFaceTest???


    //constructor for the object I want to create:TestData(InterFaceTest[]);

    TestData TD = new TestData(it);


Comments

  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    it = new InterfaceTest[*number-you-want*];

    Unless I'm badly mistaken. An array of references.


  • Closed Accounts Posts: 7 kob2376


    As I said the factory method has to be used to create the interface object and ref, so it = new InterfaceTest[*number-you-want*];
    is not an option.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Try it ;)

    Something like:

    Button[] buttons=new Button[10];
    buttons[0]=new Button("Test");

    is perfectly valid. The Button[]=new Button... is just defining an array of button references. It should be the same for your class.


  • Registered Users Posts: 3,012 ✭✭✭BizzyC


    An array can't be initialised unless you give it a length, untill you do so it'll be null, causing the exception when you enter something into it.
    If you're not able to set the length when declaring the object, I'd suggest using a hashtable or linked-list rather than an array.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Oh, he doesn't know the length! Missed that (silly me :rolleyes: )


  • Advertisement
  • Registered Users Posts: 4,276 ✭✭✭damnyanks


    Change to Array list?


  • Closed Accounts Posts: 92 ✭✭tempest


    kob2376 wrote:
    As I said the factory method has to be used to create the interface object and ref, so it = new InterfaceTest[*number-you-want*];
    is not an option.

    Ask yourself the following questions?
    1. Does the factory method create a single object or multiple objects?

    If the factory method creates multiple objects then it is the factory methods responsibility to initialise the array and return it to you initialised.

    If the factory method creates a single object then it is the callers responsibility to know the number of items which will be created and to initialise the storage, be it an array or a LinkedList or a HashMap etc. It looks like this is your situation. This leads to asking yourself the next question.

    2. Why don't I know the number of items I will create?

    If the answer is: because it is truly dynamic and not really available to you yet then the solution is to use a dynamic data structure such as a LinkedList or an ArrayList.

    If the answer is: oh well actually I do know because it's computable from this variable or that variable then the solution is to initialise the array at declaration time. i.e.
    rsynnott wrote:
    it = new InterfaceTest[*number-you-want*];


Advertisement