Results 1 to 4 of 4
  1. #1
    onstock is offline Member
    Join Date
    Nov 2011
    Location
    Lithuania
    Posts
    2
    Rep Power
    0

    Default Call a public string variable

    I took a sample code for a drop-down menu for my applet and placed it in a InputDialogWithDropdownListbox class.

    Java Code:
    public class InputDialogWithDropdownListbox{
    	public String input;
                   public void drop(Valstybe G[], int n) {
                   String[] ListZemynai = new String[10];
                       for(int i=0;i<n;i++){
                             ListZemynai[i]=G[i].gautiZemyna();   
                                            }
                   final List<String> lst = Arrays.asList(ListZemynai);   
                   final HashSet<String> set = new HashSet<String>(lst);   
                   String[] unique = (String[]) set.toArray(new String[set.size()]);  
                   input = (String) JOptionPane.showInputDialog(null, "Pasirinkite zemyna",
            "Pasirinkimas", JOptionPane.QUESTION_MESSAGE, null, unique, unique[1] // 
                   ); 
            }
    }
    Here is where I draw the drop-down menu:

    Java Code:
    class SurastiDialogas extends JDialog {
    	  SurastiDialogas(){
    		InputDialogWithDropdownListbox menii = new InputDialogWithDropdownListbox();
                           menii.drop(A,n);
    		JButton ok = new JButton("OK");
    		System.out.println(menii.input ); //returns a value
    		
    	}
    }
    I have invoked String type public variable input located inside drop method. And it did print out the string value which I chose in the dialog.
    The problem is, I cannot use such string variable in another method which I need to compare to another list of strings. Here is the code:

    Java Code:
    public Valstybe[] surasti (Valstybe G[], int n){
    		Valstybe A[] = new Valstybe[10];
                    InputDialogWithDropdownListbox ZemynaiOption = new InputDialogWithDropdownListbox();  
                    System.out.println("The value of input located in another class: " + "\n");
                    [B]System.out.println(ZemynaiOption.input); [/B] //returns null
                    String[] valstybes = new String[10];   
                    String[] zemynai = new String[10]; 
                    int i=0;
    		for (int a=0;a<n;a++){
    
    			valstybes[a]=G[a].gautiPav(); //we get a value from a file (skipped code) and then parse to array
    			zemynai[a]=G[a].gautiZemyna(); // ---------- "" ----------
                            if(ZemynaiOption.equals(zemynai[a])) { //checking where zemynai[a] equals input string from drop-down menu
    						A[i]=G[a];  //if equals, we form a new array
    						i++; 
    						}
    	
    			}	
    			return A;  //returns filtered array
                    }
    System.out.println(ZemynaiOption.input); here returns null while in method SurastiDialogas menii.input returns a string value. What am I missing here to return input value inside Valstybe method?
    Last edited by onstock; 11-22-2011 at 06:57 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Call a public string variable

    System.out.println(ZemynaiOption.input); here returns null
    The println method does not return any value. It is defined as void. Can you explain what you mean by "returns"?
    Are you saying that the above println prints out a null for the value of the input variable in the ZemynaiOption object?
    Does that variable have a value other than null? Where and when is the variable: input assigned a value?

  3. #3
    onstock is offline Member
    Join Date
    Nov 2011
    Location
    Lithuania
    Posts
    2
    Rep Power
    0

    Default Re: Call a public string variable

    Quote Originally Posted by Norm View Post
    The println method does not return any value. It is defined as void. Can you explain what you mean by "returns"?
    Are you saying that the above println prints out a null for the value of the input variable in the ZemynaiOption object?
    Exactly.


    Java Code:
    input = (String) JOptionPane.showInputDialog(null, "Pasirinkite zemyna",
            "Pasirinkimas", JOptionPane.QUESTION_MESSAGE, null, unique, unique[1] // 
                   );
    Here is where variable: input gets assigned with a value.

    For instance, I have a list in drop-down menu with some values (values are taken from a text file):
    Java Code:
    Europe
    Asia
    Africa
    After selecting some value (Europe, for example) and clicking "OK" button, the code
    Java Code:
    System.out.println(menii.input ); //returns a value
    prints out value: "Europe", while

    Java Code:
    System.out.println(ZemynaiOption.input);
    prints out value: "null".

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Call a public string variable

    To see when the variable input gets its value, add a println just after the assignment statement that sets its value to print out its value.
    You may be getting its value before it gets a valid value.

Similar Threads

  1. Replies: 4
    Last Post: 10-11-2011, 11:48 PM
  2. Replies: 10
    Last Post: 07-10-2011, 08:37 AM
  3. Replies: 12
    Last Post: 12-12-2010, 06:04 PM
  4. Public static void main (String args[])
    By arefeh in forum New To Java
    Replies: 12
    Last Post: 01-28-2010, 11:58 AM
  5. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:58 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
  •