Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

NullPointerException  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Want my paintComponent to draw rects, ovals and lines but getting a NullPointerException. debugged my code to find which variable might be causing the problem but everything seems to be right.

Obviously not do!! Any help on this would be really appreciated!!






 
Master Rancher
Posts: 3633
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stack trace for the exception will show you which line the exception is occurring on.
Can you post the stack trace here?
 
chop vick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
chop vick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 81 is for(Myshape rectangles : shapes)
                    rectangle.draw(g);

[Moderator edit] Line 69 in the first code listing provided by OP above.
 
author
Sheriff
Posts: 23590
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hint: Check your shapes instance variable. Did you ever initialize it?
 
chop vick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so initialized variables and no more NullPointerException!!

But now I'm getting a blank panel.....

what should be done now?


 
Ranch Hand
Posts: 146
4
 
Bartender
Posts: 3320
86
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please BeForthrightWhenCrossPostingToOtherSites (← click).

We don't mind if you cross post but do tell us as we all give up our time here for free and would rather not waste it answering a question that has already been answered elsewhere.
 
chop vick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies everybody, was unaware and won't be doing this again. I appreciate that everyone is helping out of there own good will and time and won't be cross posting again. Thanks for your help and time code ranchers 👍🏻
 
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... from what you've posted it's kinda hard to tell how big your panel is when it's displayed. Perhaps its size is zero, in which case you won't see anything. (Which is your problem statement.) You can fix that by making the panel have a border and making the border be red, so you can see how big the bordered area appears.

And then there's your paintComponent method. We know it's being called because your original post mentioned an exception thrown by it. But I'd still recommend using the @Override annotation to ensure it's actually overriding JPanel's method.

And I'm not sure what that method is supposed to be doing. It looks like it's going through the shapes array and re-drawing them three times. Why the repetition?

you should also note that although you have three entries in the shapes array, there's no guarantee that all three of them will have shapes in them. Actually I'm not clear on what the purpose of the "input" parameter is when you aren't going to display more than three shapes.
 
Saloon Keeper
Posts: 4797
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps if you had written it like this it would have been more obvious why you're getting three repetitions.
 
Carey Brown
Saloon Keeper
Posts: 4797
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are not being used.

Do you know about Lists? Seems like a better approach than your use of arrays, and with "shapes" having a fixed length of 3.
 
Tony Docherty
Bartender
Posts: 3320
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

chop vick wrote:
But now I'm getting a blank panel.....

what should be done now?


Please can you show the code that creates and displays your panel
 
Carey Brown
Saloon Keeper
Posts: 4797
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here you are setting shapes[1] to a new MyRect3. However, in the following loop you set shapes[0], shapes[1], and shapes[2], each to yet another new MyRect3 and each of these MyRect3 instances have exactly the same parameters.

[Edit] Sorry, seems that I missed your more recent post that does not have a loop.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!