java套接字测试程序出现空指针错误:java.lang.NullPointerException,实在不懂,求解!
packageqk;importjava.awt.BorderLayout;importjava.awt.EventQueue;importjava.net.Server...
package qk;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ServerSocketFrame extends JFrame {
private JPanel contentPane;
private JTextArea textArea;
private ServerSocket server;
private Socket socket;
public void getServer(){
try{
server = new ServerSocket(1978);
textArea.append("服务器套接字已经创建成功\n");
while (true){
textArea.append("等待客户机的连接........\n");
socket = server.accept();
textArea.append("连接成功......\n");
}
}catch (Exception e){
e.printStackTrace();//输出异常信息
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ServerSocketFrame frame = new ServerSocketFrame();
frame.setVisible(true);
frame.getServer();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ServerSocketFrame() {
//super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 369, 262);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
}
错误指向 textArea.append("服务器套接字已经创建成功\n");
frame.getServer(); 展开
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ServerSocketFrame extends JFrame {
private JPanel contentPane;
private JTextArea textArea;
private ServerSocket server;
private Socket socket;
public void getServer(){
try{
server = new ServerSocket(1978);
textArea.append("服务器套接字已经创建成功\n");
while (true){
textArea.append("等待客户机的连接........\n");
socket = server.accept();
textArea.append("连接成功......\n");
}
}catch (Exception e){
e.printStackTrace();//输出异常信息
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ServerSocketFrame frame = new ServerSocketFrame();
frame.setVisible(true);
frame.getServer();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ServerSocketFrame() {
//super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 369, 262);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
}
错误指向 textArea.append("服务器套接字已经创建成功\n");
frame.getServer(); 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询