Dopo aver cercato fra i diversi topics che ci sono nel forum su questo problema, mi sono deciso a chiedere una mano perchè proprio non sono riuscito a capire dove sta il problema. In una classe chiamata malunni ho una maschera dove visualizzo , per adesso due jlabel. In una di queste metto del testo ottenuto da un array che si chiama cl e che si trova in un'altra classe chiamata lAlunni. L'array cl è un array monodimensionale formato da elementi di una terza classe chiamata alunno e che contiene quattro campi stringa con le relative funzioni di put e get. Tutte e tre le classi non sono static e nemmeno le varie funzioni. L'errore di compilazione me lo da sulla istruzione seguente: JLabel label = new JLabel(lAlunni.cl[1].getProg()); che fa parte della classe malunni all'interno della funzione maschera. Per chiarire meglio riporto di seguito le tre classi che suno in tre files diversi. Ringraziando per l'aiuto e i chiarimenti che vorrete darmi, invio cordiali saluti.
codice:
package registro;
import java.awt.*;
import javax.swing.*;
public class malunni
{
public void maschera()
{
System.out.println("Eseguo il metodo ma");
JFrame f = new JFrame("Esempio 1");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel(lAlunni.cl[1].getProg());
JLabel label1 = new JLabel("Carmine Marotta");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 3, 5, 5));
panel.add(label);
panel.add(label1);
f.setContentPane(panel);
f.setBounds(200,100, 300,150);
f.setVisible(true);
}
}
codice:
package registro;
import java.io.*;
public class lAlunni
{
alunno[] cl = new alunno[500];
public void letturaAlunni() throws IOException
{
char chPre, ch = ' ';
String st = new String();
for(int i=0; i<500; i++)
{
cl[I] = new alunno();
}
int a, l,lNomeMax = 0, lCognomeMax = 0, in = 0, n_alunni = 0;
FileInputStream fin;
FileOutputStream fout;
try {
fin = new FileInputStream("classiMie.txt");
}
catch(FileNotFoundException exc)
{
System.out.println("File non trovato");
return;
}
catch(ArrayIndexOutOfBoundsException exc)
{
System.out.println("Uso File classiMie.txt");
return;
}
do {
a = fin.read();
if(a != -1)
{
ch =((char) a);
if((ch != '\t') & (ch != '\n') & (ch != '\r'))
{
st+=ch;
}
if(ch == '\t')
{
if(in == 0)
{
cl[n_alunni].putNome(st);
}
if(in == 1)
{
cl[n_alunni].putCognome(st);
}
st = "";
in++;
}
if(ch == '\n')
{
if(in == 2)
{
cl[n_alunni].putClasse(st);
cl[n_alunni].putProg(n_alunni + 1);
}
st = "";
in = 0;
System.out.print(cl[n_alunni].getProg() + " ");
System.out.print(cl[n_alunni].getCognome() + " ");
System.out.print(cl[n_alunni].getNome() + " ");
System.out.println(cl[n_alunni].getClasse());
n_alunni++;
}
}
}
while(a != -1);
fin.close();
//Apro il file di output
try {
fout = new FileOutputStream("alunniClassiOld.txt");
}
catch(FileNotFoundException exc)
{
System.out.println("Errore nell'apertura del file di output.");
return;
}
catch(ArrayIndexOutOfBoundsException exc)
{
System.out.println("alunniClassiOld.txt");
return;
}
try {
for(int i = 0; i < n_alunni; i++)
{
st = cl[I].getProg();
for (int j = 0; j < st.length(); j++)
{
a = st.charAt(j);
fout.write(a);
}
fout.write('#');
st = "";
st = cl[I].getCognome();
for (int j = 0; j < st.length(); j++)
{
a = st.charAt(j);
fout.write(a);
}
fout.write('#');
st = "";
st = cl[I].getNome();
for (int j = 0; j < st.length(); j++)
{
a = st.charAt(j);
fout.write(a);
}
fout.write('#');
st = "";
st = cl[I].getClasse();
for (int j = 0; j < st.length(); j++)
{
a = st.charAt(j);
fout.write(a);
}
fout.write('#');
fout.write('\n');
st = "";
}
}
catch(IOException exc)
{
System.out.println("alunniClassiOld.txt");
}
fout.close();
}
}
codice:
package registro;
public class alunno
{
String Prog;
String Nome;
String Cognome;
String Classe;
public void putProg(int np)
{
Prog = new String(String.valueOf (np));
}
public void putNome(String n)
{
Nome = new String(n);
}
public void putCognome(String c)
{
Cognome = new String(c);
}
public void putClasse(String c)
{
Classe = new String(c);
}
public String getProg()
{
return Prog;
//return Integer.parseInt(Prog);
}
public String getNome()
{
return Nome;
}
public String getCognome()
{
return Cognome;
}
public String getClasse()
{
return Classe;
}
}