java - trouble using printwriter from another class -
my main problem nullpointerexception when use chatpanel.pw.println(chatpanel.name + " has disconnected chat.") while printwriter in chatpanel class , i'm trying use printwriter send message through socket. if me understand , maybe give me solution problem. did remove lot of code but, should compile.
import java.io.*; import java.awt.*; import java.net.*; import javax.swing.*; import java.awt.event.*; import javax.swing.jcomponent; public class chatframe1 extends jframe{ public chatframe1(){ setlayout(new gridbaglayout()); setsize(1000,600); settitle("chat"); setresizable(true); addwindowlistener(new windowadapter(){ public void windowclosing(windowevent we){ //the problem here. chatpanel.pw.println(chatpanel.name + " has disconnected chat."); system.out.println(chatpanel.name); system.exit(0); } }); chatpanel sp = new chatpanel(); setvisible(true); } public static void main(string[] args){ new chatframe1(); } } class chatpanel extends jpanel implements actionlistener, runnable{ thread t; jtextfield tf; jtextarea ta; jbutton b; socket s; bufferedreader br; public static printwriter pw; string temp; boolean connected; public static string name; public chatpanel(){ gridbagconstraints gbc = new gridbagconstraints(); name = (string)joptionpane.showinputdialog(null,"enter username "+ "(please use letters , numbers) :", "username login", joptionpane.plain_message, null, null, "user-name"); setlayout(new gridbaglayout()); tf = new jtextfield(); tf.addactionlistener(this); ta = new jtextarea(); b = new jbutton("press connect"); b.addactionlistener(this); } public void actionperformed(actionevent ae){ if((tf != null) && (!connected)){ b.setlabel("press connect"); } if((ae.getsource() == b) && (!connected)){ try{ s = new socket("127.0.0.1", 2020); pw = new printwriter(s.getoutputstream(), true); tf.settext(""); }catch(unknownhostexception uhe){ system.out.println(uhe.getmessage()); }catch(ioexception ioe){ system.out.println(ioe.getmessage()); } connected = true; t = new thread(this); b.setlabel("disconnect"); t.start(); }else if((ae.getsource() == b) && (connected)){ connected = false; try{ pw.println(name +" has disconnected chat."); ta.settext(""); s.close(); //no buffering so, ok }catch(ioexception ioe){ system.out.println(ioe.getmessage()); } b.setlabel("press reconnect"); }else{ temp = tf.gettext(); tf.settext(""); } } public void run(){ try{ bufferedreader br = new bufferedreader(new inputstreamreader(s.getinputstream())); while(((temp = br.readline()) != null) && connected){ ta.append(temp + "\n"); } }catch(ioexception ioe){ system.out.println(ioe.getmessage()); } } }
i think giving nullpointerexception
chatpanel.pw.println(chatpanel.name + " has disconnected chat.");
because chatpanel.pw
has never been initialized in code, , therefore null
you should ensure chatpanel.pw
has been initialized somewhere (maybe within constructor) inside chatpanel
class before calling chatpanel.pw.println()
Comments
Post a Comment