For loop iteration won't function correctly

[Closed]
Report
Posts
10
Registration date
Tuesday December 11, 2012
Status
Member
Last seen
February 4, 2013
-
Posts
8
Registration date
Friday May 24, 2013
Status
Member
Last seen
May 27, 2013
-
Hello,
I am trying to make this pie chart program work in NetBeans 7.2.1, but it won't compile for a reason I cannot grasp....
This is the block of code that is creating issues:

for (int i = 0; i < values.size(); i++) {
                g2d.setColor(colors.get(i));

and this is the errors I am getting from it:

method setColor in class Graphics cannot be applied to given types;
required: Color
found: Object
reason: actual argument Object cannot be converted to Color by method invocation conversion

Can someone please tell me what is happening here, because this program runs perfectly from the desktop, in the command prompt, but I need it to be in Net5Beans so that I can use it for another program I am working on??
PLEASE HELP ME!!

3 replies

Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768
Just from few lines one reason could be that "colors.get(i)" is object that you may have to explicitly cast as Color
g2d.setColor((Color)colors.get(i));
Posts
10
Registration date
Tuesday December 11, 2012
Status
Member
Last seen
February 4, 2013

@rizvisa1,
I figured that as well, but what was confusing me was that this EXACT code ran fine when I compiled and ran it from the command prompt, but had issues when I sposed the code into NetBeans, and that was freaking me out a bit??
Posts
10
Registration date
Tuesday December 11, 2012
Status
Member
Last seen
February 4, 2013

I don't believe so, I have the latest run-time environment installed, if I am not mistaken....
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768
i would uninstall and re install netbeans, if i were you. You can use eclipse too
Posts
10
Registration date
Tuesday December 11, 2012
Status
Member
Last seen
February 4, 2013

I( may end up doing that, as soon as I am sure that it is not something wrong with the code!!
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768
Since you said code works in desktop and only in IDE it is causing issue, I dont think any thing is wrong with code. Some how IDE is corrupted to some extent or using different java version. See if this helps
http://forums.netbeans.org/ptopic6210.html
Posts
10
Registration date
Tuesday December 11, 2012
Status
Member
Last seen
February 4, 2013

Yeah, I am about to the point now where I think it might be a problem with NetBeans. I think I will reinstall it from scratch!!
Posts
8
Registration date
Friday May 24, 2013
Status
Member
Last seen
May 27, 2013

g2d.setColor((Color)colors.get(i));

Try this.....
This is called downcasting