permaculture playing cards*
The moose likes Beginning Java and the fly likes Problem Using an Array and Object of an Instantiable Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem Using an Array and Object of an Instantiable Class" Watch "Problem Using an Array and Object of an Instantiable Class" New topic
Author

Problem Using an Array and Object of an Instantiable Class

Scott Kamen
Greenhorn

Joined: Apr 01, 2003
Posts: 15
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

Joined: Mar 14, 2001
Posts: 74
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!!


"It is almost always answered in the API" - Murali Nanchala
Scott Kamen
Greenhorn

Joined: Apr 01, 2003
Posts: 15
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

Joined: Mar 16, 2001
Posts: 984

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.


Please ignore post, I have no idea what I am talking about.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem Using an Array and Object of an Instantiable Class
 
Similar Threads
Arrays: Calling input from other methods and classes
Passing Objects Created By User to Array in Different Class
problem with hashCode()
getting NullPointerException
Adding object to array