Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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!!






 
Rancher
Posts: 3385
38
  • 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
Marshal
Posts: 23436
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: 3306
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: 23451
46
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: 4058
48
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: 4058
48
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: 3306
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: 4058
48
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!