Comparable

Snake87

Mitglied
Hallo ich soll von meinen Prof. aus, ein Programm schreiben was die Worthäufigkeit berechnen.
Also wie oft kommt ein Wort vor in einen Text.

Erstmal ich bekomme folgende Fehlermeldung:

Code:
java.lang.NullPointerException
	at java.lang.String.compareTo(Unknown Source)
	at AllWords.register(AllWords.java:20)
	at wc.main(wc.java:19)

Java:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

// wc.java
public class wc {
public static void main(String[] s) {
  AllWords words = new AllWords(10000);// reicht aus für den gegebenen Text lächelnd

  
  File file = new File("faust.txt");
  Scanner scanner;
try {
 scanner = new Scanner(file);

 while(scanner.hasNext() == true ) {
      //System.out.println(scanner.next() );
      String t = scanner.next();
      words.register(t);
    }

} catch (FileNotFoundException e) {
 System.out.println("Datei nicht gefunden");// TODO Auto-generated catch block
 e.printStackTrace();
}

}
}


Java:
public class Word implements Comparable<Word> {

private String content; // das Wort als Zeichenkette
private int n; // die Anzahl des Auftretens dieses Wortes im Text


public String getContent(){
  return content;
}

public Word(String s){
s = this. content;         // s als content übernehmen,
n = 1;                    // zähler auf 1 setzen (erstes Auftreten)
}

public int frequency() { return n; }

public void inc(){
  this.n++;                // erhöhe Zähler für dieses Wort um 1
}

public int compareTo(Word w){
 
//String implementiert Comparable, daher kann man auf diese Methode drauf zugreifen und verwenden  
  return this.getContent().compareTo(w.getContent());

}

public String toString(){
 return this.getContent()+" : "+this.n;
// liefert für dieses Wort die Zeichenkette: "Häufigkeit : Wort"
}

}



Java:
public class AllWords {

private Word words []; // das Feld, in dem alle Wörter erfasst werden sollen
private int all = 0; // Index der nächsten freien Position in words[]
int max;

public AllWords (int n)
{
max = n;// Platz für maximal max Wörter im Feld
words = new Word[n]; //initalieren des arrays
}

public void register(String s)
{
    //Vergleiche ob Worrt bereits vorhanden ist
    for (int i = 0; i < this.all; i++) {
      //wenn dies der Fall ist Zähler um 1 erhöhen
      if(s.compareTo(words[i].getContent() ) == 0) {
        words[i].inc();
        //springe aus der Methode heraus und führe nachfolgenden Code nicht mehr aus
        return;
      }
  }
  
    if(all < max) {
      words[all] = new Word(s);
      all++;
     }
    else{
      System.out.println("Kein Platz mehr vorhanden");
      System.exit(-1);
    }
  }
}
 
Alle deine Wörter im Array words sind null. Wenn du darauf getContent() aufrufst --> NPE

EDIT:
dies ist falsch.

Der content selber ist null im word. dann knallts. siehe post 4
 
Zuletzt bearbeitet:
Hallo Snake87!

Du greifst in der Methode Register auf ein leeres Feld zu, ergo die NullPointerException.
Du solltest vorher abfragen ob in dem Feld schon was drinnen ist.
z.B.:
Java:
If(words[i] != null) {
    if(s.compareTo(words[i].getContent() ) == 0) {
lg
 
ach ja..
verursacht durch:
Java:
public Word(String s){
s = this. content;         //<---------------------- content immer null
n = 1;
}
 
Danke euch, denn Fehler hatte nie alleine gefunden. Obwohl er doch eigentlich recht trival ist.😳

Habe einfach mal die Seiten vertauscht, also statt:

Code:
s = this. content;

Habe ich nun geschrieben:

Code:
this.content = s

Ja wenn man den eigentlichen Inhalt immer überschreibt brauch man sich nicht wundern.

Danke nochmal.
 

Zurück
Oben