Print variable whicj was read in another class

i have a project in several classes the 1st class read from keyboard data and the last 2 classes (one report on main) print out the content which was read , the calculation and start the aplication

the code is in next lines

class 1

/**

*

*/

package main;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @author Nazakthul

*

*/

publicclass Citire{

private String linie;

publicvoid Cit()throws IOException{

BufferedReader input =new BufferedReader(new InputStreamReader(

System.in));

System.out.println("Give a String");

linie = input.readLine();

System.out.println(linie);

}

publicvoid setLinie(String linie){

this.linie = linie;

}

public String getLinie(){

return linie;

}

public Citire(){

}

}

Class 2

/**

*

*/

package main;

/**

* @author Nazakthul

*

*/

publicclass Raport{

Citire sir =new Citire();

publicvoid afisare(){

Stringsirul = sir.getLinie();

System.out.println(sirul);

}

}

class 3

/**

*

*/

package main;

import java.io.IOException;

/**

* @author Nazakthul

*

*/

publicclass Main{

publicstaticvoid main(String[] args)throws IOException{

Citire s =new Citire();

Raport raport=new Raport();

raport.afisare();

}

}

When i used String sirul = sir.getLinie

i wanted to memorize in sirul what i read from keyboard and use print to print the string but when i run the app what is comeback from app is |null|

what i have to do print what i read from KB

please HELP ME!!!

[3643 byte] By [Nazakthula] at [2007-11-15]
# 1

When you create a Citire object, the linie variable isn't assigned to anything, so it's null. You then call the getLinie() method on that object, which just returns linie (meaning it returns null). If you want the Citire object to have a user-entered value store there, then you have to call the method that takes user input.

public class Main {

public static void main(String[] args) throws IOException {

// Citire s = new Citire();// don't create a Citire here, the Raport class creates it's own

Raport raport=new Raport();

raport.afisare();

}

}

public class Raport {

Citire sir = new Citire();

public void afisare(){

// call sir.Cit() to get the user input and save it in sir

Stringsirul = sir.getLinie();// that way, sir will return that input here

System.out.println(sirul);

}

}

hunter9000a at 2007-7-12 > top of java,Java Essentials,New To Java...
# 2

the constructor of CItire is wrong

the rigth code is

class Citire {

private String linie;

public Citire() {

try {

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Give a String");

linie = input.readLine();

} catch (Exception e) {

e.printStackTrace();

}

}

public void setLinie(String linie) {

this.linie = linie;

}

public String getLinie() {

return linie;

}

}

enjoy.

Message was edited by:

pbulgarelli

Message was edited by:

pbulgarelli

pbulgarellia at 2007-7-12 > top of java,Java Essentials,New To Java...
# 3
thanks it works no i have to expand this code for my real program
Nazakthula at 2007-7-12 > top of java,Java Essentials,New To Java...
# 4
And don't forget the Dukes, : )
pbulgarellia at 2007-7-12 > top of java,Java Essentials,New To Java...