Got a little Problem with my getter and setters. I want to open a new window useing a Menu Item and want to commit my Path to the new Window. Now I get NullPointerException but I don't know why.

Here is my Code:

@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;
}

and the class where i want to use the Path:

@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();
    }
}


}

And this is the error Message

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)

What am I doing wrong?

有帮助吗?

解决方案

the error is on line 27 where you have written : this.setLocationRelativeTo(null);
remove this line and run it again.
and also you haven't mention where you have written gui = new Gui and if have written this code txt_file = new JTextField(gui.getOutpath()); befor making a new gui you will get NullPointerException

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top