java - Mistake when create 2 thread files in one program -
i have 2 thread file in project of server: threadsocket , threadvalidate. threadsocket used sending , receiving information between client , server threadvalidate used testing account in signin , signup form.
this threadvalidate:
public class threadvalidate extends thread{ socket socket; string username = null, password = null; bufferedreader infromclient = null; printwriter outtoclient = null; string driver = "sun.jdbc.odbc.jdbcodbcdriver"; string databaseurl = "jdbc:odbc:driver={sql server};server=abc-pc;database=abc;uid=sa;pwd="; public threadvalidate(socket psocket){ this.socket=psocket; } public synchronized void run(){ try{ class.forname(driver); connection connection; connection = drivermanager.getconnection(databaseurl); infromclient = new bufferedreader(new inputstreamreader(socket.getinputstream())); outtoclient = new printwriter(socket.getoutputstream(), true); username = infromclient.readline(); password = infromclient.readline(); system.out.println(username + " " + password); //Đăng nhập string sqlsignin = "select * userdangnhap username = ? , password = ?"; // preparedstatement preparedstatement statementsignin = connection.preparestatement(sqlsignin); statementsignin.setstring(1, username); statementsignin.setstring(2, password); resultset rssignin = statementsignin.executequery(); if (rssignin != null) { username = rssignin.getstring("username"); password = rssignin.getstring("password"); outtoclient.println("exist"); } else { outtoclient.println("not exist"); } } catch (classnotfoundexception | sqlexception | ioexception ex){ } } }
and code in signin form:
public class signin extends javax.swing.jframe { socket socketvalidate = null; private bufferedreader invalidate = null; private printwriter outvalidate = null; public static string username = null; public static string password = null; public static string kq = ""; public signin (java.awt.frame parent, boolean modal) { initcomponents(); this.setresizable(false); } public signin (socket client){ this.socketvalidate = client; } public void listensocket() throws unknownhostexception { string serveraddress = "localhost"; try { socketvalidate = new socket(serveraddress, 4444); invalidate = new bufferedreader(new inputstreamreader(socketvalidate.getinputstream())); outvalidate = new printwriter(socketvalidate.getoutputstream(), true); } catch (ioexception e) { system.out.println("no i/o" + e.getmessage()); system.exit(1); } catch (exception ex) { system.out.println("unknown host: " + serveraddress); system.exit(1); } } private void bntdnactionperformed(java.awt.event.actionevent evt) { this.username = txtusername.gettext(); this.password = txtpassword.gettext(); joptionpane.showmessagedialog(this, username + " " + password); outvalidate.println(username); outvalidate.println(password); try { kq = invalidate.readline(); } catch (ioexception ex) { logger.getlogger(dangnhap.class.getname()).log(level.severe, null, ex); } switch (kq) { case "exist": this.dispose(); break; case "not exist": joptionpane.showmessagedialog(this, "retype again, please"); break; } }
it shows error on these lines: outvalidate.println(username); outvalidate.println(password); error
exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ client.signin .bntdnactionperformed(signin .java:188) @ client.signin .access$000(signin .java:22) @ client.signin $1.actionperformed(signin .java:102)
please me fix it, don't understand reason why. can't send information username , password threadvalidate process it. threadsocket.java in project of server , gameclient.form in project of client run ok.
note: threadsocket , threadvalidate in same project of server, gameclient , signin in same project of client.
Comments
Post a Comment