• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

NullPointerException

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!






 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
chop vick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also posted at: http://www.dreamincode.net/forums/topic/400540-problems-with-joptionpane/
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic