public boolean test(Example testExample)
{
// calculate posterior probs 后验概率
double [] posteriorProbs = calculateProbs(testExample);
// predicted class
int predictedClass = argMax(posteriorProbs);
int freqnum[]=new int[numCategories];//计算词频
System.out.print("Document: " + testExample.name + "\nResults: ");
StringBuffer src=null;
File f=testExample.getDocument().file;
try{
BufferedReader in=new BufferedReader(new FileReader(f));
String str=new String();
while((str=in.readLine())!=null)
src=src.append(str);////报错处 !!!
in.close();
}
catch(IOException e){
e.printStackTrace();
} for (int j=0; j<numCategories; j++)
{
System.out.print(cat[j] + "(" + posteriorProbs[j] + ") ");
freqnum[j]=StringUtils.countMatches(src.toString(),cat[j]);
System.out.println("frequent num="+freqnum[j]);
}
System.out.println("Predicted class: " +cat[predictedClass] + "\n");
//System.out.println("Predicted class: " +cat[predictedClass] + "\n");
if (debug) {
System.out.print("Document: " + testExample.name + "\nResults: ");
for (int j=0; j<numCategories; j++) {
System.out.print(cat[j] + "(" + posteriorProbs[j] + ")\t");
}
System.out.println("\nCorrect class: " + testExample.getCategory() + ", Predicted class: " + predictedClass + "\n");
}
return (predictedClass == testExample.getCategory()); }
**************************************************************
报错 :java.lang.NullPointerException at ir.classifiers.NaiveBayes.test(NaiveBayes.java:131) at ir.classifiers.TestNaiveBayes.main(TestNaiveBayes.java:34)如下: while(i<examples.size()){
Example e =(Example)examples.get(i);
BC.test(e);//BC是NaiveBayes类
i++;
}
大虾们,帮帮忙
{
// calculate posterior probs 后验概率
double [] posteriorProbs = calculateProbs(testExample);
// predicted class
int predictedClass = argMax(posteriorProbs);
int freqnum[]=new int[numCategories];//计算词频
System.out.print("Document: " + testExample.name + "\nResults: ");
StringBuffer src=null;
File f=testExample.getDocument().file;
try{
BufferedReader in=new BufferedReader(new FileReader(f));
String str=new String();
while((str=in.readLine())!=null)
src=src.append(str);////报错处 !!!
in.close();
}
catch(IOException e){
e.printStackTrace();
} for (int j=0; j<numCategories; j++)
{
System.out.print(cat[j] + "(" + posteriorProbs[j] + ") ");
freqnum[j]=StringUtils.countMatches(src.toString(),cat[j]);
System.out.println("frequent num="+freqnum[j]);
}
System.out.println("Predicted class: " +cat[predictedClass] + "\n");
//System.out.println("Predicted class: " +cat[predictedClass] + "\n");
if (debug) {
System.out.print("Document: " + testExample.name + "\nResults: ");
for (int j=0; j<numCategories; j++) {
System.out.print(cat[j] + "(" + posteriorProbs[j] + ")\t");
}
System.out.println("\nCorrect class: " + testExample.getCategory() + ", Predicted class: " + predictedClass + "\n");
}
return (predictedClass == testExample.getCategory()); }
**************************************************************
报错 :java.lang.NullPointerException at ir.classifiers.NaiveBayes.test(NaiveBayes.java:131) at ir.classifiers.TestNaiveBayes.main(TestNaiveBayes.java:34)如下: while(i<examples.size()){
Example e =(Example)examples.get(i);
BC.test(e);//BC是NaiveBayes类
i++;
}
大虾们,帮帮忙
解决方案 »
- windows下安装的cygwin 如何运行java程序???? 急!!!!!!!
- 难道说java泛型不支持基础类型数据???
- 请问怎么创建main()方法。主方法接收键盘输入数据
- socket服务器端收发来的字符串时有的收不到.为什么?
- 在Eclipse里输入super()时怎么不不提示“构造函数”相关参数?
- 还是问一下这个问题吧,说不定有好办法
- 求<java语言与XML处理教程-SAX...>源码
- 如何截获键盘消息啊,大家帮我看看这个简单的程序
- 低级问题。知道文件路径和文件名,如何用默认的打开方式打开该文件。
- 如何得到
- 关于System.in.read的问题,请指教!!谢谢!!
- 用JAVA如何读取系统服务的状态
这里错了 src没有实例化,它是null 因此下面的src也就无法append!
src=src.append(str);////报错处 !!!
in.close();
}
in.close()应该放在循环外面,
in.close后,in.readLine()就会报错
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
class myButton extends JButton
{
int ;
myButton()
{
=(int)(Math.floor(Math.random()+0.3)); }
}
class shengchengmigong extends JPanel
{
static myButton []mybutton;
shengchengmigong()
{
this.setLayout(new GridLayout(10,10,0,0));
myButton []mybutton=new myButton[144];
for(int i=0;i<=99;i++)
{
mybutton[i]=new myButton();
}
int i=0;
while(i<=99)
{
if(i==88||i==11)
{}
else if(i<=9||i>=90)
{mybutton[i].setBackground(Color.black);}
else if(i%10==0)
{mybutton[i].setBackground(Color.black);}
else if((i+1)%10==0)
{mybutton[i].setBackground(Color.black);}
else if(mybutton[i].==1)
{mybutton[i].setBackground(Color.black);}
else
{}
this.add(mybutton[i]);
i++;
}
}
}
class mymigong implements ActionListener
{
JButton sc;
JButton qj;
JFrame f;
shengchengmigong mymigong;
mymigong()
{
sc=new JButton("生成迷宫");
qj=new JButton("求解");
sc.addActionListener(this);
qj.addActionListener(this);
JPanel jp=new JPanel();
jp.setLayout(new FlowLayout(FlowLayout.CENTER));
jp.add(sc); jp.add(qj);
f=new JFrame("小小迷宫");
f.setBounds(0,0,500,550);
f.setLayout(new BorderLayout());
shengchengmigong mymigong=new shengchengmigong();
f.add(mymigong,BorderLayout.CENTER);
f.add(jp,BorderLayout.NORTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==sc)
{
mymigong=new shengchengmigong();
f.add(mymigong,BorderLayout.CENTER);
}
else if(e.getSource()==qj)
{
ArrayList<myButton> arraylist=new ArrayList<myButton>(1);
try{
if(arraylist.add(shengchengmigong.mybutton[11]));System.out.println("true");
shengchengmigong.mybutton[11].setBackground(Color.red);
}
catch(Exception ee)
{System.out.println(ee.toString());} }
}
}public class migong
{
public static void main(String args[])
{
new mymigong();
}
}