aspose file tools*
The moose likes Java in General and the fly likes how to skip only the lines causing nullpointer exception while reading bytestream. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to skip only the lines causing nullpointer exception while reading bytestream." Watch "how to skip only the lines causing nullpointer exception while reading bytestream." New topic
Author

how to skip only the lines causing nullpointer exception while reading bytestream.

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
I am doing a parsing operation and it gives a null pointer exception for certain characters. I want to just ignore these lines and parse rest of the file.



I tried doing the below to skip bytes causing null pointer exception as :






However it throws Null pointer exception. Why does it throw null pointer exception despite already checking the null condition in the if statement.

I have another way of skipping the bytestream causing null.It is

bytestream.skip (number of bytes to skip);


This would solve the problem. But how do I know how many bytes are to be skipped?




My code:

BufferedInputStream inStream = new BufferedInputStream(
new FileInputStream(new File(file)));
int len = inStream.available();
byte[] buf = new byte[len];
inStream.read(buf, 0, len);
inStream.close();
ByteArrayInputStream byteStream = new ByteArrayInputStream(buf, 0,
len);

try{

while (byteStream != null && byteStream.available() > 0) {

data = parser.Parse(byteStream);

}
catch(Exception e)
e.printStackTrace();
{
}


thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
We don't know what this "parser" is or what it's parsing, nor do we see the stack trace, so we can't speculate on the reasons of the exception.

One thing to try is to put the try/catch block around just the parser.Parse method call, instead of around the entire loop.


Ping & DNS - my free Android networking tools app
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Thanks. I tried that. It gave "null pointer exception" again.

Can I ask a general question regarding byteStream.skip method?. It can make one skip bytes but how to know how many bytes to be skipped as it takes as a parameter the number of bytes to be skipped. Any other way of skipping a line? Also do the code for skipping the line have to be in catch block?If no then where else can it be?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
Obviously, rearranging the code does not make the underlying problem go away. The question is whether it allows the parsing to proceed?

If this was my problem, I'd rather focus on figuring out why the exception happens in the first place, rather than trying a workaround that sounds very brittle.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10101
    
164

Like Ulf said, the right thing to do is understand what's causing the NullPointerException. Skipping a byte stream isn't an answer to solving NullPointerExceptions.

[My Blog] [JavaRanch Journal]
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 644
Obviously, rearranging the code does not make the underlying problem go away. The question is whether it allows the parsing to proceed?

If this was my problem, I'd rather focus on figuring out why the exception happens in the first place, rather than trying a workaround that sounds very brittle.


thanks
There may be some garbage data lines in code which cannot be parsed. It needs to be skipped and rest of the line should be parsed.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41823
    
  63
In which did the rearranging of the code (catching the exception inside of the loop) not help? It seems it should have allowed you to work around the problem. Post a shortest-possible excerpt of a problematic file that show the issue, and the relevant excerpt of the code that you were using to handle it, and which still failed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to skip only the lines causing nullpointer exception while reading bytestream.