Results 1 to 20 of 20

Thread: Visible string

  1. #1
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Visible string

    Hi

    What's wrong in my code bcs I dont see String password outside Test class. How can I fix it ?
    Java Code:
    package aplikacja.glowna;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    
    public class Test {
    
    	@SuppressWarnings("unused")
    	public void testPassword() throws FileNotFoundException, IOException {
    
    		// Reading properties file in Java example
    		Properties props = new Properties();
    		FileInputStream plik = new FileInputStream("aplikacja.properties");
    
    		// loading properites from properties file
    		props.load(plik);
    
    		// reading proeprty
    		String username = props.getProperty("jdbc.username");
    		String password = props.getProperty("jdbc.password");
    		String driver = props.getProperty("jdbc.driver");
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Visible string

    Your password variables (and others) are local to the method testPassword(). So their scope is constrained to that method.

    You need to make them instance variables of the class
    I recommend making them private and using a getMethod to retreive them.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: Visible string

    Can I please for example ?

    Java Code:
    package aplikacja.glowna;
    
    import javax.swing.JLabel;
    
    public class OProgramie {
    
    	Test testPassword = new Test();	
    	JLabel autorLabel = new JLabel(Test.class.getMethods(password));
    	
    }
    Last edited by mr.suchy; 03-13-2013 at 12:32 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Visible string

    Java Code:
    private String password; // place this below your class declaration.
    
    ...
    elsewhere in your code, do not re-declare password as a String.
    ...
    
    
    // instance method to get password
    public String getPassword() {
         return password;
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: Visible string

    Thanks for help but still have a problem.
    Java Code:
    package aplikacja.glowna;
    
    import javax.swing.JLabel;
    
    public class OProgramie {
    
    	Test testPassword = new Test();	
    	JLabel autorLabel = new JLabel(testPassword.getPassword());
    	
    }
    Java Code:
    package aplikacja.glowna;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    
    public class Test {
    
    	private String password;
    
    	@SuppressWarnings("unused")
    	public void testPassword() throws FileNotFoundException, IOException {
    
    		// Reading properties file in Java example
    		Properties props = new Properties();
    		FileInputStream plik = new FileInputStream("aplikacja.properties");
    
    		// loading properites from properties file
    		props.load(plik);
    
    		// reading proeprty
    		String username = props.getProperty("jdbc.username");
    		String password = props.getProperty("jdbc.password");
    		String driver = props.getProperty("jdbc.driver");
    	}
    
    	public String getPassword() {
    		return password;
    	}
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Visible string

    Quote Originally Posted by jim829 View Post
    Java Code:
    ...
    elsewhere in your code, do not re-declare password as a String.
    ...
    Regards,
    Jim

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: Visible string

    Java Code:
    @SuppressWarnings("unused")
    Can I suggest removing this?
    Because the warnings it is suppressing are actually meaningful, especially in this case.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: Visible string

    Quote Originally Posted by jim829 View Post
    Java Code:
    private String password; // place this below your class declaration.
    
    ...
    elsewhere in your code, do not re-declare password as a String.
    ...
    
    
    // instance method to get password
    public String getPassword() {
         return password;
    }
    Regards,
    Jim
    Thanks but still I don't get where is a problem. I must add it on OProgramie class or Test class ? I understand thant I cant re-declare password as a String but I have to in this place (myabe I wrong)
    Java Code:
    String password = props.getProperty("jdbc.password");
    Or maybe I need to change password to passwordTwo in this place. Thanks anyway for help.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Visible string

    Remove the String declaration from password.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: Visible string

    @jim829 thanks for your help and patience. I dont have error and warnings but "password" returs null and I dont know why.

    Java Code:
    package aplikacja.glowna;
    
    import javax.swing.JLabel;
    
    public class OProgramie {
    
    	Test testPassword = new Test();	
    	JLabel autorLabel = new JLabel("Nazwa "+testPassword.getPassword());
    	
    }
    Java Code:
    package aplikacja.glowna;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    
    public class Test {
    
    	private String password;
    
    	public void testPassword() throws FileNotFoundException, IOException {
    
    		// Reading properties file in Java example
    		Properties props = new Properties();
    		FileInputStream plik = new FileInputStream("aplikacja.properties");
    
    		// loading properites from properties file
    		props.load(plik);
    
    		// reading proeprty
    		String username = props.getProperty("jdbc.username");
    		password = props.getProperty("jdbc.password");
    		String driver = props.getProperty("jdbc.driver");
    	}
    
    	public String getPassword() {
    		//return password;
    		//String password = "hehe";
    		return password;
    	}
    }

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Visible string

    You never seem to call method public void testPassword(). Try this:
    Java Code:
    Test testPassword = new Test();
    testPassword.testPassword();
    JLabel autorLabel = new JLabel("Nazwa "+testPassword.getPassword());
    You might discover that your naming is not very clear: testPassword.testPassword(). Try renaming the testPassword() method to initialize() for example, or move the code into a constructor.

    Edit: this won't compile if you initialize objects in the fields of OProgramie. Also here, try moving the initializing code to the constructor. That's why constructors exist anyway:
    Java Code:
    public class OProgramie {
     
       private Test testPassword;
       private JLabel autorLabel;
       
       public OProgramie {
          testPassword = new Test();
          testPssword.testPassword();
          autorlabel = new JLabel("Nazwa "+testPassword.getPassword());
       }
    }
    Last edited by SurfMan; 03-13-2013 at 02:41 PM.

  12. #12
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: Visible string

    Quote Originally Posted by SurfMan View Post
    You never seem to call method public void testPassword(). Try this:
    Java Code:
    public class OProgramie {
     
       private Test testPassword;
       private JLabel autorLabel;
       
       public OProgramie {
          testPassword = new Test();
          testPssword.testPassword();
          autorlabel = new JLabel("Nazwa "+testPassword.getPassword());
       }
    }
    I see errors when I past your code. Maybe I must return and try display something easier in java like "Hello World" :-) My code in last post is still current.

    Best regards!

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: Visible string

    What errors?
    I can see one, which you should be able to fix yourself...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: Visible string

    yeah, good to know. Thanks anyway.


    Best regards

  15. #15
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Visible string

    It was a typo which I did notice when I was done, but decided to leave it in there as an excercise for the reader... Apparently you found it. Did you fix it?

  16. #16
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: Visible string

    No, I dont fix it bcs I learn Java from few days and I dont have the same knowledge as you. SO if you can please correct my code and tell me what is wrong. Thanks once again.

  17. #17
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Visible string

    I *have* corrected your code, and I told you what's wrong. See reply #11. The method that kicks off the reading of the Properties file is never called. I even gave you the correct code, albeit with a typo.

  18. #18
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: Visible string

    Of course you have right but still I have "Syntax error on token "public", class expected after this token" in public OProgramie(11 line).
    Java Code:
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import javax.swing.JLabel;
    
    public class OProgramie {
    	  
    	   private Test testPassword;
    	   private JLabel autorLabel;	   
        
    	   public OProgramie{
    	      testPassword = new Test();
    	      testPassword.testPassword();
    	      autorLabel = new JLabel("Nazwa "+testPassword.getPassword());
    	   }
    	}

  19. #19
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    929
    Rep Power
    2

    Default Re: Visible string

    Java Code:
    public OProgramie() {
              testPassword = new Test();
              testPassword.testPassword();
              autorLabel = new JLabel("Nazwa "+testPassword.getPassword());
           }
    LOL I didn't even notice that one... Sorry for the second typo. It's a constructor, those have () behind it. If you didn't spot this by yourself, then read up on constructors: )

  20. #20
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: Visible string

    I dont know why but it's works :-)
    Java Code:
    package aplikacja.glowna;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import javax.swing.JLabel;
    
    public class OProgramie {
    	  
    	   public Test testPassword;
    	   public JLabel autorLabel;	   
        
    	   public OProgramie() throws FileNotFoundException, IOException {
    	          testPassword = new Test();
    	          testPassword.testPassword();
    	          setAutorLabel(new JLabel(testPassword.getPassword()));
    	       }
    
    	/**
    	 * @return the autorLabel
    	 */
    	public JLabel getAutorLabel() {
    		return autorLabel;
    	}
    
    	/**
    	 * @param autorLabel the autorLabel to set
    	 */
    	public void setAutorLabel(JLabel autorLabel) {
    		this.autorLabel = autorLabel;
    	}
    	}
    Thanks once again!

Similar Threads

  1. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  2. GUI is visible but content is not.
    By seemant.bisht in forum AWT / Swing
    Replies: 3
    Last Post: 10-07-2009, 06:28 PM
  3. JTextField not visible?
    By McChill in forum New To Java
    Replies: 2
    Last Post: 05-20-2009, 12:17 AM
  4. Text are not visible in GUI
    By VinTiger in forum New To Java
    Replies: 5
    Last Post: 05-15-2009, 08:14 AM
  5. constructor ... is not visible
    By emceenugget in forum New To Java
    Replies: 2
    Last Post: 09-27-2008, 05:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •