我的 getter 和 setter 有点问题。我想使用菜单项打开一个新窗口,并希望将我的路径提交到新窗口。现在我得到 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)
我究竟做错了什么?