Results 1 to 7 of 7
  1. #1
    lene is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default [SOLVED] java.lang.NullPointerException while Parsing a file

    Hello,
    my problem is in following
    I have socket client-server interaction where client sends the file and server receives it. As soon as it receives it has to parse the received file for some string.
    While doing that i get
    Null pointer exeption.
    Im sure that server gets the file, but something wrong happens when the file has to be parsed. I think it just cannot find it. The code is below.

    How i can proceed?
    Thanks


    Server code:


    public class Server {
    //public static void main (String [] args ) throws IOException {
    public String getResponse () throws IOException {
    int filesize=500000; // file size
    long start = System.currentTimeMillis();
    int bytesRead;
    int current = 0;
    // create socket
    ServerSocket servsock = new ServerSocket(45789);
    while (true) {
    System.out.println("Waiting for connection");

    Socket sock = servsock.accept();
    System.out.println("Connected : " + sock);



    byte [] mybytearray = new byte [filesize];
    InputStream is = sock.getInputStream();
    FileOutputStream fos = new FileOutputStream("ResponseGot.txt");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray,0,mybytearray.length);
    current = bytesRead;

    do {
    bytesRead =
    is.read(mybytearray, current, (mybytearray.length-current));
    if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);

    bos.write(mybytearray, 0 , current);
    long end = System.currentTimeMillis();
    System.out.println(end-start);
    bos.close();
    sock.close();





    Parser parser=new Parser();
    // System.out.println("start Parser");
    String MessageGot=parser.response();

    return MessageGot;
    }

    }
    }

    ParserCode:

    public class Parser {

    public String response(){


    String string_to_find1 = "not sure";
    String string_to_find2 = "dont know";

    String arg[]=null;
    File file = null;





    // Get the file from the argument line.
    if (arg.length > 0) file = new File (arg[0]);
    if (file == null || !file.exists ()) {
    System.out.println ("Getting file ResponseGot for parsing");
    file = new File ("ResponseGot.txt");

    }


    String parsedresponse=null;

    try {
    // Create a FileReader and then wrap it with BufferedReader.
    FileReader file_reader = new FileReader (file);
    BufferedReader buf_reader = new BufferedReader (file_reader);

    // Read each line of the file and look for the string of interest.
    do {
    String line = buf_reader.readLine ();
    if (line == null) break;
    else if (line.indexOf(string_to_find1) != -1)
    parsedresponse = "not sure";
    else if (line.indexOf(string_to_find2) != -1)
    parsedresponse = "dont know";

    } while (true);
    buf_reader.close ();
    }
    catch (IOException e) {
    System.out.println ("IO exception =" + e );
    }

    System.out.printf ("The response is " + parsedresponse);

    return parsedresponse;
    }
    }


    the output i get:

    java.lang.NullPointerException
    35984
    at ....Parser.response(Parser.java:45)
    at .....PEMServer.getResponse(PEMServer.java:50)
    Last edited by lene; 05-13-2008 at 11:29 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    No one can at least compile your code to test the error. Where is you main method. We don't know the way you workout.

  3. #3
    lene is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    Well the point is that PArser and Server were separate classes with own
    "mains" but now i joined them into one big program there as you see server program is evoked and server has to evoke parser and then return the value.
    so her is the parser in the MAIN form it can be run and it gives the right answer -just get anyfile to check.
    but when evoked by server it doesnt work! :(






    import java.io.*;


    public class Parser {

    // public String response(){
    public static void main (String arg[]) {

    String string_to_find1 = "not sure";
    String string_to_find2 = "dont know";
    String parsedresponse=null;
    // String arg[]=null;
    File file = null;



    // Get the file from the argument line.
    if (arg.length > 0) file = new File (arg[0]);
    if (file == null || !file.exists ()) {
    System.out.println ("Getting file ResponseGot for parsing");
    file = new File ("ResponseGot.txt");

    }


    // String parsedresponse;

    try {
    // Create a FileReader and then wrap it with BufferedReader.
    FileReader file_reader = new FileReader (file);
    BufferedReader buf_reader = new BufferedReader (file_reader);


    do {
    String line = buf_reader.readLine ();
    if (line == null) break;
    else if (line.indexOf(string_to_find1) != -1)
    parsedresponse = "not sure";
    else if (line.indexOf(string_to_find2) != -1)
    parsedresponse = "dont know";
    //else System.out.println ("Not legal response" );
    } while (true);
    buf_reader.close ();
    }
    catch (IOException e) {
    System.out.println ("IO exception =" + e );
    }

    System.out.printf ("The response is " + parsedresponse);

    // return parsedresponse;
    }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, how did you do that communication between the server and the parser? You said that you application not working on the server, so best thing we have to check is the communication scenario.

  5. #5
    lene is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    yes as i said its socket client -server communication. The client code is below:

    import java.net.*;
    import java.io.*;

    public class Client{
    public static void main (String [] args ) throws IOException {

    Socket sock = new Socket("127.0.0.1",45789);
    System.out.println("Connected");


    // send Response file
    File myFile = new File ("Response.txt");
    byte [] mybytearray = new byte [(int)myFile.length()];
    FileInputStream fis = new FileInputStream(myFile);
    BufferedInputStream bis = new BufferedInputStream(fis);
    bis.read(mybytearray,0,mybytearray.length);
    OutputStream os = sock.getOutputStream();
    System.out.println("Sending...");
    os.write(mybytearray,0,mybytearray.length);
    os.flush();
    sock.close();
    }

    }


    and now i made stand alone MAIN with server so first you run server , then client with some txt message containing string "not sure". So as soon as server will get the message it evokes parser...and here it happens! :(

    server code:



    import java.net.*;
    import java.io.*;

    public class Server {
    public static void main (String [] args ) throws IOException {
    //public String getResponse () throws IOException {
    int filesize=500000; // file size
    long start = System.currentTimeMillis();
    int bytesRead;
    int current = 0;
    // create socket
    ServerSocket servsock = new ServerSocket(45789);
    while (true) {
    System.out.println("Waiting for connection");

    Socket sock = servsock.accept();
    System.out.println("Connected : " + sock);



    byte [] mybytearray = new byte [filesize];
    InputStream is = sock.getInputStream();
    FileOutputStream fos = new FileOutputStream("ResponseGot.txt");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray,0,mybytearray.length);
    current = bytesRead;

    do {
    bytesRead =
    is.read(mybytearray, current, (mybytearray.length-current));
    if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);

    bos.write(mybytearray, 0 , current);
    long end = System.currentTimeMillis();
    System.out.println(end-start);
    bos.close();
    sock.close();


    Parser parser=new Parser();
    System.out.println("file got! - start Parser");
    String MessageGot=parser.response();
    System.out.println("the response" + MessageGot);
    // return MessageGot;
    }
    }

    }

  6. #6
    lene is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    oki i got..it was parser problem....
    arg[] =null...
    thanks for help!

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 10:01 AM
  2. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:39 PM
  3. Replies: 2
    Last Post: 07-16-2007, 07:21 AM
  4. java.lang.NullPointerException
    By Felissa in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:02 AM
  5. Replies: 2
    Last Post: 07-02-2007, 03:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •