This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes InputStream NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "InputStream NullPointerException" Watch "InputStream NullPointerException" New topic
Author

InputStream NullPointerException

Dan Greene
Greenhorn

Joined: Mar 09, 2013
Posts: 5
Self taught novice here so be gentle. This program was given to me by a guy I work with and he moved away so we cant get hold of him. Anyway Im trying to loop data into a buffer, using ByteArrayOutputStream, till all the data stops arriving. I have put this together through research but Im getting a nullpointerexception and not sure why. Any help would be great.


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

The more relevant information you provide, the better folks here will be able to help you. In this case, which exact line is causing the NPE?

Also, this is not the way to handle exceptions:



It says, "If something goes wrong, print out that fact, but then continue on as if everything is fine, even though it clearly isn't."

If you're going to catch an exception, you have to actually do something to correct it, or else rethrow it. Just catching it doesn't make the problem go away. So if you can't retry or provide some reasonable default in place of what the try block was supposed to do (and the vast majority of the time, you can't), then there's no reason to catch the exception, unless you're going to wrap it in an exception more appropriate to expose to the layer above and rethrow it. Just continuing on like everything is fine will not work.
Dan Greene
Greenhorn

Joined: Mar 09, 2013
Posts: 5
Thank you for the advice with the errors.

The big prob with the nullpointeterexcep is that there is no line referenced. That's why I'm looking for help. If I had a reference I would be just a touch less clueless. I'm not sure if my refernce to the size of data, if statement, is right that is my thought
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dan Greene wrote:Thank you for the advice with the errors.

The big prob with the nullpointeterexcep is that there is no line referenced.


Then you're running you code in some bizarre manner that I can only recommend strongly against. Run it from the command line, or in a decent mainstream IDE, and you'll get a stack trace.

Otherwise about all you can do is either A) Study the code intently until you find the problem, B) Get somebody to study the code intently for you until they find the problem, of C) Add a bunch of print statements to narrow down where it's blowing up.

I'd never try to diagnose an exception without a stack trace.
Dan Greene
Greenhorn

Joined: Mar 09, 2013
Posts: 5
I am running my code through eclipse
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

It's been a while since I used Eclipse, but I know it has a console that shows the line that caused the error, and a stack trace. There must be something in your settings where you've got it hidden. If you google for how to see stack trace in eclipse, you should find instructions as well as images.

James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

"The big prob with the nullpointeterexcep is that there is no line referenced"

In your catch block, use the following line:



Hopefully this will give you more information on the exception.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

James Boswell wrote:"The big prob with the nullpointeterexcep is that there is no line referenced"

In your catch block, use the following line:



Hopefully this will give you more information on the exception.


Good catch.

@OP: This is goes back to when I said that you shouldn't just catch the exception, ignore it, and move on. I should have mentioned that at the very least you ought to call printStackTrace(), as James points out. Sorry for missing that. A bit scatterbrained today I guess.

Note, though, that even calling printStackTrace() isn't proper exception handling. It provides diagnostic information to help you figure out what the problem is, but in the end, if that's all you do, then your code is still ignoring the fact that something went wrong and continuing on as if all was well. If you had got rid of the catch completely (the "don't bother catching it if you won't fix or rethrow it" part of my earlier post), then the JVM would have printed out the stack trace when your program failed.

Dan Greene
Greenhorn

Joined: Mar 09, 2013
Posts: 5
Thank you guys very much for the help with the print stack trace. I was able to find the problem is the call. I need to find a way to loop the another packet into the buffer now. I thought calling a new reciever would initiate that but I was wrong. Thanks again guys
Dan Greene
Greenhorn

Joined: Mar 09, 2013
Posts: 5
Last thing on this I dont know if I wrote it wrong but when I change "Reciever" to "Packet" I now get an error with .

I thought I had that coded right what did I do wrong.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

Dan

I think you need to post more of your code (like what Receiver and Packet do). Also, post the latest stacktrace of the exception you are getting.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InputStream NullPointerException
 
Similar Threads
Presenting image read from database in a html page
Cannot download the whole text file
Reading a large file, getting out of memory error.
upload file through applet
Adding Header and Footer to an existing PDF