aspose file tools*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using for loop to find current Component" Watch "using for loop to find current Component" New topic
Author

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
Mike
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
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.

Thanks
Mike
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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: http://aspose.com/file-tools
 
subject: using for loop to find current Component