wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Where am I getting this from? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Where am I getting this from?" Watch "Where am I getting this from?" New topic

Where am I getting this from?

Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
Dumb question
I have another thread going for my nullPointerException error
and am still troubleshooting it but I'm wondering if this might
be the problem, or just something I'm not getting at the moment.

Our class has us coding from pseudo code. Not a good way to learn Java.

the pseudo I have a question about is this:

This is my coding for the class it appears in:

Sorry to use so much code for the question.
What I'm wondering is ----
where does currentShape come from.
I have 11 class files in this project, and I'm not defining it anywhere that I can see. This is early in the main method, so it should be defined and initialized or assigned or whatever before I'm using it, but if it's there I'm blind. Did the teacher throw a curve and I'm using the wrong name maybe? or is this some internal JAVA thing that's refering to the array created in myFields. It has a shape field in it.

The more I learn the more confused I get


I need to get this running and turned in tonight, last project of the semester.

Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265


As I read your code, currentShape is created in your foreach loop:

I suspect your problem is in your method loadShapesArray(), where you declare a local Shape shapes[] instead of using the ShapeTest static member shapes[].

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
Thanks Stevi

a basic question I must of missed:

the shapes[] in the loadShapesArray method is
and instance variable.

loadShapesArray is private static

is the instance created differently than the local?

it's originally created as a private static constructor
of the ShapeTest class

so it's available throughout that class?

by declaring it again in the private static loadShapesArray
method of ShapeTest class doesn't that let me modify it
within the loadShapesArray?

But then again, I'm calling loadShapesArray from the main method
of ShapeTest, so I must be passing values back to the main somehow?

I'm very confused right now.

Any suggestions?

Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265


Java will let you declare variables of the same name in different scopes.

That's why you can do things (example is for an instance variable):

Since you delcare a new reference, Shape shapes[], it's this reference shapes that loadShapesArray will use. It then promptly marks it eligible for GC when you leave loadShapesArray(), and you lose all the work you did to load your array. The class static variable shapes is still null, hence your NPE.

I think you'll find if you modify


that you'll be using the ShapeTest private static shapes as you are intending.
[ May 09, 2008: Message edited by: Stevi Deter ]
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
Thanks Stevi

That was it

and I made my midnight deadline to turn it in too.

Now to take my final exam,

then I can get Head First Java and "really" learn this stuff.

Thanks everyone

I'm sure I'll have a few thousand questions as I study on my own this summer.
It is sorta covered in the JavaRanch Style Guide.
subject: Where am I getting this from?
Similar Threads
enhanced for loop hangup?
Null Pointer Exception
the instance variable shapes
lost n confussed