Results 1 to 15 of 15
- 12-27-2008, 02:40 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 27
- Rep Power
- 0
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.
.......
}
-
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.
-
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]
- 12-27-2008, 02:53 AM #4
Display method
Don't you have to call the display method in the welcomeScreen class to set the value for the studID variable?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
hey cjsl, estás un poco despacio esta noche, ¿no?
que tengas una buena noche.
- 12-27-2008, 04:11 PM #6
nel...
no soy lento... lo que pasa es que eres demasiado rapido !
Saludos,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
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.
- 12-28-2008, 01:57 AM #8
Member
- Join Date
- Dec 2008
- Posts
- 27
- Rep Power
- 0
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.
-
without code showing the problem, who knows?
- 12-28-2008, 03:52 AM #10
Member
- Join Date
- Dec 2008
- Posts
- 27
- Rep Power
- 0
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(); } }
-
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-28-2008, 05:04 AM #12
Member
- Join Date
- Dec 2008
- Posts
- 55
- Rep Power
- 0
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.
- 12-28-2008, 01:09 PM #13
Member
- Join Date
- Dec 2008
- Posts
- 27
- Rep Power
- 0
Ok. I get it now. Thanks very much.
- 12-28-2008, 01:32 PM #14
There's more than one way to do it.
There are some variations of the approach here, all may be discovered by getting this right the first time.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(); // ..... } }
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
- 12-28-2008, 02:24 PM #15
Member
- Join Date
- Dec 2008
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
org.apache.jasper.JasperException: Unable to compile class for JSP:
By benperks in forum New To JavaReplies: 5Last Post: 11-20-2011, 09:22 PM -
How to pass value from servlet to midlet?
By sharmaj2me in forum CLDC and MIDPReplies: 1Last Post: 12-05-2008, 03:52 PM -
[SOLVED] Unable to instantiate class?
By xcallmejudasx in forum New To JavaReplies: 1Last Post: 11-03-2008, 04:03 PM -
Pass by ref. A work around?
By diRisig in forum New To JavaReplies: 0Last Post: 02-05-2008, 07:25 PM -
how to pass array in java?
By sivasayanth in forum New To JavaReplies: 3Last Post: 01-13-2008, 04:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks