File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access a variable

 
Iain Palmer
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe your problem is here

You are passing null arrays to the Polygon constructor for your x and y values.
 
Iain Palmer
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but am I wrong in thinking by the time I want to access them I've initialized them as in
 
Iain Palmer
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I forgot to put the code in

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Success I initialized then
 
Iain Palmer
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Success I initialized then

like this and no problem now thank you
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic