Your problem is that your constructor expects a string to be passed in. In your main method, you are not passing anything. Two ways to fix it.
1 - Pass a string in your call to Bird.
Bird x = new Bird("Robin");
2 - Create a second constructor that will set a default value for the birdName. One way to do it is like this:
Bird() {
Bird("Eagle");
}
It also looks to me like you should anticipate some problems with the line:
Bird name = new Bird ("Eagle");
It doesn't make sence to me.
One more thing, since you have defined a "getter", you should use it.
System.out.println( x.getName() );
Keep on coding, you are getting there.
2007-06-25 07:42:52
·
answer #1
·
answered by Dilbert's Desk 5
·
0⤊
0⤋
You initialized the array purchase, but you never assigned aPurchase objects as any of its items. You left it with nothing in it. So when Java first looks at that first array index, it finds a null pointer in the array, decides (correctly) that there's no object being pointed to, and throws an error. This is exactly what you would expect. In order to avoid the error, you need to either put some objects in the array, or perform an explicit check for null on each iteration. It may be that you assumed array declaration would work the same way in Java as it does in C/C++. In C/C++, your code would work, because an array is actually a set of objects of that type in memory. Java isn't like this. An array of aPurchase objects in Java is not actually a set of aPurchase objects, but rather a set of pointers to aPurchase objects. Those pointers start out null, i.e. not pointing to anything. Also, ten million objects is one hell of a crapload of objects. Even without actually initializing any of the contents of that array, it's still going to take up 40 megabytes of memory just storing the pointers. When you see yourself initializing an array to some giant quantity like that, you should probably ask yourself whether a dynamically sized data structure such as an ArrayList might not work better.
2016-04-01 03:51:38
·
answer #2
·
answered by Edeltraud 4
·
0⤊
0⤋
when each new class is created, a default constructor with zero param is given.
example:
Bird(){ }
when u create ur own constructor,
Bird(String birdName)
{
this.birdName=birdName;
}
the default constructor is gone. So when u run Bird x = new Bird(); this constructor no longer exist.
Solution:
Recreate a new constructor with zero param,
Bird(){ }
:)
2007-06-26 23:08:37
·
answer #3
·
answered by Zeus 3
·
0⤊
0⤋
I'm still trying to figure out why there's an object being constructed in the class itself.
Look at this line:
Bird name = new Bird("Eagle");
Aside from that, the other folks were accurate in their assessment. The last line in your "main" function should preferable look like this: "System.out.println (x.getname());"
2007-06-25 07:58:02
·
answer #4
·
answered by Chris C 7
·
0⤊
0⤋
You must supply a string name for the Bird object in your constructor call. There's no default (or empty) constructor signature, so you _have_ to supply a string argument.
2007-06-25 07:37:53
·
answer #5
·
answered by Dr.Mr.Ed 5
·
0⤊
0⤋