java - String data from a Scanner through a PrintWriter -


i trying pass data string printwriter while simultaneously reading bufferedreader between 2 classes named server.java , client.java. problem having trouble handling exceptions being thrown block of code reads data scanner object (marked below).

client.java

package root;  /**  * @author noah teshima  */  import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.inetaddress; import java.net.socket; import java.util.scanner;  public class client {     private socket clientsocket;      private bufferedreader bufferedreader;     private inputstreamreader inputstreamreader;     private printwriter printwriter;      private scanner scanner;      public static void main(string[] args) {         new client("localhost", 1025);     }      public client(string hostname, int portnumber) {         try {             this.clientsocket = new socket(hostname, portnumber);             this.bufferedreader = new bufferedreader(this.inputstreamreader = new inputstreamreader(this.clientsocket.getinputstream()));             this.printwriter = new printwriter(clientsocket.getoutputstream());             string msg = "",                     msg2 = "";              this.printwriter.println(this.getclass());             this.printwriter.flush();             system.out.println(this.getclass().getname() + " connected " + this.bufferedreader.readline());              while(!(msg = this.scanner.nextline()).equals("exit")) { //source of problem                 this.printwriter.println(this.getclass().getname() + ": " + msg);                 this.printwriter.flush();                  while((msg2 = this.bufferedreader.readline()) != null) {                     system.out.println(msg2);                 }             }              this.clientsocket.close();         }catch(ioexception exception) {             exception.printstacktrace();         }     } } 

stack trace::

exception in thread "main" java.lang.nullpointerexception     @ root.client.<init>(client.java:47)     @ root.client.main(client.java:25) 

the code used read bufferedreader , write printwriter same both classes, posted client.java. if see other class file, happy so.

before use

while(!(msg = this.scanner.nextline()).equals("exit")) { //source of problem

you should have initialized scanner.
scanner object null when used it, , hence nullpointerexception.

i not see scanner = new scanner(...); anywhere within code,
maybe have forgetten it?


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -