my dog learned polymorphism
The moose likes Beginning Java and the fly likes using for loop to find current Component Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using for loop to find current Component" Watch "using for loop to find current Component" New topic

using for loop to find current Component

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi All,

I am having a difficult time using a for loop to find the current Component in my GUI using CardLayout. I am putting this in the beginning Java because it is more a poroblem of understanding
how to use the for loop, or Alorithms in general<-- is this considered an algorithm, or logic?

here is a small code snippet

I am getting a NullPointerException all the time.This type of operation seems to be my biggest problem at the moment.Are there any good books or online tutorials i can refer to?

thanks in advance
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15093

In which line exactly does the NullPointerException happen? Look carefully at that line to see what might be null there that should not be null.

You get a NullPointerException if you try to call a method or get the value of a member variable on a variable that is null.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi Jesper

Thanks for your reply, I actually think i have it working(to a certain point!) here is what i have done

when changePanel1 is showing, it also prints changePanel1 correctly and when the other 2 are showing then it doesn't print anything which is correct.
however i have some JTextFields and JButtons on each panel with the same names in each panel

if i use lbl4.setText(txt1.getText()) instead of the System.out.println(getCurrentPanel().getName()); the text gets set on the third panel? and not always right away?

First question is : does the the method getCurrentPanel() look correct to you?

second question is: why would the text then get set on a different panel? or can i not have JTextFields and JButtons with the same names on different panels , even if i specify which panel is showing?

I hope this is understandable ,if not i will post my current code completely. If everything looks OK then maybe this should be switched to the Swing Forum then.

Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Why do you have two nested loops, both over cardPanel.getComponents()? You should remove one of them. I would choose the inner one. Also, you call getComponents() for each iteration of the inner loop. You should cache its results, which makes getComponent(i) obsolete as you can replace it with direct array access. So one of the following should be good:

How To Ask Questions How To Answer Questions
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Thank you Rob,

I will see how those work out.
I agree. Here's the link:
subject: using for loop to find current Component
It's not a secret anymore!