我写了一个简单的即时通讯程序,它在日食中运行得非常好。我想将它导出到.jar并在多台计算机上运行它,而我的主计算机托管了java socketServer。该程序在.jar中运行服务器但从eclipse执行客户端时有效。但是,当我运行我的client.jar时,它将无法连接到服务器。我的代码的两个部分都很长。我正在使用我的实际IPv4地址使用套接字(ipaddress,port#)。我也在端口号9000上操作我不确定它是否有任何影响。我一直在搜索这个网站和许多其他网站,但我似乎无法找到关于这个主题的已经开放的话题。我可能会补充一点,我手动将我的IPv4地址输入GUI,因为我一直在我的桌面和笔记本电脑之间切换。我的笔记本电脑IPv4总是在变化,这就是我手动输入的原因
Server.java中的代码
serverUser = new ServerSocket(9000);
textArea.append("User Server created on " + new Date() + "\n");
serverChat = new ServerSocket(9100);
textArea.append("Chat Server created on " + new Date() + "\n");
while(true)
{
Socket socket = serverUser.accept();
clientNum++;
new DataOutputStream(socket.getOutputStream()).writeUTF("You have connected to the server");
User user = new User(socket, new DataInputStream(socket.getInputStream()).readUTF());
userList.add(user);
client.java中的套接字
public void actionPerformed(ActionEvent e)
{
boolean next = false;
userName = userNameField.getText();
password = new String(passwordField.getPassword());
ipAddress = ipAddressField.getText();
if(ipAddress.equals(""))
status.setText("You must enter the IP Address of the Server");
else
{
userNameField.setText("");
ipAddressField.setText("");
passwordField.setText("");
query = "select * from User where userName = '" + userName + "' and password = '" + password + "'";
try
{
line 328 ResultSet rs = statement.executeQuery(query);
if(rs.next())
next = true;
rs = statement.executeQuery("select * from User");
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
if(next)
{
tabbedPane.setEnabledAt(0, false);
tabbedPane.setEnabledAt(1, true);
tabbedPane.setSelectedIndex(1);
logout.setEnabled(true);
try
{
socket = new Socket(ipAddress, 9000);
output = new DataOutputStream(socket.getOutputStream());
input = new DataInputStream(socket.getInputStream());
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
output.writeUTF(userName);
output.flush();
status.setText(input.readUTF());
setTitle(userName);
Thread thread = new Thread(new UpdateTask(userList));
thread.start();
}
catch(IOException ex)
{
System.out.println(ex.getMessage());
}
}
else
status.setText("Invalid user name or password");
}
}
好的,我收到了错误,当然我的问题出错了。问题最终是因为总是nullPointer而烦人,我不知道我是怎么回事。 这是错误
C:\Users\Satokaheni\Programs\Java Workspace\Runnable Jars>java -jar Client.jar
Works
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at UserMenu$6.actionPerformed(UserMenu.java:328)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)
男人我觉得自己真的很蠢我很抱歉这个问题出错但是你可以帮忙解决这个问题吗? 我知道查询不是null因为我打印出来了。所以我不知道为什么它给了我一个nullPointer
我发现为什么我认为我得到空指针
path to 'db/loginDB.db': 'C:\Users\Satokaheni\Programs\Java Workspace\Runnable Jars\db' does not exist
这是我的数据库代码
Class.forName("org.sqlite.JDBC");
try
{
connection = DriverManager.getConnection("jdbc:sqlite:db/loginDB.db");
statement = connection.createStatement();
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
任何提示将不胜感激。
谢谢。
PS如果你想我可以发布代码。