Results 1 to 4 of 4

Thread: Returning object arrays

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Returning object arrays

    Im having a refrencing problem that i am not too sure how to deal with. When i return an Object array from a method what do i store it in? As an example i have a simple method that when invoked creates a new Object array and adds new instances of other Objects(JButtons) to a JPanel then returns the JButton[] array. When the method returns what to i store the returned refrence in? passDigits(digits) is passing a null Object array.
    Code:
    class X{
     JButton[] digits;
     JPanel dig = new JPanel();
     digits = addDigits(dig);
     passDigits(digits);
    }
    Code:
    public JButton[] addDigits(JPanel dig){
        JButton[] digits = new JButton[10];
        for(int i = 9; i <= 0; --i){
            digits[i] = new JButton(String.valueOf(i));
            dig.add(digits[i]);
          }
           return digits;
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I don't see a problem with the code...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I found out why i was getting a NullPointerException. The loop never gets executed. When i tried to refrence a element from the JButton[] array thats when the exception is thrown. Sorry my fault.

  4. #4
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228
    Strange.
    I was going to tell you to check your condition in the for loop, but the thread was locked. Obviously, the thread is open now.

Posting Permissions

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



Click Here to Expand Forum to Full Width