jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes java.lang.NullPointerException at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.lang.NullPointerException at runtime" Watch "java.lang.NullPointerException at runtime" New topic
Author

java.lang.NullPointerException at runtime

Bhathiya Pathirana
Greenhorn

Joined: Aug 01, 2010
Posts: 9
i get these errors when i'm trying to add data


My coding...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

You didn't initialize your queArray instance variable -- in your Queue inner class... hence, it is null.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

More specifically, this is what's in your constructor:The stack trace gives the precise line in your code that starts down the path to the exception. One you have that line number, work backwards in your code.

The code is nearly unreadable because of the lack of proper indentation. Also, class names should begin with a capital letter.
Bhathiya Pathirana
Greenhorn

Joined: Aug 01, 2010
Posts: 9
@Henry Wong
i initialized queArray variable


this time i don't get any errors but it does not get filled up so when i retrieve it is EMPTY
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseAMeaningfulSubjectLine when posting. Explaining your question or remark briefly on the subject line allows us to know what we are getting into, and if we will be able to help.
Bhathiya Pathirana
Greenhorn

Joined: Aug 01, 2010
Posts: 9
@Henry Wong Subject line is changed Thank for the info
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Bhathiya Pathirana wrote:@Henry Wong
i initialized queArray variable


this time i don't get any errors but it does not get filled up so when i retrieve it is EMPTY



That's not the correct fix. The correct fix is to do it correctly in your constructor. With this change, the array size and the maxsize variable may be out of sync.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Bhathiya Pathirana wrote:@Henry Wong Subject line is changed Thank for the info


Thanks for letting me know... but it wasn't me who requested it...

Henry
Bhathiya Pathirana
Greenhorn

Joined: Aug 01, 2010
Posts: 9
This is functioning properly
Bhathiya Pathirana
Greenhorn

Joined: Aug 01, 2010
Posts: 9
this is the line throwing exception
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Bhathiya Pathirana wrote:This is functioning properly


Depends on what you mean by functioning properly. If one of the tasks to do is to initialize the queArray *instance* variable, then your constructor doesn't do that.

Henry
Bhathiya Pathirana
Greenhorn

Joined: Aug 01, 2010
Posts: 9
When i'm debugging

above lines are successfully executed and the variable values are
queArray==#1020(length=5)
maxSize==5

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Bhathiya Pathirana wrote:When i'm debugging

above lines are successfully executed and the variable values are
queArray==#1020(length=5)
maxSize==5




Yes... but what about the queArray *instance* variable? I believe you that you set the queArray *local* variable correctly.

Henry
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

IT'S LOCAL TO THE CONSTRUCTOR. Please KeepItDown, self.

It's local to the constructor.
Bhathiya Pathirana
Greenhorn

Joined: Aug 01, 2010
Posts: 9
queArray is not null
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Bhathiya Pathirana wrote:queArray is not null


Sorry, I can't think of anymore hints to get you to the solution. Just change this (in the constructor) ...



to this...




Henry
Bhathiya Pathirana
Greenhorn

Joined: Aug 01, 2010
Posts: 9
i'm really sorry, it's just i'm a beginner but thank you very much for trying to help me out
i get no error but when i try to retrieve, it is empty
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Bhathiya Pathirana wrote:
i get no error but when i try to retrieve, it is empty


Not unexpected. All we did was help you fix one bug. You didn't expect to only have one bug right?

You still have to debug the application to locate the others.

Henry
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
Bhathiya Pathirana wrote:i'm really sorry, it's just i'm a beginner but thank you very much for trying to help me out
i get no error but when i try to retrieve, it is empty


Hi.
It's a similar error to your first one. You need to learn about the scope of instances of objects/variables.


Do you understand why that was wrong?


Can you see the conflict?

If I were you I would start with a simple example. You are using inheritance, a GUI, inner classes- all very advanced topics. Start off with a very simple example where you can enter your name and get it to return it back to you(System.out.print() will do fine!). After that get it to store a number of names and print them all back to you. Once you can do that you can start worrying about creating Person classes with name/email fields etc.

Good luck!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Eoin Brosnan wrote:Can you see the conflict?

You mean that compiler error on line 11? But if you leave out the "private" on that line then your example is a good one of local variables shadowing / hiding instance fields.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
Rob Prime wrote:
Eoin Brosnan wrote:Can you see the conflict?

You mean that compiler error on line 11? But if you leave out the "private" on that line then your example is a good one of local variables shadowing / hiding instance fields.


Once I turned off my PC last night I felt that I had made that error! Updated.
Bhathiya Pathirana
Greenhorn

Joined: Aug 01, 2010
Posts: 9
Thank you very much for you all guys being so supportive and kind about me, can anyone please let me have some hints for where i should start learning from? course names or training materials, so i can start having a good foundation..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.NullPointerException at runtime
 
Similar Threads
Error in FileReader
Form validation error
JScrollPane
Display error in CardLayout
Form validation error