aspose file tools*
The moose likes Beginning Java and the fly likes Access a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access a variable" Watch "Access a variable" New topic
Author

Access a variable

Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
I am writing a program that asks a user how many sides a shape has. then it draws the shape on screen . I've created a polygon shape unintilized then after the user has decided how many sides the shape has got . I then initialize it with the values, but when i try to draw it. It produces the srror message
C:\DocumentsandSettings\IainPalmer
\Java2dGraphics\src\java2dgraphics
\TriangleTest.java:317:
local variable s is accessed from within inner class;
needs to be declared final
g.drawPolygon(s);
1 error

here is the code whic is causing me problems

and
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
The variable s that g.drawPolygon(s) is using is the one in the method declaration public void drawShape(Polygon s), not instance variable s from the TriangleTest object. Since it doesn't seem that you edit the polygon in that method, you could simply change the declaration to public void drawShape(final Polygon s)
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
Thanks it compiles fine now but i'm getting a null pointer exception. I can't understand why , although I've not initialized variable 's' to start off. When I've tried to do something with it I think I've initialized it or am I Wrong the error message I get is

Exception in thread "main" java.lang.NullPointerException
at java.awt.Polygon.<init>(Polygon.java:122)
at java2dgraphics.TriangleTest.<init>(TriangleTest.java:27)
at java2dgraphics.TriangleTest.main(TriangleTest.java:31)
Java Result: 1

the lines of code it referrs to are
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I believe your problem is here

You are passing null arrays to the Polygon constructor for your x and y values.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
but am I wrong in thinking by the time I want to access them I've initialized them as in
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
Sorry I forgot to put the code in

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
What's happening is that you are not just declaring a variable s of type Polygon that you will use later, you are actually instatiating a new Polygon and using the null variables that you just declared.
[ January 28, 2006: Message edited by: Garrett Rowe ]
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
Success I initialized then
Iain Palmer
Ranch Hand

Joined: Jul 28, 2004
Posts: 56
Success I initialized then

like this and no problem now thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access a variable