java代码写的过程额米有报错运行的时候报错了
有代码与错误提示写的过程中是没有报错的错误提示:
Exception in thread "main" java.lang.NullPointerException
at zuoye.AprioriAlgorithm.findF1Item(AprioriAlgorithm.java:83)
at zuoye.AprioriAlgorithm.main(AprioriAlgorithm.java:25)
package zuoye;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
public class AprioriAlgorithm {
private int minSup;
private int position;
private static List<String>date;
private static List<Set<String>>dataSet;
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
AprioriAlgorithm apriori = new AprioriAlgorithm();
date = apriori.builDate();
apriori.setMinSup1(2);
date = apriori.builDate();
List<Set<String>>f1Set = apriori.findF1Item(date);
apriori.printSet(f1Set,1);
List<Set<String>>result = f1Set;
int i = 2;
do {
result = apriori.arioriGen(result);
apriori.printSet(result, 1);
i++;
}while(result.size() != 0);
long endTime = System.currentTimeMillis();
System.out.println("共用时:"+(endTime - startTime) + "ms");
}
public void setMinSup1(int minSup) {
this.minSup = minSup;
}
List<String> builDate(String...fileName){
List<String>data = new ArrayList<String>();
if(fileName.length != 0) {
File file = new File(fileName);
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while((line = reader.readLine()) !=null) {
date.add(line);
}
}catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else {
data.add("I1 I2 I5");
data.add("I2 I4");
data.add("I2 I3");
data.add("I1 I2 T4");
data.add("I1 I3");
data.add("I2 I3");
data.add("I1 I3");
data.add("I1 I2 I3 I5");
data.add("I1 I2 I3");
}
dataSet = new ArrayList<Set<String>>();
Set<String>dSet;
for(String d : data) {
dSet = new TreeSet<String>();
String[] dArr = d.split("");
for(String str : dArr) {
dSet.add(str);
}
}
return date;
}
List<Set<String>>findF1Item(List<String>data){
List<Set<String>>result = new ArrayList<Set<String>>();
Map<String,Integer> dc = new HashMap<String,Integer>();
for(String d : data) {
String[] items = d.split("");
for(String item : items) {
if(dc.containsKey(item)) {
dc.put(item, dc.get(item)+1);
}else {
dc.put(item, 1);
}
}
}
Set<String>itemKeys = dc.keySet();
Set<String>tempKeys = new TreeSet<String>();
for(String str : itemKeys) {
tempKeys.add(str);
}
for(String item : tempKeys) {
if(dc.get(item)>= minSup) {
Set<String>f1Set = new TreeSet<String>();
f1Set.add(item);
result.add(f1Set);
}
}
return result;
}
List<Set<String>>arioriGen(List<Set<String>>preSet){
List<Set<String>>result = new ArrayList<Set<String>>();
int preSetSize = preSet.size();
for(int i = 0;i<preSetSize-1;i++) {
for(int j = i+1;j<preSetSize;j++) {
String[] strA1 = preSet.get(i).toArray(new String);
String[] strA2 = preSet.get(j).toArray(new String);
if(isCanLink(strA1,strA2)) {
Set<String>set = new TreeSet<String>();
for(String str : strA1) {
set.add(str);
}
set.add((String) strA2);
if(!isNeedCut(preSet,set)) {
result.add(set);
}
}
}
}
return chckSupport(result);
}
List<Set<String>>chckSupport(List<Set<String>>setList){
List<Set<String>>result = new ArrayList<Set<String>>();
boolean flag = true;
int [] counter = new int ;
for(int i =0;i<setList.size();i++) {
for(Set<String>dSets : dataSet) {
if(setList.get(i).size()>dSets.size()) {
flag = true;
}else {
for(String str : setList.get(i)){
if(!dSets.contains(str)) {
flag = false;
break;
}
}
if(flag) {
counter += 1;
}else {
flag = true;
}
}
}
}
for(int i = 0;i<setList.size();i++) {
if(counter>= minSup) {
result.add(setList.get(i));
}
}
return result;
}
boolean isCanLink(String[] s1,String[] s2) {
boolean flag = true;
if(s1.length == s2.length) {
for(int i = 0;i<s1.length-1;i++) {
if(!s1.equals(s2)) {
flag = false;
break;
}
}
if(s1.equals(s2.length-1)) {
flag = false;
}
}else{
flag = true;
}
return flag;
}
boolean isNeedCut(List<Set<String>> setList, Set<String> set) {
boolean flag = false;
List<Set<String>>subSets = getSubset(set);
for(Set<String>subSet : subSets) {
if(!isContained(setList,subSet)) {
flag = true;
break;
}
}
return flag;
}
private boolean isContained(List<Set<String>> setList, Set<String> set) {
boolean flag = false;
int psoition = 0;
for(Set<String>s : setList) {
String[] sArr = s.toArray(new String);
String[] setArr = set.toArray(new String);
for(int i = 0;i<sArr.length;i++) {
if(sArr.equals(setArr)) {
position = i;
}else {
break;
}
}
if(position == sArr.length-1) {
flag = true;
position = 0;
}
}
return flag;
}
private List<Set<String>> getSubset(Set<String> set) {
List<Set<String>>result = new ArrayList<Set<String>>();
String[] setArr = set.toArray(new String);
for(int i = 0;i<setArr.length;i++) {
Set<String>subSet = new TreeSet<String>();
for(int j = 0;j<setArr.length;j++) {
if(i!=j) {
subSet.add((String) setArr);
}
}
result.add(subSet);
}
return null;
}
void printSet(List<Set<String>> setList, int i) {
System.out.println("频繁"+i+"项集:共"+setList.size()+"项:{");
for(Set<String>set : setList) {
System.out.print("[");
for(String str : set) {
System.out.print(str+"");
}
System.out.print("],");
}
System.out.println("}");
}
}
标题打错了是:
Java代码写的时候没有报错运行的时候却又错误
求大佬帮忙看看这代码又什么错误 空指针异常是运行时异常 83行data可能为空 第17行改成:
private static List<String> date = new ArrayList<>();
你这应该是只定义了引用类型,但没创建对象
页:
[1]