Click to See Complete Forum and Search --> : Component.x is not visible


renevanh
11-19-2008, 05:25 PM
While updating/refreshing my Java skills, I run into an error I can't get solved because I just don't understand what could be wrong.

The code of the class I'm working on:


import java.awt.*;


public class Staafdiagram extends MSO{


int r=0, g=0, b=0;
int boven = 50;

public void paint(Graphics gr){


int links = 275;
int breedte = 10;
int hoogte = 5;
Color c = new Color(r,g,b);
for(int x=0; x<allunis.length; x++);
gr.setColor(c);
gr.fillRect(links, boven, breedte, hoogte);
gr.drawString(allunis[x].naam, links+20, boven);
newuni();
}

public void newuni(){
r+=20;
g+=20;
b+=20;
boven+=20;
}

}


The error:


The field Component.x is not visible


It is about the line: gr.drawString(allunis[x].naam, links+20, boven);
Somehow, there is something wrong with allunis or the x itself.
allunis is an array created and populated in the MSO class and is working fine.
Since allunis is a 'global variable' in MSO (ok, it's called different in Java, but I lost it) and the current class extends MSO, it should be working (and it does in the for loop definition).
The quickfix option of Eclipse only suggest to create a local variable x, but that doesn't solve the problem (there is a local x already anyway).

Anyone around who can help me out?

chazzy
11-19-2008, 08:01 PM
sounds like the compilation error is in MSO, not this class.

Assuming that MSO extends Component, you're using component.x instead of component.getX() to reference the member.

renevanh
11-20-2008, 04:00 AM
I'm not sure about that... it says the error is in staafdiagram.java and the x in the line has a red underline.

Just to be sure, this is MSO.java:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MSO extends JApplet
implements ItemListener, ActionListener
{

uni [] allunis;
JTextField tf = new JTextField(5);
JComboBox selectUni = new JComboBox();

public void init() {

JPanel controls = new JPanel();
Container c = this.getContentPane();

c.add(controls, BorderLayout.SOUTH );

//haal de parameters op uit de HTML code en zet ze in array's

String UNIVERSITEITEN = this.getParameter("UNIVERSITEITEN");
String UNIS[] = UNIVERSITEITEN.split(" ");
String [] VALUE;
VALUE = new String[UNIS.length];
allunis = new uni[UNIS.length];

for (int t=0; t<UNIS.length; t++) {
VALUE[t] = this.getParameter(UNIS[t]);
}

//koppel de namen en de parameters vervolgens in een object uni
for (int x=0; x<UNIS.length; x++) {
allunis[x] = new uni(UNIS[x], VALUE[x]);
}

//Maak een ComboBox om te kiezen welke waarde in het textveld moet komen
for (int x=0; x<allunis.length; x++) {
selectUni.addItem(allunis[x].naam);
//tf.setText(allunis[x].value);
}
JButton changebutton = new JButton("Update");
selectUni.addItemListener(this);
changebutton.addActionListener(this);
controls.add(selectUni);
controls.add(tf);
controls.add(changebutton);

}

public void itemStateChanged(ItemEvent event) {
Object item = event.getItem();
String naam = "" + item;
for (int x=0; x<allunis.length; x++) {
if(allunis[x].naam.equals(naam)) {
tf.setText(allunis[x].value);
}
}
}

public void actionPerformed(ActionEvent e) {
String waarde = tf.getText();
Object item = selectUni.getSelectedItem();
String naam = "" + item;
for (int x=0; x<allunis.length; x++) {
if(allunis[x].naam.equals(naam)) {
allunis[x].value = waarde;
}
}
this.repaint();
}
}

chazzy
11-20-2008, 05:44 AM
so what happens when you change all of your int x's to int i's?

renevanh
11-20-2008, 12:49 PM
The it gives me "i cannot be resolved"...

When I edit the location of the declaration of i it works just fine.


public void paint(Graphics gr){

int i;
int links = 275;
int breedte = 10;
int hoogte = 5;
Color c = new Color(r,g,b);
for(i=0; i<allunis.length; i++);
gr.setColor(c);
gr.fillRect(links, boven, breedte, hoogte);
gr.drawString(allunis[i].naam, links+20, boven);
newuni();
}


Kinda weird... :confused:

Anyway: now I'm getting an "java.lang.ArrayIndexOutOfBoundsException: 6" about the allunis array, which is weird to, because the array has a length of 6 in this case, so it's not out of bounds at all when i<allunis.length (i=5, so allunis[5] is the last one called).

chazzy
11-20-2008, 05:12 PM
haha that's what I get for being blind.

I didn't realize you had a ; at the end of your for loop


for(int x=0; x<allunis.length; x++);
gr.setColor(c);
gr.fillRect(links, boven, breedte, hoogte);
gr.drawString(allunis[x].naam, links+20, boven);
newuni();


Should be


for(int x=0; x<allunis.length; x++){
gr.setColor(c);
gr.fillRect(links, boven, breedte, hoogte);
gr.drawString(allunis[x].naam, links+20, boven);
newuni();
}

renevanh
11-20-2008, 06:00 PM
Omg... that's stupid...