• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException on Array of User Defined Class  RSS feed

 
Sudar Shan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I am not sure if I have grasped the object concept well enough.

I am trying to create a Generic Object Streamer Class to test my understanding on 1) Generic Classes 2) ObjectOutputStream.

I have seen few discussions on objects needing to be initialized. But I don't understand why, or whats happening behind the scenes in terms of memory/pointer.

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem, as you said, is that the newEmployee reference is declared, but never initialized, meaning that at line 5 you are trying to store a reference to a new Employee instance at index 0 of an array that doesn't exist.
Initializing the array directly after you declare the reference at line 4 will fix that. This tutorial might help you.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you from a C++ background? If so then Java works differently to C++. As Jelle has pointed out you always have to assign a value to references to objects otherwise the reference will be null (if it is a class or instance field), or will fail to compile (if its a local variable).
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that code actually compile? I am surprised you get a null exception, because it should produce a compiler error that the array has never been initialised.

You do not need to know what is going on in terms of memory or pointers. Also remove the no‑arguments constructor from the Employee class, because that is a potential source of errors.
 
Sudar Shan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the code did not compile. However, when I declared the array as "Employee[] newEmployee = null", I got it compiled but it threw the run time error.

I have been coding in IBM i for 8+ years (COBOL and RPG ). Java is very new to me, please excuse if I ask way too basic questions. I believe in writing silly programs like these. This makes me understand the concepts and to be able to identify the issue if it were to occur again (though you guys might facepalm)

I have removed the no argument constructor, something isn't right yet:
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What "isn't right"?
Remember that ItDoesntWorkIsUseless (this is a link).

This code compiles fine (not counting missing curly brace at the end).

If the output is what you don't like you should override toString() method.
 
Sudar Shan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
apologies Pawel, I will try to provide more information next time. And as you said, I am missing the override to toString method.

I would also like to know why no argument constructor is bad.

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudar Shan wrote:I would also like to know why no argument constructor is bad.

It is not bad in general. It was bad in your class because it allowed to create an instance with inproper state (wrong id and null name).
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not always bad. However your Employee class has internal data that needs initialising. If you use a no-argument constructor it won't be initialised.
 
Sudar Shan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all for your answers. I will be more careful when I post the next time.

BTW, I think there should be a quiz on HowToAskQuestions.. for new forum members ;)
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this?
HowToAskQuestionsOnJavaRanch
 
Sudar Shan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes but a quiz! And grant a cow for someone who answers all questions on it! ...nah, I am digressing. Its ok. Good day guys.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look in the books, they tell you that a constructor is there to enable you to instantiate a class. But the truth is, the constructor is there to restrict instantiation. If you have one constructor with
String int
as the parameter types, that tells users, “yes, you can instantiate this class but you must provide me with a String and an int.”
You can add more constraints to it, for example:-You can read about requireNonNull here. There are other ways to verify the contents of a String.

If you look in the Java® Language Specification (this is one of the few parts easy to understand), you find that you can use the access modifier on the constructor to restrict instantiation a different way.
 
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!