null pointer exception in thread

A

asit

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class AppServer extends Frame implements ActionListener,
Runnable{

/**
* @param args
*/

Button b1;
TextField tf;
TextArea ta;
ServerSocket ss;
Socket s;
PrintWriter pw;
Scanner input;
Thread th;


public AppServer() {
Frame f = new Frame("Server side chatting");
f.setLayout(new FlowLayout());
f.setBackground(Color.ORANGE);
b1 = new Button("Send");
b1.setBackground(Color.PINK);
b1.addActionListener(this);
tf = new TextField(30);
ta = new TextArea();
ta.setBackground(Color.CYAN);
f.addWindowListener(new W1());
f.add(tf);
f.add(b1);
f.add(ta);
try {
ss = new ServerSocket(12000);
s = ss.accept();
input = new Scanner(s.getInputStream());
pw = new PrintWriter(s.getOutputStream(),true);
}catch(Exception e) {
e.printStackTrace();
}

setFont(new Font("Arial", Font.BOLD,20));
f.setSize(200,200);
f.setLocation(300,300);
f.setVisible(true);
f.validate();
th = new Thread(this);
th.setDaemon(true);
th.start();
}

private class W1 extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}

public void actionPerformed(ActionEvent ae) {
pw.println(tf.getText());
tf.setText("");
}

public void run() {
while(true) {
try {
ta.append(input.nextLine());
}catch(Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AppServer a = new AppServer();

}

}

In the above code, I have used a thread which updates the text area if
some data is received. Why it shows the following exception ???

java.lang.NullPointerException
at AppServer.run(AppServer.java:70)
at java.lang.Thread.run(Thread.java:619)
 
D

Donkey Hottie

asit said:
In the above code, I have used a thread which updates the
text area if some data is received. Why it shows the
following exception ???

java.lang.NullPointerException
at AppServer.run(AppServer.java:70)
at java.lang.Thread.run(Thread.java:619)

I compiled and run your code, and somehow decided to telnet into it with
port 12000 (you did not say how it was to used) and it works (kind of).

It does not crash, and does not throw NPE.

What I send in telnet is shown in no window but the telnet itself.


What I send in your AppServer window is shown in telnet window. But no in
the AppServer window itself.

No Null Pointer Exception seen in my test. End of test.

** Just check what is happening on your AppServer.java:70 **

Something on that line has an undefined reference, but I can not say what it
is was my editor formats your code differently and I can not see what is on
line 70.
 
J

John B. Matthews

"Donkey Hottie said:
I compiled and run your code, and somehow decided to telnet into it
with port 12000 (you did not say how it was to used) and it works
(kind of).

It does not crash, and does not throw NPE.

What I send in telnet is shown in no window but the telnet itself.

What I send in your AppServer window is shown in telnet window. But
no in the AppServer window itself.

No Null Pointer Exception seen in my test. End of test.

** Just check what is happening on your AppServer.java:70 **

Something on that line has an undefined reference, but I can not say
what it is was my editor formats your code differently and I can not
see what is on line 70.

I got similar results, but I think it's a fluke. As the GUI is not built
on the EDT but on the initial thread, the TextArea (referenced at line
70) may not be visible to the AppServer thread when it begins. Of
course, building the GUI on the EDT in the usual way blocks the EDT at
accept(). One solution would be to delegate the socket handling to a
SwingWorker.
 
V

Volker Borchert

asit said:
In the above code, I have used a thread which updates the text area if
some data is received. Why it shows the following exception ???

java.lang.NullPointerException
at AppServer.run(AppServer.java:70)
at java.lang.Thread.run(Thread.java:619)

Possibly a memory visibility issue. Google up on this, and/or on
"happens-before" or "this escape". There is a reason why starting a
thread inside a constructor is sometimes considered a Bad Thing [tm].
 
J

John B. Matthews

asit said:
In the above code, I have used a thread which updates the text area if
some data is received. Why it shows the following exception ???

java.lang.NullPointerException
at AppServer.run(AppServer.java:70)
at java.lang.Thread.run(Thread.java:619)

Possibly a memory visibility issue. Google up on this, and/or on
"happens-before" or "this escape". There is a reason why starting a
thread inside a constructor is sometimes considered a Bad Thing [tm].

Good point. Sadly, I've struggled with "this escape," violating the
advice against "starting a thread inside a constructor" quite recently:

<http://groups.google.com/group/comp.lang.java.programmer/msg/
8cc954b4d39d58db>

I found this informative but somewhat dated article:

<http://www.ibm.com/developerworks/java/library/j-jtp0618.html>

Can anyone suggest something more recent?

Thanks in advance.
 
A

Arne Vajhøj

asit said:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class AppServer extends Frame implements ActionListener,
Runnable{

/**
* @param args
*/

Button b1;
TextField tf;
TextArea ta;
ServerSocket ss;
Socket s;
PrintWriter pw;
Scanner input;
Thread th;


public AppServer() {
Frame f = new Frame("Server side chatting");
f.setLayout(new FlowLayout());
f.setBackground(Color.ORANGE);
b1 = new Button("Send");
b1.setBackground(Color.PINK);
b1.addActionListener(this);
tf = new TextField(30);
ta = new TextArea();
ta.setBackground(Color.CYAN);
f.addWindowListener(new W1());
f.add(tf);
f.add(b1);
f.add(ta);
try {
ss = new ServerSocket(12000);
s = ss.accept();
input = new Scanner(s.getInputStream());
pw = new PrintWriter(s.getOutputStream(),true);
}catch(Exception e) {
e.printStackTrace();
}

setFont(new Font("Arial", Font.BOLD,20));
f.setSize(200,200);
f.setLocation(300,300);
f.setVisible(true);
f.validate();
th = new Thread(this);
th.setDaemon(true);
th.start();
}

private class W1 extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}

public void actionPerformed(ActionEvent ae) {
pw.println(tf.getText());
tf.setText("");
}

public void run() {
while(true) {
try {
ta.append(input.nextLine());
}catch(Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AppServer a = new AppServer();

}

}

In the above code, I have used a thread which updates the text area if
some data is received. Why it shows the following exception ???

java.lang.NullPointerException
at AppServer.run(AppServer.java:70)
at java.lang.Thread.run(Thread.java:619)

Unlike the other posters I am not so worried about "this"
being passed to the thread. A thread start is a memory barrier.

I am more concerned over that the thread is set to daemon.
When the main thread runs out it will start exiting the app.

And I don't think it is good to continue in the while loop
even if Scanner throws exceptions.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top