Array of references

Discussion in 'Java' started by Yan, Oct 22, 2003.

  1. Yan

    Yan Guest

    Hi.

    I have plenty of experiance in C/C++, but this Java no-pointer policy
    is driving me crazy. I want to create an array of references - what
    would be an array of pointers in C.
    I try to do the following:

    JTree[] trees = new JTree[2];
    ....
    JTree tree1 = new JTree();
    JTree tree2 = new JTree();


    I already have 2 trees and I want to reference them using the array,
    so I do:

    trees[0] = tree1;
    trees[1] = tree2;

    but when I try to access trees[0], for example I get a null pointer
    exception. How can I do this stuff.

    Thank you in advance
    Yan
    Yan, Oct 22, 2003
    #1
    1. Advertising

  2. On 22 Oct 2003 09:28:19 -0700, Yan wrote:
    > I have plenty of experiance in C/C++, but this Java no-pointer
    > policy is driving me crazy. I want to create an array of references
    > - what would be an array of pointers in C. I try to do the
    > following:
    >
    > JTree[] trees = new JTree[2];
    > ...
    > JTree tree1 = new JTree();
    > JTree tree2 = new JTree();
    >
    >
    > I already have 2 trees and I want to reference them using the array,
    > so I do:
    >
    > trees[0] = tree1;
    > trees[1] = tree2;


    So far so good.

    > but when I try to access trees[0], for example I get a null pointer
    > exception. How can I do this stuff.


    There's nothing wrong with the few lines of code you've posted. Your
    problem is due to something else.

    Show a complete example that has the problem you describe.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Oct 22, 2003
    #2
    1. Advertising

  3. Yan

    ak Guest

    Yep, your code seems ok.

    you can do also following:

    JTree[] trees = new JTree[count];
    for(int i = 0; i < count; i++) {
    trees = new JTree();
    }



    "Yan" <> schrieb im Newsbeitrag
    news:...
    > Hi.
    >
    > I have plenty of experiance in C/C++, but this Java no-pointer policy
    > is driving me crazy. I want to create an array of references - what
    > would be an array of pointers in C.
    > I try to do the following:
    >
    > JTree[] trees = new JTree[2];
    > ...
    > JTree tree1 = new JTree();
    > JTree tree2 = new JTree();
    >
    >
    > I already have 2 trees and I want to reference them using the array,
    > so I do:
    >
    > trees[0] = tree1;
    > trees[1] = tree2;
    >
    > but when I try to access trees[0], for example I get a null pointer
    > exception. How can I do this stuff.
    >
    > Thank you in advance
    > Yan
    ak, Oct 22, 2003
    #3
  4. Yan

    Yan Guest

    Sorry.

    Got the problem - it was somewhere else. I'm using 1 tree model and 2 views
    in different tabs, so I need to know which tab I am in right now, so the
    current tree pointer wasn't initialized in the right place.

    thanx
    Yan
    Yan, Oct 23, 2003
    #4
  5. Yan

    Darren Guest

    The following works fine for me, I'd say you have another mistake in
    your code.


    import javax.swing.JTree;

    public class Test {

    public static void main( String[] args ) {
    JTree[] trees = new JTree[2];
    JTree tree1 = new JTree();
    JTree tree2 = new JTree();

    trees[0] = tree1;
    trees[1] = tree2;

    System.out.println( trees[0] );
    System.out.println( trees[1] );
    }

    }


    Yan wrote:

    > Hi.
    >
    > I have plenty of experiance in C/C++, but this Java no-pointer policy
    > is driving me crazy. I want to create an array of references - what
    > would be an array of pointers in C.
    > I try to do the following:
    >
    > JTree[] trees = new JTree[2];
    > ...
    > JTree tree1 = new JTree();
    > JTree tree2 = new JTree();
    >
    >
    > I already have 2 trees and I want to reference them using the array,
    > so I do:
    >
    > trees[0] = tree1;
    > trees[1] = tree2;
    >
    > but when I try to access trees[0], for example I get a null pointer
    > exception. How can I do this stuff.
    >
    > Thank you in advance
    > Yan
    Darren, Oct 23, 2003
    #5
  6. Yan wrote:

    > I already have 2 trees and I want to reference them using the array,
    > so I do:
    >
    > trees[0] = tree1;
    > trees[1] = tree2;
    >
    > but when I try to access trees[0], for example I get a null pointer
    > exception. How can I do this stuff.


    Are you initializing the reference in trees[0]? Unlike in C++, Java does
    not initialize the elements of an array of object references.

    --
    Darryl L. Pierce <>
    Visit the Infobahn Offramp - <http://bellsouthpwp.net/m/c/mcpierce>
    "What do you care what other people think, Mr. Feynman?"
    Darryl L. Pierce, Oct 24, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    415
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    435
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    589
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    694
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    821
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page