Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Declared variables not carrying over

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Declared variables not carrying over

    Hello,
    I have in one class a JFrame declared like this:
    public class copyPastePopup extends JFrame {
     
    	JTextField cpText;
    	String text;
     
    	public copyPastePopup(String heading, String inputText) {
    		inputText = text;
    with other code following it. I then have that frame called like this:
    copyPastePopup popup = new copyPastePopup("Encoded Message",finalMessage);
    where finalMessage is a string. However, when I run the program, the JFrame sees the variables as null even though they have a string fed to them.
    Any Idea whats going on?

    evanscott815


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Declared variables not carrying over

    First off, I'd recommend using the standard naming conventions- classes start with an upper-case letter, methods and variables start with a lower-case letter. Seems trivial but it makes code a lot easier to read.

    But think about what you're doing: you take inputText as a parameter. You then throw away whatever the value is and set it equal to text, which hasn't been set to anything yet. Is that really what you want to do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Declared variables not carrying over

    Yes, since text is a class-wide variable. I use it in other functions in that class.
    However, even when I check the value of inputText, it is still null.
    Personally, I like to use the nocaps-Caps format for names.
    Last edited by evanscott815; October 4th, 2012 at 10:48 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Declared variables not carrying over

    Quote Originally Posted by evanscott815 View Post
    Yes, since text is a class-wide variable. I use it in other functions in that class.
    That part is fine.

    Quote Originally Posted by evanscott815 View Post
    However, even when I check the value of inputText, it is still null.
    That's because you're setting it equal to text, which hasn't been set yet, so it's null. It's like if you do this:
    int x = 0;
    int y = 7;
    y = x;
    ...and then ask why y isn't equal to 7.

    Quote Originally Posted by evanscott815 View Post
    Personally, I like to use the nocaps-Caps format for names.
    For your own purposes I suppose that's fine, but you're asking other people to read your code. Using non-standard naming conventions screams at more experienced developers, making it harder to actually see what's going on in your code. Shouldn't you make it as easy as possible to help you?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Declared variables not carrying over

    Yeah, I get it. I will go through and rename my classes.
    How do you suggest I set text equal to inputText?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Declared variables not carrying over

    Quote Originally Posted by evanscott815 View Post
    How do you suggest I set text equal to inputText?
    Well, you just said it. Right now you're setting inputText equal to text. It sounds like you might want to go the other way around?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    evanscott815 (October 4th, 2012)

  8. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Declared variables not carrying over

    Ohhh... *facepalms*
    That helps!
    Thank you!

Similar Threads

  1. append to text field declared out of class
    By chopficaro in forum Java Theory & Questions
    Replies: 1
    Last Post: July 25th, 2012, 03:23 PM
  2. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 07:34 PM
  3. Replies: 9
    Last Post: December 8th, 2011, 06:11 AM
  4. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  5. JFrame declared as setAlwaysOnTop doesn't stay on top during slide show
    By ravindra_appikatla in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 09:08 AM