这是我在stackoverflow中的第一个问题。我正在尝试在客户端和服务器之间创建套接字连接(此应用程序具有GUI组件)。这是计划的流程:
1. start 'MultiServer' // this frame has a related class file named 'MultiServerThread'
2. start 'LogIn' // when this frame is opened for the first time, it successfully establishes connection to the 'MultiServer'
3. from 'LogIn', go to 'SignUp' // I want to know how to end the connection from 'LogIn'
4. from 'SignUp, go back to 'LogIn' // when this frame is opened again, it now throws Exception in thread "AWT-EventQueue-0"
但是,当我返回LogIn(以及除LogIn之外的其他帧)并尝试向服务器发出查询(out.writeObject(value))时,错误开始。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at client.LogIn.submitData(LogIn.java:461)
at client.LogIn.setReaction(LogIn.java:255)
at client.LogIn.lambda$new$0(LogIn.java:241)
at client.LogIn$$Lambda$13/1313922862.actionPerformed(Unknown Source)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
这是我的LogIn代码(代码仅修剪到相关部分)。它指向我的代码的三行; ActionListener getEvent ...,submitData(String)try语句,setReaction(ActionEvent)else if语句:
package client;
import ...;
public class LogIn extends JFrame implements KeyListener {
public LogIn() {
initComponents();
....
}
// PROTECTED BLOCKS
@SuppressWarnings("unchecked")
private void initComponents() { .... }
// USER-GENERATED BLOCKS
private String QueryType, Username, Password, ServerResponse;
private ObjectOutputStream out;
private ObjectInputStream in;
private Socket socket;
ActionListener getEvent = (ActionEvent eventEmergence) -> { setReaction(eventEmergence); }; // <EXCEPTION POINTS HERE>
private void setReaction(ActionEvent eventEncountered) {
if (eventEncountered.getSource() == BT_Register) {
openSignUp();
this.dispose();
}
else if (eventEncountered.getSource() == BT_Login || eventEncountered.getSource() == TF_Password) {
submitData(eventEncountered.getActionCommand()); // <EXCEPTION POINTS HERE>
}
....
}
private void validateChar(KeyEvent evt) { .... }
// FRAME CREATION BLOCKS
private void openSignUp() {
SignUp SFW = new SignUp();
SFW.setVisible(true);
this.dispose();
}
private void openBallot(String StudentID, String StudentName, String Section) { .... }
private void openAccessDenied() { .... }
private void openAuthenticationFailure() { .... }
private void openInfoDev() { .... }
private void openInfoSys() { .... }
// KEY LISTENER METHOD OVERRIDE BLOCKS
@Override public void keyTyped(KeyEvent e) { .... }
@Override public void keyPressed(KeyEvent e) { .... }
@Override public void keyReleased(KeyEvent e) { .... }
// CONNECTION-RELATED BLOCKS
private void initConnection() throws IOException {
try {
socket = new Socket("localhost", 7555);
out = new ObjectOutputStream(socket.getOutputStream()); // send
out.flush();
in = new ObjectInputStream(socket.getInputStream()); // receive
translateResponse();
}
catch (UnknownHostException e) { .... }
catch (IOException e) { .... }
endConnection();
}
private void endConnection() throws IOException {
out.close();
in.close();
socket.close();
}
private void translateResponse() throws IOException {
do {
try {
ServerResponse = (String)in.readObject(); // receive server response
// determine action based from server response
if (ServerResponse.equals("Accepted")) {
String StudentID = (String)in.readObject();
String StudentName = (String)in.readObject();
String Section = (String)in.readObject();
openBallot(StudentID, StudentName, Section);
}
else if (ServerResponse.equals("AccountAlreadyVoted")) {
openAccessDenied();
}
else if (ServerResponse.equals("IncorrectData")) {
openAuthenticationFailure();
}
}
catch(ClassNotFoundException e) { }
}
while (!ServerResponse.equals("connection.terminate"));
}
private void submitData(String idle) {
QueryType = "Login";
Username = TF_Username.getText(); // get Username string
Password = TF_Password.getText(); // get Password string
try {
out.writeObject(QueryType); // submit type of operation <EXCEPTION POINTS HERE>
out.writeObject(Username); // send Username
out.writeObject(Password); // send Password
out.flush();
}
catch(IOException e) { }
}
public static void main(String args[]) throws IOException {
// create new LogIn
LogIn LI = new LogIn();
LI.setVisible(true);
LI.initConnection();
}
答案 0 :(得分:0)
堆栈跟踪列出了程序上次以相反顺序执行的调用,因此您的程序在异常之前流动:
在第461行,eventEncountered
可能为空。学习使用调试器有助于您了解变量的设置(不)。