ファイルから単語を読み取り、ダブルリンクリストに入れます.equals()メソッドを使用して、単語がダブルリンクリストにあるかどうかを確認し、メソッドは正常に機能しましたが、compareto()メソッドを使用して単語を並べ替えますこれらの例外が表示されます:-
Exception in thread "main" java.lang.NullPointerException
at DoubleLinkedList.insert(DoubleLinkedList.java:49)
at FileSystemIndexer.indexFile(FileSystemIndexer.java:88)
at FileSystemIndexer.IndexDirectory(FileSystemIndexer.java:18)
at TestPhase1.main(TestPhase1.java:15)
メソッド:-
import java.io.*;
import java.util.*;
public class FileSystemIndexer {
private DoubleLinkedList<Index> invertedIndex;
public FileSystemIndexer (){
invertedIndex=new DoubleLinkedList<Index>();
}
public void IndexDirectory(File dir){
File[] files =dir.listFiles();
for(int i=0 ; i<files.length; i++){
indexFile(files[i]); //FileSystemIndexer.java:18
}
}
public void indexFile(File file ){
Scanner input = null;
try{
input = new Scanner(file);
}
catch(Exception e) {
System.out.println(e);
}
String c;
input.useDelimiter(",|\\.|:|\\s+");
while(input.hasNext()) {
c=input.next();
if(c.equals("")){
c=input.next();
}else{
//System.out.println("here: " + c);
if(invertedIndex.empty()){
invertedIndex.insert( new Index(c,file));
}else{
invertedIndex.FindFirst();
while(!invertedIndex.last()){
if(invertedIndex.retrieve().getWord().compareTo(c)==-1){
System.out.println("here: " + c);
invertedIndex.FindPrevious();
invertedIndex.insert(new Index(c,file));
}else if(invertedIndex.retrieve().getWord().compareTo(c)==1){
System.out.println("here: " + c);
invertedIndex.findNext();
invertedIndex.insert(new Index(c,file)); //FileSystemIndexer.java:88
}
else if(invertedIndex.retrieve().getWord().compareTo(c)==0){
System.out.println("here: " + c);
invertedIndex.retrieve().getFiles().insert(file);
}
/*
if(invertedIndex.retrieve().getWord().equals(c)){
invertedIndex.retrieve().getFiles().insert(file);
}
invertedIndex.findNext();
}
if(invertedIndex.retrieve().getWord().equals(c)){
invertedIndex.retrieve().getFiles().insert(file);
}else {
Index i=new Index(c,file);
invertedIndex.insert(i);
}
}/**/
} if(invertedIndex.retrieve().getWord().compareTo(c)==-1){
invertedIndex.FindPrevious();
invertedIndex.insert(new Index(c,file));
}else if(invertedIndex.retrieve().getWord().compareTo(c)==1){
invertedIndex.findNext();
invertedIndex.insert(new Index(c,file));
}
else if(invertedIndex.retrieve().getWord().compareTo(c)==0){
invertedIndex.retrieve().getFiles().insert(file);
}
}
}
}
input.close();
//}
//catch (Exception e){
// System.out.println(e);
//}
}
ダブルリンクリストの挿入方法:-
public void insert(T val ){
Node<T> tmp= new Node<T> (val);
if(empty()){
current=head=tmp;
}else{
tmp.next=current.next; //DoubleLinkedList.java:49
tmp.previous=current;
if(current.next !=null)
current.next.previous=tmp;
current.next=tmp;
current=tmp;
}
}
二重リンクリストの私の実装:
public class DoubleLinkedList<T> {
private Node<T> head;
private Node<T> current ;
public DoubleLinkedList(){
head=current=null;
}
public boolean empty(){
return head==null;
}
public boolean last(){
return current.next==null;
}
public boolean first(){
return current.previous==null;
}
public boolean full(){
return false ;
}
public void FindFirst(){
current=head;
}
public void findNext(){
current=current.next;
}
public void FindPrevious(){
current=current.previous;
}
public T retrieve(){
return current.data;
}
public void update(T val ){
current.data=val;
}
public void insert(T val ){
Node<T> tmp= new Node<T> (val);
if(empty()){
current=head=tmp;
}else{
tmp.next=current.next;
tmp.previous=current;
if(current.next !=null)
current.next.previous=tmp;
current.next=tmp;
current=tmp;
}/*
if(empty()){
current=head=tmp;
}else{
tmp.previous = current;
tmp.next = current.next;
if(current.next != null)
current.next.previous = tmp;
current.next = tmp;
}*/
}
public void remove(){
if(current==head){
head=head.next;
if(head!=null)
head.previous=null;
}else{
current.previous.next=current.next;
if(current.next !=null)
current.next.previous=current.previous;
}
if(current.next==null)
current=head;
else
current=current.next ;
}
public void remove2(){
if(current==head)
head=head.next;
else
current.previous.next=current.next;
if(current.next !=null)
current.next.previous=current.previous;
if(current.next==null)
current=head;
else
current=current.next;
}
}