Results 1 to 4 of 4
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default Creating an array of children while excluding grandchildren.

    I am trying to create a new Array to be placed into a combo box but I cannot pull just 1 generation. My tree is set-up like this.

    ServerGroup (has children)
    +Servers (has children)
    +Targets ( doesn't have children)


    These are the relevant parts of each class
    Java Code:
    //NewDeploymentView.Java
    	    Server[] servers = ServerGroup.getChildren();
    	    String[] names = null;
    	    for(int i = 0; i < servers.length; i++){
    	    	names[i] = servers[i].getName(); 
    	    	System.out.println("Server "+i+" "+names[i]);
    	    }
    	    c.setItems(names);
    Java Code:
    //ServerGroup.Java
    	public static Server[] getChildren() {
    		return (Server[]) serverGroupChildren.toArray(new Server[serverGroupChildren.size()]);
    	}
    
    	public static boolean hasChildren() {
    		return serverGroupChildren.size()>0;
    	}
    Java Code:
    //Server.Java
    public boolean hasChildren() {
    		return serverChildren.size()>0;
    	}
    	public Target[] getChildren() {
    		return (Target[]) serverChildren.toArray(new Target[serverChildren.size()]);
    	}
    Java Code:
    //Target.Java
    	public Server getServerParent(){
    		return serverParent;
    	}
    	public boolean hasChildren(){
    		return false;
    	}
    I receive this warning
    Java Code:
    java.lang.NullPointerException
    	at groups.ServerGroup.getChildren(ServerGroup.java:55)
    	at views.NewDeploymentView.createPartControl(NewDeploymentView.java:61)
    I believe what is happening is when I call ServerGroup.getChildren() it digs down and tries to return the great grandchildren(which do not exist) because it doesn't know to stop and just return its own children. I think that serverChildren.size() within Server.Java is where null is but don't know how to work around this or stop it from going this deep. Any help?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    "at groups.ServerGroup.getChildren(ServerGroup.java:55 )"
    What object is at line 55 in ServerGroup's getChildren method that is null? Then why is it null? Can you test if its null and not call a method/reference a variable if it is null?

    What is the value of serverGroupChildren when getChildren is called?

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    return serverGroupChildren.size()>0; is line 55. It shouldn't be null because Server objects are the children.

    I changed it to return true and now get
    Java Code:
    java.lang.NullPointerException
    	at views.NewDeploymentView.createPartControl(NewDeploymentView.java:65)
    line 65 is names[i] = servers[i].getName();

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    fixed it. Just redefined String[] names = new String[servers.length);

    Thanks for the insight

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 01:40 PM
  2. How to load a directory in JTree with Children(On expansion)
    By aneesahamedaa in forum AWT / Swing
    Replies: 0
    Last Post: 10-13-2008, 02:23 PM
  3. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 07:14 AM
  4. Creating Array of LinkedList
    By sasikumardr in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 11:25 AM
  5. creating array at runtime
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 11-08-2007, 11:06 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
  •