In der java console beim austesten in html, wird eine null pointer exception der methode parse(), die von init() aufgerufen wird, gemeldet. Meinst du das so?
Sicherheitshalber das ganze programm:
Die Datei "tanach.txt" sieht so aus und bis auf die erste zeile sind alle zeilen gleich strukturiert.
Woran liegts? Außerdem gefällt mir nicht, das array book für alel fälle mit einer component zu initialiseren, für den fall, das keine datei gefunden wird. die rückgabe von 0 oder -1 im problemfall wäre besser, wenn das irgend möglich ist. aber die exception passiert irgendwo anders in der methode, denn die datei existiert, bzw wurde schonmal gefunden.
Sicherheitshalber das ganze programm:
Code:
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;
public class hitomi extends Applet {
// ascii to gematria value matrix starting with $20 SPACE
// michigan claremont style
// ABGDHVZX+YKLMN$OPCQRSTkmnpc
// special shins no special value: s = 300, # = 300
private final static int[][] gematrix = {{
// lettermatrix A
0,0,0,300,50,0,0,0, 0,0,0,9,0,0,0,0,
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
0,1,2,90,4,0,0,3, 5,0,0,20,30,40,50,70,
80,100,200,300,400,0,6,0, 8,10,7,0,0,0,0,0,
0,0,0,90,0,0,0,0, 0,0,0,20,0,40,50,0,
80,0,0,300,0,0,0,0, 0,0,0,0,0,0,0,0
}, {
// lettermatrix B, end letters have high values
0,0,0,300,60,0,0,0, 0,0,0,9,0,0,0,0,
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
0,1,2,90,4,0,0,3, 5,0,0,20,30,40,50,70,
80,100,200,300,400,0,6,0, 8,10,7,0,0,0,0,0,
0,0,0,900,0,0,0,0, 0,0,0,500,0,600,700,0,
800,0,0,300,0,0,0,0, 0,0,0,0,0,0,0,0
}};
private class word {//word objekt
public int[] sum = new int[1];//sums by gematrix A and B
public String w;//the word
//public int nr;//word number in the verse by index
//public word(String w, int sumA, int sumB) {
// this.w = w;
// this.sum[0] = sumA;
// this.sum[1] = sumB;
//}
}
//verse object baut auf array aus word objekten auf
private class verse {
public int chap, rel; //relative verse number
public String id; //e.g. 'GEN' or 'EXO' etc
public word[] w;//words
//constructor
//public verse() {
//this.nr = nr;//by index
//this.id = id;
//}
}
verse[] thisbook;
public void init() {
setBackground(Color.white);
//later read once at first user plot request or display 666 as default
//with vector: isEmpty(); flush with removeAll();
//with hastable can store multiple parsed texts in mem, each is an array of
//verse objects, for hashtable see [url]http://www.rgagnon.com/howto.html[/url]
thisbook = parse("tanach.txt");
}
public void paint(Graphics g) {
// Get the width and height of the Applet
int aw = this.getSize().width;
int ah = this.getSize().height;
int as; // pixel diameter of the full wheel
if (aw<ah) {as = aw;} else {as = ah;}
// set up hitomi function parameters
g.setColor(Color.orange);
g.drawOval(0,0,as,as);
g.drawString("Value: " + thisbook[0].w[0].sum[0], 10,10);
}
private verse[] parse(String f){
// read claremont textfile by filename and parse contents into a verse[] and return that
verse[] book = new verse[0];
try {
URL u = new URL(getCodeBase(), f);
//later try FileReader in place of InputStreamReader
BufferedReader br = new BufferedReader (new InputStreamReader(u.openStream()));
int v=0, m=0;
String l = "";
l = br.readLine();
if (l.startsWith("INIT")) {
//read number of verses
m = Integer.parseInt(l.substring(4,6));//max words in verse
v = Integer.parseInt(l.substring(6,10));//verses in text
} else {
// search size and go back to file start or even save checked file
m = 50; v = 5855; //currently fall back to dummy safe values only
}
book = new verse[v-1]; //method returns this
v=0;
int w=0, i=0, j=0, c=0;//word number, index in line, j index of next space or end
String wd="";
byte[] b;
while(null != (l = br.readLine())) {
//calc and store this verse values
book[v].id = l.substring(0, 3);//store tag GEN or EXO etc
book[v].chap = Integer.parseInt(l.substring(4,7));//chapter number
book[v].rel = Integer.parseInt(l.substring(8,11));//verse number
i=12;//verse words start at index 12 in the line of text
book[v].w = new word[m];
//word loop
while (i<l.length()) {
if (-1 == (j = l.indexOf(" ",i))) {
j = l.indexOf(".",i);
}
wd = l.substring(i,j);
book[v].w[w].w = wd;
b = wd.getBytes();
for (c = 0; i<j; c++) {
//use ascii code
book[v].w[w].sum[0] += gematrix[0][b[c]-32];
book[v].w[w].sum[1] += gematrix[1][b[c]-32];
i++;
}
i++; w++;
}
}
br.close();
}
catch(IOException e) {
//Graphics g = this.getGraphics();
//g.drawString(e.getMessage, 0, 0);
}
return book;
}
}
//end
Die Datei "tanach.txt" sieht so aus und bis auf die erste zeile sind alle zeilen gleich strukturiert.
Code:
INIT265852
GEN 001:001 BRASYT BRA ALHYm AT HSMYm VAT HARc.
GEN 001:002 VHARc HYTH THV VBHV VXSk OL-PNY THVm VRVX ALHYm MRXPT OL-PNY HMYm.
GEN 001:003 VYAMR ALHYm YHY AVR VYHY-AVR.
GEN 001:004 VYRA ALHYm AT-HAVR KY-+VB VYBDL ALHYm BYn HAVR VBYn HXSk.
GEN 001:005 VYQRA ALHYm LAVR YVm VLXSk QRA LYLH VYHY-ORB VYHY-BQR YVm AXD.
GEN 001:006 VYAMR ALHYm YHY RQYO BTVk HMYm VYHY MBDYL BYn MYm LMYm.
GEN 001:007 VYOs ALHYm AT-HRQYO VYBDL BYn HMYm ASR MTXT LRQYO VBYn HMYm ASR MOL
LRQYO VYHY-Kn.
...
Woran liegts? Außerdem gefällt mir nicht, das array book für alel fälle mit einer component zu initialiseren, für den fall, das keine datei gefunden wird. die rückgabe von 0 oder -1 im problemfall wäre besser, wenn das irgend möglich ist. aber die exception passiert irgendwo anders in der methode, denn die datei existiert, bzw wurde schonmal gefunden.