Results 1 to 15 of 15
  1. #1
    ddatta8 is offline Member
    Join Date
    Dec 2008
    Posts
    27
    Rep Power
    0

    Default unable to pass value from one class to another

    Hi,
    I have a Java class welcomeScreen show below. It reads a value studID and has a method "returnStudID()" which returns this value. I initialize an instance of welcomeScreen in a class mainMenu (below) where I use "returnStudID()" to read studID but when I try to print it, it shows a null value. Why is this happening? I'll really appreciate some help with this.
    Thanks.

    class welcomeScreen
    {
    private String studID, password;

    public void display(){
    System.out.println("Welcome to the Student Registration System!");
    System.out.println("****************************** *************");
    System.out.println("Enter student id:");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try {
    studID = br.readLine();
    }
    catch (IOException ioe) {
    System.out.println("IO error trying to read student id!");
    System.exit(1);
    }
    System.out.println("Enter password:");
    try {
    password = br.readLine();
    }
    catch (IOException ioe) {
    System.out.println("IO error trying to read password!");
    System.exit(1);
    }
    }

    public boolean student_authentication(){
    String fileStudID, filePassword;
    boolean foundStudID = false, foundPassword = false;

    try {
    StreamTokenizer sr = new StreamTokenizer(new FileReader("studID.txt"));
    sr.nextToken(); fileStudID = sr.sval;
    sr.nextToken(); filePassword = sr.sval;

    while (fileStudID != null) {
    if (fileStudID.equals(studID)){
    foundStudID = true;
    if (filePassword.equals(password)){
    foundPassword = true; break;
    }
    }
    sr.nextToken(); fileStudID = sr.sval;
    sr.nextToken(); filePassword = sr.sval;
    }
    }
    catch (IOException e) {
    System.out.println("Uh oh, got an IOException error!");
    e.printStackTrace();
    }

    if ((foundStudID == true) && (foundPassword == true)) return true;
    else return false;
    }

    public String returnStudID(){
    return studID;
    }
    }

    class mainMenu {

    int option, addCourseName;
    private courseAdd ca = new courseAdd();
    private courseDrop cd = new courseDrop();
    private welcomeScreen ws = new welcomeScreen();
    String studID = ws.returnStudID();......gives NULL value on printing.
    .......
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The welcomeScreen object won't have a valid studID String until you call display() on the same welcomeScreen object, and I don't see that you ever call this method.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. You may want to click on the Preview tab to make sure that your code is formatted correctly. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:

    Java Code:
    [code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    [/code]

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Display method

    Don't you have to call the display method in the welcomeScreen class to set the value for the studID variable?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    hey cjsl, estás un poco despacio esta noche, ¿no?
    que tengas una buena noche.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default nel...

    no soy lento... lo que pasa es que eres demasiado rapido !

    Saludos,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    LOL, and a good morning to you too!
    No matter, it appears that the intrepid original poster is going to be a post-and-gone.

  8. #8
    ddatta8 is offline Member
    Join Date
    Dec 2008
    Posts
    27
    Rep Power
    0

    Default

    Hi,
    I am sorry that I forgot to mention but I did call the "display()" in the "public static void main()" method which did initialize studID properly but then, I try to access it from mainMenu() and I get null value. Why does this happen? Please help.
    Thanks.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    without code showing the problem, who knows?

  10. #10
    ddatta8 is offline Member
    Join Date
    Dec 2008
    Posts
    27
    Rep Power
    0

    Default

    Hi,
    I have posted the entire code here.
    Thanks.

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.math.*;
    
    class welcomeScreen
    {
    	private String studID, password;
    		
    	public void display(){
    		System.out.println("Welcome to the Student Registration System!");
    		System.out.println("*******************************************");
    		System.out.println("Enter student id:");
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try {
                studID = br.readLine();
            } 
            catch (IOException ioe) {
                System.out.println("IO error trying to read student id!");
                System.exit(1);
            }
    		System.out.println("Enter password:");
            try {
                password = br.readLine();
            } 
            catch (IOException ioe) {
                System.out.println("IO error trying to read password!");
                System.exit(1);
            }
    	} 
    	
    	public boolean student_authentication(){
    	    String fileStudID, filePassword;
            boolean foundStudID = false, foundPassword = false;
    
            try { 
                StreamTokenizer sr = new StreamTokenizer(new FileReader("studID.txt"));
                sr.nextToken(); fileStudID = sr.sval;
                sr.nextToken(); filePassword = sr.sval;
                
                while (fileStudID != null) {
                	if (fileStudID.equals(studID)){
                    	foundStudID = true; 
                    	if (filePassword.equals(password)){
                    		foundPassword = true; break;
                    	}  	
                    }
                    sr.nextToken(); fileStudID = sr.sval;
                    sr.nextToken(); filePassword = sr.sval;              	 
                } 
            } 
            catch (IOException e) { 
                System.out.println("Uh oh, got an IOException error!");
                e.printStackTrace();
            }
    
            if ((foundStudID == true) && (foundPassword == true)) return true;
            else return false;
    	}
    	
    	public String returnStudID(){
    		return studID;
    	}
    }
    
    class mainMenu {
    	
    	int option, addCourseName;
    	private courseAdd ca = new courseAdd();
    	private courseDrop cd = new courseDrop();
    	private welcomeScreen ws = new welcomeScreen();
    	String studID = ws.returnStudID();
    	
    	public void display(){
    	
    	    System.out.println("studID" + " " + studID);	
    		ca.readMenu();         
    		
    		while (option != 3){
    		
    			System.out.println("Please choose from the following options!");
    			System.out.println("*******************************************");
    			System.out.println("1. Course Add:");
    			System.out.println("2. Course Drop:");
    			System.out.println("3. Exit:");
    		
            	try {
                	BufferedReader br = new BufferedReader(new InputStreamReader ( System.in ));
                	option = Integer.parseInt(br.readLine());
                
                	if (option == 1){      
                	      		
            			ca.showMenu();
            			
                	    BufferedReader br1 = new BufferedReader(new InputStreamReader ( System.in ));
                	    addCourseName = Integer.parseInt(br1.readLine());
                	
                	    if ((ca.checkDuplCourse(studID, addCourseName) == false) &&  (ca.checkNumStudInCourse(addCourseName) == true) && (ca.checkScheduleConflict() == false)){
                	    	if (ca.addCourse() == true) System.out.println("Course added to student schedule");
                		}
                		else System.out.println("Course cannot be added to student schedule");
                	}
                	else System.out.println("2");
            	} 
            	catch (IOException ioe) {
                	System.out.println("IO error trying to read option!");
                	System.exit(1);
            	}
            }
    	}
    	
    }
    
    class courseAdd {
    	
    	private String[] courseName = new String[5];
    	private String[] numStudInCourse = new String[5];
    	private String[] numStudEnrolled = new String[5];
    	private String[] daysOfClass = new String[5];
    	private String[] timeOfClassStart = new String[5]; 
    	private String[] timeOfClassEnd = new String[5]; 
    	private String[] studID = new String[15]; 
    	private String[] courseEnrolled = new String[15];
    	
    	public void readMenu() {
    		
    		int recCount = 0;             
    		
    		try { 
                StreamTokenizer sr = new StreamTokenizer(new FileReader("courses.txt"));
          	    
          	    while (sr.nextToken() != StreamTokenizer.TT_EOF) {
          	    	courseName[recCount] = sr.sval;
          	    	sr.nextToken(); numStudInCourse[recCount] = Double.toString(sr.nval);
          	    	sr.nextToken(); numStudEnrolled[recCount] = Double.toString(sr.nval);
            	  	sr.nextToken(); daysOfClass[recCount] = sr.sval;
            	  	sr.nextToken(); timeOfClassStart[recCount] = Double.toString(sr.nval);
            	  	sr.nextToken(); timeOfClassEnd[recCount] = Double.toString(sr.nval);
                    recCount++;
                } 
           	} 
            catch (IOException e) { 
               	System.out.println("Uh oh, got an IOException error!");
               	e.printStackTrace();
            }
    	}
    	
    	public void showMenu() { 
    		
    		int recCount = 0;
    		
    		while (recCount < 5){
    			System.out.println(recCount + "." + courseName[recCount]);
    			recCount++;
    		}
    	}
    	
    	public int readCourseEnrolled(){
    		int recCount = 0;             
    		
    		try { 
                StreamTokenizer sr = new StreamTokenizer(new FileReader("coursesEnrolled.txt"));
          	    
          	    while (sr.nextToken() != StreamTokenizer.TT_EOF) {
          	    	studID[recCount] = sr.sval;
            	  	sr.nextToken(); courseEnrolled[recCount] = sr.sval;
                    recCount++;
                } 
           	} 
            catch (IOException e) { 
               	System.out.println("Uh oh, got an IOException error!");
               	e.printStackTrace();
            }
            return recCount;
    		
    	}
    	
    	public boolean checkDuplCourse(String studentID, int addCourse){
    		
    		int i, j, recCount = readCourseEnrolled();
    		System.out.println("recCount" + " " + recCount);
    		System.out.println("studentID" + " " + studentID);
    		System.out.println("courseName[" + addCourse + "]" + " " + courseName[addCourse]);
    		
    		for (i=0; i<recCount; i++){
    		System.out.println(i); 
    		System.out.println(studID[i]); 
    		System.out.println(courseEnrolled[i]); 
    			if ((studentID.equals(studID[i])) && (courseName[addCourse].equals(courseEnrolled[i]))) 
    				break;
    		}
    	
    	    System.out.println("3");
    	    if (i < recCount) return true;
            else return false;
    	}
    	
    	public boolean checkNumStudInCourse(int addCourseName){
    		
    		if (!(numStudEnrolled[addCourseName].equals(numStudInCourse[addCourseName]))) 
    		    return true;
    		else return false; 
    	}
    	
    	public boolean checkScheduleConflict(){
    		return false;
    	}
    	
    	public boolean addCourse() {
    		return true;
    	}
    	
    }
    
    class courseDrop {
    	
    }
    
    public class SRS 
    { 
    	public static void main(String [] args)
    	{
    	
    	    boolean studentAuthenticated;	
    		welcomeScreen ws = new welcomeScreen();
    		mainMenu mn = new mainMenu();
    				
    		ws.display();
    		studentAuthenticated = ws.student_authentication();
    		
    		while ( studentAuthenticated == false){
    		    System.out.println("Incorrect login information.");
    			System.out.println("Please re-enter correct login information.");
    			ws.display();	
    			studentAuthenticated = ws.student_authentication();
    		}
    		
    		mn.display();
    	}
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You understand of course that if you declare a welcomeScreen object in the main method and another welcomeScreen object in the mainMenu class, that the two are completely unrelated, right? That information obtained in one is completely unavailable to the other, right?

  12. #12
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Yeah I see where the problem is. Fuburable explained it best. You are creating an instance of the variable in the main method of one class. You are then trying to access it from another class mainMenu, where it seems you created an entirely new instance of it and DIDN'T initialize StudID by calling display.

  13. #13
    ddatta8 is offline Member
    Join Date
    Dec 2008
    Posts
    27
    Rep Power
    0

    Default

    Ok. I get it now. Thanks very much.

  14. #14
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Arrow There's more than one way to do it.

    Quote Originally Posted by ddatta8 View Post
    Hi, I have posted the entire code here.
    Java Code:
    // ....
    class WelcomeScreen
    {
        MainMenu mainMenu;
        public static getInstance()
        {
            // see default constructor
            return new WelcomeScreen();//
        }
        public WelcomeScreen()
        {
            mainMenu = new MainMenu();
        }
        // .......
    }
    
    class MainMenu 
    {
        public MainMenu ()
        {
            ;
        }
        // ......
    }
    
    class CourseAdd
    {
        // .... the class name suggest an action, 
        // which is not has a / is a  
    }
    /**
    * Now you get a WelcomeScreen which has a Menu.
    * A menu is in fact a list of strings which invoke 
    * various methods or do other actions ......
    */
    public class StudentResearchSystems
    { 
        WelcomeScreen welcomeScreen;
        public static void main(String [] args)
        {
        
            boolean studentAuthenticated;   
            WelcomeScreen welcomeScreen = WelcomeScreen.getInstance();
            // .....
        }
    }
    There are some variations of the approach here, all may be discovered by getting this right the first time.

    That can be resolved by having only one Welcome Screen, which may be passed in constructors, achieved with a getWelcomeScreen() ... can get to be a time waster if you do not do the work here, now.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  15. #15
    ddatta8 is offline Member
    Join Date
    Dec 2008
    Posts
    27
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 11-20-2011, 09:22 PM
  2. How to pass value from servlet to midlet?
    By sharmaj2me in forum CLDC and MIDP
    Replies: 1
    Last Post: 12-05-2008, 03:52 PM
  3. [SOLVED] Unable to instantiate class?
    By xcallmejudasx in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 04:03 PM
  4. Pass by ref. A work around?
    By diRisig in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 07:25 PM
  5. how to pass array in java?
    By sivasayanth in forum New To Java
    Replies: 3
    Last Post: 01-13-2008, 04:33 PM

Posting Permissions

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