2013-08-20 2 views
0

getter 및 setter에 약간 문제가 있습니다. 메뉴 항목을 사용하여 새 창을 열고 새 창에 내 경로를 커밋하려고합니다. 이제 NullPointerException이 발생하지만 이유를 알 수 없습니다.Getter and Setter Java AWT-EventQueue-0 java.lang.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) 

어떻게 생각입니다 잘못하고있는거야?

+0

이 질문에 대한 ??? 제발 내려면 [SSCCE] 당신의 질문을 수정 곳에 언급하지 않은 (http://sscce.org/), 짧은, runnable, compilable, 그냥 문제에 대한 생성 NullPointerException – mKorbel

답변

0

오류가 27 행에 다음과 같이 쓰여 있습니다. this.setLocationRelativeTo(null);
이 줄을 제거하고 다시 실행하십시오.
또한 당신은 당신이 gui = new Gui을 작성하고 새로운 GUI를 만드는 befor를이 코드 txt_file = new JTextField(gui.getOutpath());를 작성한 경우, NullPointerException

+0

감사합니다 :) 잘 작동합니다 – user2074882