File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread "main" java.lang.NullPointerException" Watch "Exception in thread "main" java.lang.NullPointerException" New topic
Author

Exception in thread "main" java.lang.NullPointerException

Guy Rich
Ranch Hand

Joined: May 03, 2011
Posts: 33

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

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: May 03, 2011
Posts: 33

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

Joined: Oct 02, 2003
Posts: 11175
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: May 03, 2011
Posts: 33

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.
 
subject: Exception in thread "main" java.lang.NullPointerException
 
Similar Threads
variable RespChnl might not have been initialized
IndexOutOfBoundsException thrown by java.nio bytebuffer.put(byte[] arsrc, int offset , int length)
exposing objects built in one method to be "accessed" in another method
"member" variables and "local" variables
Trying to Understand Java and JRE