Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException at runtime

 
Bhathiya Pathirana
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get these errors when i'm trying to add data


My coding...
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't initialize your queArray instance variable -- in your Queue inner class... hence, it is null.

Henry
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry Wong Subject line is changed Thank for the info
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is functioning properly
 
Bhathiya Pathirana
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the line throwing exception
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i'm debugging

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

 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IT'S LOCAL TO THE CONSTRUCTOR. Please KeepItDown, self.

It's local to the constructor.
 
Bhathiya Pathirana
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
queArray is not null
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eoin Brosnan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic