我正在使用Swing。我想抓住用户选择的文件,并在用户按下计数按钮时使用它们来计算某些内容。然而,当我按下按钮时,即使我选择文件也有空指针。如何使其有效?
class RefractiveGUIFrame extends JFrame
{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
private File[] dataFiles;
private Object[][] refractiveIndex = null;
public RefractiveGUIFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocationByPlatform(true);
final JPanel northPanel = new JPanel();
final JPanel westPanel = new JPanel();
northPanel.setLayout(new GridLayout(2,3));
westPanel.setLayout(new GridLayout(3,1));
//create components northPanel
final JButton fileBrowseButton = new JButton("Browse..");
final JTextField fileTextField = new JTextField(100);
final JButton directoryBrowseButton = new JButton("Browse..");
final JTextField directoryTextField = new JTextField(100);
final JRadioButton fileChooser = new JRadioButton ("Choose File..",false);
final JRadioButton directoryChooser = new JRadioButton ("Choose Directory..",true);
//resposible for turning off the previously set button
ButtonGroup group = new ButtonGroup();
group.add(fileChooser);
group.add(directoryChooser);
//set browse button off
fileBrowseButton.setEnabled(false);
directoryBrowseButton.setEnabled(true);
//create components westPanel
final JButton countButton = new JButton ("Count");
final JButton displayButton = new JButton ("Display");
final JButton saveButton = new JButton ("Save");
//add components northPanel
northPanel.add(directoryChooser);
northPanel.add(fileTextField);
northPanel.add(directoryBrowseButton);
northPanel.add(fileChooser);
northPanel.add(directoryTextField);
northPanel.add(fileBrowseButton);
//add components westPanel
westPanel.add(countButton);
westPanel.add(displayButton);
westPanel.add(saveButton);
//add panels to frame
this.add(northPanel, BorderLayout.NORTH);
this.add(westPanel, BorderLayout.WEST);
//create file dialog that will be used in listeners
final JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File ("."));
//add action listeners as anonymous innner classes
fileChooser.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileBrowseButton.setEnabled(true);
directoryBrowseButton.setEnabled(false);
}
});
directoryChooser.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileBrowseButton.setEnabled(false);
directoryBrowseButton.setEnabled(true);
}
});
fileBrowseButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = chooser.showOpenDialog(northPanel);
dataFiles = chooser.getSelectedFiles();
String filename = chooser.getSelectedFile().getPath();
fileTextField.setText(filename);
}
});
directoryBrowseButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(northPanel);
String filename = chooser.getSelectedFile().toString();
fileTextField.setText(filename);
dataFiles = chooser.getSelectedFile().listFiles();
for (File f : dataFiles)
System.out.println(f);
}
});
countButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try{
for (int i =0; i<dataFiles.length;i++)
{
RefractiveIndex ri = new RefractiveIndex(dataFiles[i]);
refractiveIndex[i][i] = ri;
System.out.println("Refractive index value : " + ri.rii.toString());
}
}
catch (Exception e1) {
e1.printStackTrace();
}
}
}); //end countButton actionListener
}
}
我用以下代码编辑我的代码:
chooser.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
JFileChooser chooser = (JFileChooser) evt.getSource();
dataFiles = (File[])evt.getNewValue();
// Get list of selected files
dataFiles = chooser.getSelectedFile().listFiles();
// for (File f : dataFiles)
// System.out.println(f);
}
}
});
现在我有一个错误
java.lang.NullPointerException
at com.refractiveindex.RefractiveGUIFrame$6.actionPerformed(RefractiveGUITest.java:230)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我认为这是RefractiveIndex类中的错误。您认为相同吗?我想调试它,但我不知道怎么进入类并在RefractiveIndex类中调试。
我添加了我的RefractiveIndex类。也许它会有所帮助,因为我不知道如何解决它。实际上,当我运行它时,它打印System.out.println(entry.getKey())
行,但后来我不知道它停在哪里以及为什么停止。
public class RefractiveIndex {
public static Double sumRaMw = 0.0;
public static Double sumMw = 0.0;
public static Double sumPsvMw = 0.0;
public static Double rii;
File f = new File ("DATA.txt");
//public static void main(String[] args) throws Exception {
//String f = "DATA.txt";
//String fasta = "3LWK.fasta.txt";
//HashMap<String,ArrayList<Double>> myMap = readDataFromTxt(f);
//System.out.println(myMap.keySet());
//System.out.println(myMap.values());
//System.out.println(myMap.get("PHE"));
//ArrayList<ProteinSequence> seq = new ArrayList<ProteinSequence>();
//seq = getProteinSequenceFromFasta(fasta);
//Double refractive = calculateRefractive(myMap, seq);
//System.out.println(refractive.toString());
//}
public RefractiveIndex(File fastaFile) throws Exception{
HashMap<String,ArrayList<Double>> DataMap = readDataFromTxt(f);
//HashMap<String,ArrayList<Double>> myMap = readDataFromTxt(fastaFile);
ArrayList<ProteinSequence> seq = new ArrayList<ProteinSequence>();
seq = getProteinSequenceFromFasta(fastaFile.getAbsolutePath());
Double refractive = calculateRefractive(DataMap, seq);
}
public static HashMap<String,ArrayList<Double>> readDataFromTxt (File fileName) throws NumberFormatException, IOException
{
HashMap<String, ArrayList<Double>> map = new HashMap<String, ArrayList<Double>>();
FileReader fr = new FileReader(fileName); //read characters
BufferedReader br = new BufferedReader (fr); //read line
String line;
while ((line = br.readLine())!=null)
{
ArrayList<Double> values = new ArrayList<Double>();
String parts[] = line.split("\t");
values.add(Double.parseDouble(parts[3]));
values.add(Double.parseDouble(parts[4]));
values.add(Double.parseDouble(parts[2]));
String key = parts[1];
map.put(key, values);
}
return map;
}
public static Double calculateRefractive (HashMap<String, ArrayList<Double>> hm, ArrayList<ProteinSequence> seq)
{
for (ProteinSequence s : seq){
List<AminoAcidCompound> compound = s.getAsList();
for (int i =0;i<compound.size();i++){
ArrayList<Double> temp = hm.get(compound.get(i).toString());
sumMw += temp.get(0);
//System.out.println(sumMw.toString());
sumRaMw += temp.get(1)*temp.get(0);
//System.out.println(temp.get(2));
sumPsvMw +=temp.get(2)*temp.get(0);
}
}
Double rp = sumRaMw/sumMw;
Double vp = sumPsvMw/sumMw;
Double np = Math.sqrt((2 * rp + vp)/(vp - rp));
Double no = 1.3340 + (1.3325 - 1.3330);
rii = 1.5 * vp * no * (np * np - no * no)/(np * np + 2 * no * no);
return rii;
}
public static ArrayList<ProteinSequence> getProteinSequenceFromFasta(String file) throws Exception{
LinkedHashMap<String, ProteinSequence> a = FastaReaderHelper.readFastaProteinSequence(new File(file));
ArrayList<ProteinSequence> sequence = new ArrayList<ProteinSequence>(a.values());
for (Map.Entry<String, ProteinSequence> entry : a.entrySet()){
System.out.println(entry.getKey());
}
return sequence;
}
}
答案 0 :(得分:0)
在我点击浏览按钮并且还没有选定文件时,directoryBrowseButton
的{{1}}会被触发。然后,当您触发计数按钮时,ActionListener
为空。
您应该收听处理文件浏览器中项目选择的事件,并将它们分配给dataFiles。
dataFiles