这段java代码空指针异常如何处理
importjava.io.*;importjava.net.*;publicclassChatServer{publicstaticvoidmain(String[]a...
import java.io.*;
import java.net.*;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(1234);
while(true) {
Socket s = ss.accept();
System.out.println("a client is connected");
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
System.out.println(str);
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
***********************************************
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import org.omg.CORBA.portable.UnknownException;
public class ChatClient extends Frame {
Socket s = null;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
pack();
setVisible(true);
}
public void connect() {
try{
s = new Socket("127.0.0.1",1234);
System.out.println("connected");
}catch(UnknownHostException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String str = tfTxt.getText().trim();
taContent.setText(str);
tfTxt.setText("");
try {
System.out.println(s);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
dos.flush();
dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
**********************************************************
null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ChatClient$TFListener.actionPerformed(ChatClient.java:53)
at java.awt.TextField.processActionEvent(Unknown Source)
at java.awt.TextField.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
为什么会抛出这样的空指针异常? 展开
import java.net.*;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(1234);
while(true) {
Socket s = ss.accept();
System.out.println("a client is connected");
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
System.out.println(str);
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
***********************************************
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import org.omg.CORBA.portable.UnknownException;
public class ChatClient extends Frame {
Socket s = null;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
tfTxt.addActionListener(new TFListener());
pack();
setVisible(true);
}
public void connect() {
try{
s = new Socket("127.0.0.1",1234);
System.out.println("connected");
}catch(UnknownHostException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String str = tfTxt.getText().trim();
taContent.setText(str);
tfTxt.setText("");
try {
System.out.println(s);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
dos.flush();
dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
**********************************************************
null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ChatClient$TFListener.actionPerformed(ChatClient.java:53)
at java.awt.TextField.processActionEvent(Unknown Source)
at java.awt.TextField.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
为什么会抛出这样的空指针异常? 展开
展开全部
看一下你的程序片段:
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String str = tfTxt.getText().trim();
taContent.setText(str);
tfTxt.setText("");
try {
System.out.println(s);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
控制台输出的第一行 null 就是 System.out.println(s); 的结果。
而执行下面这行:
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
后,一定报错,因为类 ChatClient 的属性 Socket s 没有初始化。
建议将类 ChatClient 的 main 方法修改成:
public static void main(String[] args) {
new ChatClient().launchFrame();
connect() ;
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
String str = tfTxt.getText().trim();
taContent.setText(str);
tfTxt.setText("");
try {
System.out.println(s);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
控制台输出的第一行 null 就是 System.out.println(s); 的结果。
而执行下面这行:
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
后,一定报错,因为类 ChatClient 的属性 Socket s 没有初始化。
建议将类 ChatClient 的 main 方法修改成:
public static void main(String[] args) {
new ChatClient().launchFrame();
connect() ;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询