• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.Nulll Pointer Exception  RSS feed

 
Jakub Nosal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still a newbie when it comes to programming, but I think I'm grasping the basics pretty well. I don't understand though, why whenever I try to use a method that is using any of the
"tablicaPracownikow[j].something", the program gives out java.lang.Nulll Pointer Exception (whenever I try to use any of the printing methods, they also print some of the fields as Null).
Why is that happening?
P.S. Ignore the fact, that all the names are unrecognizible for you, it was written by Polish programmer.








 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I haven't read all your code (especially the Polish names make it really hard for me to read it). But the cause of your problem is most likely this: tablicaPracownikow[j] is null.

Note that when you create an array, like this:

then you will get an array which contains 50 times null. If you want 50 new SomeClass objects, then you have to create those objects and put them in the array yourself:

Another problem in your code:

This will not work. Use equals() instead of == to compare strings:
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to add some logging to the wpiszUrzednika and wpiszRobotnika methods so that you know for which values of "i" array elements get created.

Note that strings are compared using the "equals" method, not the "==" operator - I see that several times.

Also, delete lines 14 and 21 - they do nothing.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read about System#out, and click on the method name println(Object), and keep clicking, you find it leads you to String#valueOf(java.lang.Object) and when you read that, it tells you what happens if you pass null to that method.
 
Jakub Nosal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I've used your advice correctly, but the error is still there.
My code post-changes (also made some minor changes that should not influence the error).









 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jakub Nosal wrote:I think I've used your advice correctly, but the error is still there.

And which error is that - line? stack trace? If it's still an NPE, which object is null? Assuming you're trying to access an element of the array - which index? Has the element at that index ever been set to something other than null? And I concur with Jesper: the Polish variable and method names make this code hard to make sense of.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not if you are Polish, but most of us are not Polish and cannot read the names.
Another thing which makes the code hard to read is the long lines. Please look up these formatting suggestions. I have sorted most of the long lines, and changed some of the lines where you had {} crammed against the code, so you can see how it is done. Spaces around = etc would also improve the code.

Find which line produces the Exception and print every object in sight just before that line. Find which is null.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!