Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem Using an Array and Object of an Instantiable Class

 
Scott Kamen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application that has an instantiable Student class that creates objects that can store information about a student's name , major , credits earned and whether or not they're a senior. I have created set and get methods to get and change the information in these objects. I just created an array of 3 Student objects and I'm trying to initialize them by assigning their names (which is to be hardwired into the code). The problem is when I run this I get a runtime error stating that I have a null pointer exception. The code below is the setName method from my Student class.

The code below here is showing how I am trying to assign the name values to the Student objects.

I could be really off-ball here but I just can't figure out what's causing the runtime error. If anyone could provide some tips that would great.
thanks,
-Scott
 
Murali Nanchala
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The NullPointerException is not thrown in the Student Class, instead it is thrown in the Class you are creating the array of Students.
The following array
int[] i = new int[3];
holds three integers, all defaulted to zero.
Any guesses on what is this array actually holds:
String[] s = new String[2];
Two null Objects!!
 
Scott Kamen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to initialize the Student objects now but I keep getting a "cannot resolve symbol" compile error when I try to run it. To initialize the Student objects I'm using:

I have a constructor (the only constructor in this class) set up in my Student class in order to construct objects w/ the parameters I'm providing in the statements above so I'm not sure what the problem is. My constructor for my Student class is:

thanks again,
-Scott
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have already made this comment on another one of your posts.
Your "set" methods are doing more than just assigning a value. They have logic in them. Which is the biggest programming sin, but I was wondering if that may be part of your problem. Do you have print statements in your "set" methods to check that things are going as expected. The more common way of setting values in a constructor is to just assign to the instance variable. The "set" methods are for other classes to use on this class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic