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
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; holds three integers, all defaulted to zero. Any guesses on what is this array actually holds: String s = new String; Two null Objects!!
"It is almost always answered in the API" - Murali Nanchala
Joined: Apr 01, 2003
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:
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.