Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where am I getting this from?

 
Richard Chambers
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

HELP PLEASE

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

Thanks
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard,

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[].
 
Richard Chambers
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard,

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



to



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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic