[英]Java: Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
[英]Getter and Setter Java AWT-EventQueue-0 java.lang.NullPointerException
我的吸氣器和裝夾器有一些問題。 我想使用菜單項打開一個新窗口,並想將我的路徑提交到新窗口。 現在我得到了NullPointerException,但我不知道為什么。
這是我的代碼:
@SuppressWarnings("serial")
public class Gui extends JFrame implements ActionListener {
private JTextField txt_filepath;
private JButton btn_search, btn_scale, btn_delete;
private JRadioButton rb_low, rb_med, rb_high;
private JLabel lbl_outpath;
@SuppressWarnings("rawtypes")
private JList filelist;
@SuppressWarnings("rawtypes")
private DefaultListModel selectedFiles;
//JMenü
JMenuItem mitem_outpath, mitem_inpath, mitem_close, mitem_help;
//File Output
private String outpath; <- This is the String i want to commit
.
.
.
Gui() {
.
.
.
//OutPut Path
outpath=System.getProperty("user.home")+"\\Desktop\\bilder bearbeitet";
.
.
.
}
@Override
public void actionPerformed(ActionEvent arg0) {
//MENÜ Items
if(arg0.getSource()==mitem_inpath) {
//INput Path ändern
}
if(arg0.getSource()==mitem_outpath) {
//Output Path ändern
Settings settings = new Settings();
}
if(arg0.getSource()==mitem_close) {
System.exit(0);
}
if(arg0.getSource()==mitem_help) {
//Help
}
}
}
public String getOutpath() {
return outpath;
}
public void setOutpath(String outpath) {
this.outpath = outpath;
}
和我想使用路徑的類:
@SuppressWarnings("serial")
public class Settings extends JFrame implements ActionListener {
private String newPath;
private JButton btn_ok, btn_abort, btn_edit;
private JTextField txt_file;
private Gui gui;
Settings()
{
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JLabel lbl_file = new JLabel("Settings");
btn_edit = new JButton("ändern");
btn_edit.addActionListener(this);
btn_ok = new JButton("speichern");
btn_ok.addActionListener(this);
btn_abort = new JButton("abbrechen");
btn_abort.addActionListener(this);
txt_file = new JTextField(gui.getOutpath());
this.setLayout(new GridLayout(5, 2, 5, 5));
this.add(lbl_file);
this.add(txt_file);
this.add(btn_edit);
this.add(btn_ok);
this.add(btn_abort);
this.pack();
this.setLocationRelativeTo(null);
this.setSize(200, 200);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==btn_ok) {
gui.setOutpath(newPath);
}
if(arg0.getSource()==btn_edit){
newPath = txt_file.getText();
}
}
}
這是錯誤消息
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at de.patrickt.fotoscaler.Settings.<init>(Settings.java:27)
at de.patrickt.fotoscaler.Gui.actionPerformed(Gui.java:259)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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)
我究竟做錯了什么?
錯誤發生在您編寫的第27行: this.setLocationRelativeTo(null);
刪除此行,然后再次運行。
而且您還沒有提到您在哪里編寫了gui = new Gui
以及是否編寫了此代碼txt_file = new JTextField(gui.getOutpath());
在制作新的GUI之前,您將獲得NullPointerException
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.