• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access a variable

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I forgot to put the code in

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Success I initialized then
 
Iain Palmer
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Success I initialized then

like this and no problem now thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic