• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException error in a simple java code  RSS feed

 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello dear All,
I wrote a simple program, but unfortunately it has an error, java.lang.NullPointerException.
it would be nice if you could help me,thank you in advance.

defined class:



class using the defines class:

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

In which line of code does the NPE hapen? Post the entire stack trace.

I note that in the "KartenEingeben" method you're accessing "karten[i]", but I don't see any Aufgabe32 objects ever being instantiated. That is likely the reason.
 
David Freed
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much.

the complete error is here :


i thought it is enought to instantiate the array one time and then we can pass it to the method :





 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you do only need to instantiate the array once and yes you can pass it around but you need to put something in it before you try to use it.

Creating an object array just gives you an array of the specified type and size but all the elements in the array are null. If you want the array to be full of Aufgabe32 objects then you need to iterate over the array and assign a new Aufgabe32 object to each element before passing it to your methods.
 
David Freed
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, great , now it works well.




 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Elements of array you are trying to use are null. Instantiate an array with some data before fetching the same. In method KartenEingeben(Aufgabe32[] karten(), argument "karten" referring to an array object with null values.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!