Avevo provato a scrivere lo stesso programma in maniera un tantino più semplice (tanto non mi importa che impieghi molto tempo per leggere visto che girerà su un computer bello potente!), tenendo conto di quanto detto negli ultimi post.
Probabilmente sarà pieno di errori (uno già me lo dà in fase di running), ma qualcuno potrebbe guardarlo per dirmi cosa non va?
codice:
import java.util.*; // non sapevo esattamente quali librerie importare
import java.io.*; //idem
public class lettura {
public static void main(String[] args) throws IOException {
BufferedReader filebuf = new BufferedReader(new FileReader("lte100-3.5-0.0.spec"));
String nextStr;
double Teff, logg, metal;
nextStr = filebuf.readLine(); // legge la prima riga del file
StringTokenizer tok = new StringTokenizer(nextStr, " ");
//assegno le prime tre variabili
Teff = Double.parseDouble(tok.nextToken());
logg = Double.parseDouble(tok.nextToken().trim());
metal = Double.parseDouble(tok.nextToken().trim());
nextStr = filebuf.readLine(); //leggo la seconda riga
tok = new StringTokenizer(nextStr, " ");
int points = Integer.parseInt(tok.nextToken());
double[] lambda = new double[points];
double[] flux = new double[points];
double[] planck = new double[points];
while (nextStr != null){
//prima categoria
int i = 0;
do {
nextStr = filebuf.readLine();
tok = new StringTokenizer(nextStr, " ");
while (tok.hasMoreElements()){
lambda[i] = Double.parseDouble(tok.nextToken());
i++;
}
}while (!(i == points));
//seconda categoria
i = 0;
do {
nextStr = filebuf.readLine();
tok = new StringTokenizer(nextStr, " ");
while (tok.hasMoreElements()){
flux[i] = Double.parseDouble(tok.nextToken());
i++;
}
}while (!(i == points));
//terzacategoria
i = 0;
do {
nextStr = filebuf.readLine();
tok = new StringTokenizer(nextStr, " ");
while (tok.hasMoreElements()){
planck[i] = Double.parseDouble(tok.nextToken());
i++;
}
}while (!(i == points));
}
filebuf.close();
System.out.println("\nTemperatura efficace = " + Teff + "\n" +
"log g = " + logg + "\n" +
"[M/H] = " + metal+ "\n" + "Numero di punti = " + points );
//visualizza tutti gli elementi in colonna
System.out.println("\nLunghezza d'onda, flusso e funzione di planck incolonnati:");
for (int j = 0; j < points; j++) {
System.out.println();
System.out.print(lambda[j] + " "+ flux[j] + " " + planck[j]);
}
}
}
Il problema si ha quando vado ad eseguire con netbeans, infatti compare la scritta:
init:
deps-jar:
Created dir: C:\Documents and Settings\prova\build\classes
Compiling 1 source file to C:\Documents and Settings\prova\build\classes
compile-single:
run-single:
Exception in thread "main" java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.j ava:182)
at java.util.StringTokenizer.<init>(StringTokenizer.j ava:204)
at prova.main(prova.java:32)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
Presumo sia un errore dovuto al fatto che non so come gestire le eccezioni, ma di preciso cos'ho combinato?