• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.NullPointerException

 
Guy Rich
Ranch Hand
Posts: 33
Eclipse IDE Redhat Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all I'm getting an Exception in thread "main" java.lang.NullPointerException.

I suspect it's because I have coded the following:




I need to have these file channels and byte buffers accesable to several different methods within my main method...

Appreciate any suggestions

Thanks

Guy


 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That can't be a complete example, because SendMesg isn't called from anywhere. However, the problem may well be line 49. There you're declaring a local variable Mesg_Bufr, instead of initialising the static variable Mesg_Bufr. Which means that later, when SendMesg tries to use Mesg_Bufr, it hasn't been initialised.

(You are allowed to have variable and method names longer than 9 characters, you know! Why not messageBuffer, sendMessage, and other more readable options?).
 
Guy Rich
Ranch Hand
Posts: 33
Eclipse IDE Redhat Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew thanks for you reply ... no it's not the entire program ...

Mesg_Bufr is declared within the LU62XnsCvr Class declaration... and therefore is "global" as far as the main method and the other methods within
main are concerned (I don't get any symbol-not-found compiler errors)

Since the FileChannel and ByteBuffer class constructors "build" in essence byte string objects I'll try defining them within the LU62XnsCvr Class
and at runtime within the given method do the FileInputStream/FileOutputStream declarations since in NIO the channel "hides" the IOStream from
the program. So the IOStream object doesn't have to be "global" in scope.

Regarding method and variable names longer than nine characters... well frankly I find them less readable, and more prone to errors in "spelling".
I think 8 - 12 bytes for a "label" strikes a happy medium between being descriptive and obtuse.

Thanks again for your comments.

Guy
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy Rich wrote:Mesg_Bufr is declared within the LU62XnsCvr Class declaration... and therefore is "global" as far as the main method and the other methods within main are concerned (I don't get any symbol-not-found compiler errors)

Here's what I think is going on.

Mesg_Bufr is declared as a member variable of the class LU62XnsCvr. However, on line 49, you declare a NEW variable called Mesg_Bufr. This hides the class member variable, so the local one gets assigned the return from the ByteBuffer.allocate(MGbuflen) call.

It looks like main ends(?) but regardless, the Mesg_Bufr that was assigned something is only in-scope in the main method. However you eventually get to your SendMesg() method, the local one is out of scope, so the class level one is accessed - and since that is never initialized, you get your NPE.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy Rich wrote:Regarding method and variable names longer than nine characters... well frankly I find them less readable, and more prone to errors in "spelling".
I think 8 - 12 bytes for a "label" strikes a happy medium between being descriptive and obtuse.


I must admit I disagree (as does every set of coding standards I've seen). I didn't find your variables readable, and if I had to use them I'd have difficulty remembering exactly which letters you'd decided to omit, leading to more spelling mistakes rather than fewer. And IDEs will make handling the longer names easier anyway. But as long as you're not working on the same projects as me it's not my business .
 
Guy Rich
Ranch Hand
Posts: 33
Eclipse IDE Redhat Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your insightful comments Fred ... yes, I see what you mean... So my problem becomes one of how do I give accessabilty
to a set objects I need to use in various methods.

Now what I'm doing is defining all of the ByteBuffers in the LU62XnsCvr Class


Thanks again for your comments

Guy
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic