This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InputStream NullPointerException

 
Dan Greene
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running my code through eclipse
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic