日期:2014-05-20  浏览次数:20985 次

救急啊!!!!!!!!!!!!!!!!!!!! (via TranXcode)

import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import weka.attributeSelection.AttributeSelection;
import weka.attributeSelection.CfsSubsetEval;
import weka.attributeSelection.GreedyStepwise;
import weka.core.Instances;
import weka.core.Utils;
import weka.core.converters.ArffLoader;
import weka.gui.*;
import weka.gui.explorer.ClustererPanel;
import weka.gui.explorer.Explorer;
import weka.gui.explorer.Explorer.CapabilitiesFilterChangeEvent;
import weka.gui.visualize.AttributePanel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class ClusterAttrbuteGUI extends JFrame implements ActionListener{

/**
* @param args
* @throws Exception 
*/

JLabel numOfClusterLabel = null;
JTextField numOfClusterField = null;
JButton remove = null;
String numOfCluster = null;
AttributeSelectionPanel attrSelectPanel = null;

public ClusterAttrbuteGUI() throws IOException{

super();
setSize(350,300);
initGUI();
  setVisible(true);
   
}

private void initGUI(){
try{

Container container = getContentPane();

numOfClusterLabel = new JLabel("请输入聚类数");
container.add(numOfClusterLabel);
numOfClusterField = new JTextField(20);
container.add(numOfClusterField);
AttributeSelectionPanel attrSelectPanel = new AttributeSelectionPanel(true,true,true,false);
attrSelectPanel.setInstances(Cluster.instances);
container.add(attrSelectPanel);
remove = new JButton("确定");
remove.addActionListener(this);
container.add(remove);

container.setLayout(new FlowLayout());

}catch(Exception e){
JOptionPane.showMessageDialog(null,e.toString());
}
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == remove){
int selectedAttr[] = attrSelectPanel.getSelectedAttributes();
for(int i=0; i<selectedAttr.length; i++){
Cluster.instances.remove(selectedAttr[i]);
}
Cluster.numOfClusters = Integer.parseInt(numOfClusterField.getText());
try {
//Cluster cluster = new Cluster();
if(e.getSource() == remove){
for(int i=0; i<Classify.instances.numInstances(); i++)
System.out.println(Classify.instances.instance(i));
}
} catch (Exception e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null,e1.toString());
}


}
}






public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
  AttributeGUI gui = new AttributeGUI("E://hotel_model.arff");
}



}
在actionperformed函数里面int selectedAttr[] = attrSelectPanel.getSelectedAttributes()总是报错
我检查过,是attrSelectPanel为空指针
但是为什么啊?????
我在构造函数里面有new过了
P.S:attrSelectaPanel是继承JPanel类型




------解决方案--------------------
你定义了两个AttributeSelectionPanel attrSelectPanel哦,一个是在成员变量中定义的,那个赋值null,还有一个就是初始化界面时候定义的,这个有生成对象,不过是局部标量它的生存周期只有在方法体里。所以你在后面是用的attrSelectPanel是成员变量,依然还是null.