我目前正在开发Java局域网游戏。我希望能够在按下按钮后启动服务器。当我按下按钮时,变量startServer和listenToClients变为true。但是,当我开始监听客户端时,我得到一个空指针异常。我该如何解决这个问题?
这是主类的代码:
public static void main(String args[]) throws IOException {
ServerSocket ss = null;
Socket cs = null;
String address=Inet4Address.getLocalHost().getHostAddress();
InetAddress addr = InetAddress.getByName(address);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new TablaPrinc().setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
while (true) {
if (startServer == true) {
ss = new ServerSocket(5555,50,addr);
System.out.println("Server started on "+address);
startServer=false;
}
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(TablaPrinc.class.getName()).log(Level.SEVERE, null, ex);
}
if (listenToClients==true){
cs = ss.accept();
System.out.println("\n we have a new client. ");
Connection con=new Connection(cs, ++i);
}
}
}
这是连接类:
static class Connection extends Thread {
int identity;
Socket cs = null;
DataInputStream is = null;
DataOutputStream os = null;
public Connection(Socket client, int i) throws IOException {
cs = client;
identity = i;
is = new DataInputStream(cs.getInputStream());
os = new DataOutputStream(cs.getOutputStream());
start();
}
public void run(){
while(true){
/* Interaction between the server and the clients */
}
}
}
答案 0 :(得分:0)
首先确保首先将startServer变量设置为true,然后等待它再次变为false,这意味着ServerSocket已初始化,最后将listenToClients设置为true。否则,当ServerSocket为null时,您可能遇到listenToClients为true的情况,得到您正在讨论的NullPointerException。
你能为我们提供完整的堆栈跟踪打印异常吗?
答案 1 :(得分:0)
好的,我修改了代码,摆脱了异常,这就是我现在所拥有的:
服务器:
package game;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
public class GServer {
static int i=0;
static int numberOfPlayers=0;
static ArrayList <String> players=new ArrayList <String> ();
public GServer(int nrj) throws UnknownHostException, IOException{
this.numberOfPlayers=nrj;
ServerSocket ss = null;
Socket cs = null;
String address=Inet4Address.getLocalHost().getHostAddress();
InetAddress addr = InetAddress.getByName(address);
ss = new ServerSocket(5555,50,addr);
MainFrame.jLabel18.setText("Server started on "+address);
for (int j=0;i<nrj;j++) {
cs = ss.accept();
System.out.println("\n New Client");
Connection con=new Connection(cs, ++i);
players.add(Connection.is.readUTF());
}
for (int i=0;i<players.size();i++){
System.out.println("\n Player:"+players.get(i));
}
}
}
class Connection extends Thread {
int identity;
Socket cs = null;
static DataInputStream is = null;
static DataOutputStream os = null;
public Connection(Socket client, int i) throws IOException {
cs = client;
identity = i;
is = new DataInputStream(cs.getInputStream());
os = new DataOutputStream(cs.getOutputStream());
start();
}
public void run(){
while(true){
}
}
}
这是客户:
package game;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class GClient {
public GClient() throws IOException{
String address=MainFrame.jTextField18.getText();
Socket cs = new Socket(address, 5555);
MainFrame.jLabel20.setText("Connected to "+address);
final DataOutputStream os = new DataOutputStream(cs.getOutputStream());
DataInputStream is = new DataInputStream(cs.getInputStream());
os.writeUTF(TablaPrinc.jTextField19.getText());
}
}
这是主框架中的主要类:
public static void main(String args[]) throws IOException {
try {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new TablaPrinc().setVisible(true);
} catch (IOException ex) {
Logger .getLogger(TablaPrinc.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
while(true){
if (serverStart==true){
int numberOfPlayers=Integer.parseInt(jTextField19.getText());
GServer gs=new GServer(numberOfPlayers);
serverStart=false;
jButton2.setEnabled(false);
}
try {
Thread.sleep(15);
} catch (InterruptedException ex) {
Logger.getLogger(TablaPrinc.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我想要做的是在客户端侦听结束后在所有帧上禁用jButton2。使用此代码,这仅发生在第一帧
按下此按钮后客户端连接到服务器:
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel3.setVisible(false);
jLabel16.setVisible(false);
jLabel17.setVisible(false);
jLabel18.setVisible(false);
jTextField18.setVisible(false);
jTextField19.setVisible(false);
jTextField20.setVisible(false);
jButton5.setVisible(false);
jButton6.setVisible(false);
jButton7.setVisible(true);
jLabel20.setVisible(true);
jLabel7.setVisible(true);
jTextField3.setVisible(true);
try {
GClient gc=new GClient();
} catch (IOException ex) {
jLabel20.setText("Connection unsuccesfull");
Logger.getLogger(TablaPrinc.class.getName()).log(Level.SEVERE, null, ex);
}
}