Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2008, 12:26 PM
Member
 
Join Date: Apr 2008
Posts: 5
lene is on a distinguished road
[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 12:29 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-13-2008, 01:06 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-13-2008, 02:25 PM
Member
 
Join Date: Apr 2008
Posts: 5
lene is on a distinguished road
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;
}
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-13-2008, 02:30 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-13-2008, 03:13 PM
Member
 
Join Date: Apr 2008
Posts: 5
lene is on a distinguished road
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;
}
}

}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-13-2008, 04:00 PM
Member
 
Join Date: Apr 2008
Posts: 5
lene is on a distinguished road
oki i got..it was parser problem....
arg[] =null...
thanks for help!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-14-2008, 06:04 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok, if you solve this problem please mark it as solved.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.NullPointerException stevemcc AWT / Swing 2 02-08-2008 11:01 AM
java.lang.NullPointerException ravian New To Java 1 01-13-2008 09:39 PM
AWT-EventQueue-0 java.lang.NullPointerException susan NetBeans 2 07-16-2007 08:21 AM
java.lang.NullPointerException Felissa Advanced Java 1 07-05-2007 08:02 AM
Error Java.lang.NullPointerException in JBuilder2006 Jack Other IDEs 2 07-02-2007 04:29 AM


All times are GMT +3. The time now is 07:13 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org