java programming in network!

import java.net.*;
import java.io.*;

public class lab8S extends Thread {

public static final void main(String[] args)
{
try{
ServerSocket eSocket = new ServerSocket(2002);
ServerSocket efile=new ServerSocket(2003);
new process1(eSocket.accept()).start();
new fileserver(efile.accept()).start();
try
{
Thread.currentThread().sleep(1000);
}catch(InterruptedException ie)
{
}
}
catch(Exception e)
{
}
}
}
class process1 extends Thread
{
private Socket s=null;
public process1(Socket soc)
{
s=soc;
}

public final void run()
{
try{
ScreenOutThread st=new ScreenOutThread(s.getInputStream());
st.start();
new KeyboardInThread(s.getOutputStream(),st.dname).start();
try
{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e)
{}
}
catch(Exception e)
{
try{
s.close();
}
catch(Exception ex){}
return;
}//catch
}//run()
}//process1




class KeyboardInThread extends Thread {
OutputStream out;
PrintWriter pStream =null;
private static File[] files=null;
private static File[] dirs=null;
public static String dname1;
public KeyboardInThread(OutputStream out,String dd) {
this.out = out;
dname1=dd;
}

public void run() {
try {
try
{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e)
{}

pStream=new PrintWriter(out);
System.out.println(dname1);
File ff=new File(dname1);
files = ff.listFiles(new FileFilter() {
public final boolean accept(File file) {
return file.isFile();}});
dirs = ff.listFiles(new FileFilter() {
public final boolean accept(File file) {
return file.isDirectory();
}});
for(int i=0;i<files.length;i++)
pStream.println(files[i].getName());
for(int i=0;i<dirs.length;i++)
pStream.println(dirs[i].getName());
pStream.flush();
}
catch(NullPointerException ne)
{
      ne.printStackTrace(System.err);
}
catch(Exception ioe) {}
}
}



class ScreenOutThread extends Thread {
InputStream in;
BufferedReader br=null;
public static String dname;
public ScreenOutThread(InputStream inB) {
in = inB;
}
public void run() {
try {
br=new BufferedReader(new InputStreamReader(in));
dname=br.readLine();
System.out.println("Required directory Name:"+dname);
}catch(Exception ioe) {}
}
}


class fileserver extends Thread
{
private Socket s1=null;
public fileserver(Socket soc1)
{
s1=soc1;
}
public void run()
{
String machineName = null;
BufferedReader sIn = null;
BufferedReader in = null;
BufferedWriter out1=null;
FileReader file_in = null;
PrintStream sOut=null;
try {
sIn = new BufferedReader(new InputStreamReader(s1.getInputStream()));
String name=sIn.readLine();
System.out.println(name);
file_in = new FileReader("c:/temp/sreeja/yy.txt");
in = new BufferedReader(file_in);
out1 = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));
int i = -1;
while ((i = in.read()) != -1) {
out1.write(i);
}
try
{
Thread.currentThread().sleep(1000);
}catch(InterruptedException e)
{}
System.out.println("Required file is transferred to client");
out1.flush();
out1.close();
file_in.close();
in.close();
}catch (Exception ioe) {}
}//run()
}//fileserver



from the above program i am storing  a directory name(String), in the variable dname in class ScreenOutThread,but while i try to use the variable dname in the class KeyboardInThread ,i get nullpointerexception why?
sraveendAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RuadRauFlessaCommented:
well you are trying to read dname from a file and then constructing an input stream in regards to that. Now it is never a good idea to assume that the info you get from an text file in respect to file locations and such are correct. Test... throw an exception and display a message... halt the operation up to a point where you can continue without that data.
0
girionisCommented:
> variable dname in class ScreenOutThread,but while i try to use the
> variable dname in the class KeyboardInThread ,i get nullpointerexception why?

Are you sure that dname is not null? What is the exact lien that throws the npe?

0
achilkaCommented:
avoid declaring dname as static member
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

sraveendAuthor Commented:
I could get  dname correclty in the class ScreenOutThread,but it's not displaying outside the class ScreenOutThread
0
sraveendAuthor Commented:
ScreenOutThread st=new ScreenOutThread(s.getInputStream());
st.start();
String xx=st.dname;

i get null in variable xx.No use of removing static member
0
RuadRauFlessaCommented:
Is there a reason why dname is static???

Also remember that the ScreenOutThread is in fact threaded and the value of dname is read witin the run method. You are trying to read the dname variable directly after instructing java to schedule the thread. That does not mean that it has actually run yet.
0
sraveendAuthor Commented:
I've no idea how to use the variable dname from class ScreenOutThread in the class KeyboardInThread ,pls help!
0
RuadRauFlessaCommented:
By the looks of it the ScreenOutThread does not need to be a thread since the type of processing you are doing is simply reading something from a file. You could remove the threading in it and your problems should be solved.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.