gpt4 book ai didi

java - JFileChooser 产生错误

转载 作者:行者123 更新时间:2023-11-29 04:37:39 26 4
gpt4 key购买 nike

我希望我的问题是可以理解的,我希望这一节是正确的。我必须构建一个从 Apache 日志文件格式到 IIS 格式的转换器,并且我构建了一个从命令行运行的系统。我刚刚添加了一个 GUI,这是我第一次使用 JFileChooser。问题从那里开始:我收到一个奇怪的错误。这是产生错误的代码:

public class HTTPiis extends JPanel{

public HTTPiis() {
setLayout(new FlowLayout());

JButton selStart = new JButton("Apri");
JButton selDest = new JButton("Seleziona destinazione");
JButton converti = new JButton("Converti");

add(selStart);
add(selDest);
add(converti);

selStart.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
final JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(new JPanel());

String source = chooser.getSelectedFile().getAbsolutePath();
FileHandler fh = new HTTPtoIIS();
fh.setFilePath(source);
HTTPtoIIS h = new HTTPtoIIS();
h.convert();
}
});
}

}这是错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.File.<init>(File.java:277)
at tesi.FileLoader.load(FileLoader.java:17)
at tesi.HTTPLogHandler.parse(HTTPLogHandler.java:17)
at tesi.HTTPtoIIS.convert(HTTPtoIIS.java:49)
at grafica.HTTPiis$1.actionPerformed(HTTPiis.java:41)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

你能解释一下为什么我错了吗?如果您需要更多代码,请问我!谢谢!!

最佳答案

为什么要向文件选择器添加一个空面板?

对我来说,这个错误看起来像是您的转换器代码的问题。你也是:

  1. 是否进行任何基本调试?

  2. 显示从文件选择器返回的字符串值?

  3. 尝试对字符串值进行硬编码以确定问题出在字符串还是转换器代码上?

在您可以向我们提供更多详细信息之前,我们无法建议具体的解决方案。

也许您的代码需要一个完全限定的文件名。也许它需要一个相对的名字。我们不知道,因为您编写了转换器代码。文件选择器所做的只是给你一个文件名。获取文件名的正确格式取决于您。

阅读 How to Use File Choosers 上的 Swing 教程部分工作示例。

关于java - JFileChooser 产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40613111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com