JTextArea
의 텍스트를 변수에 저장하려고 시도한 다음 FileWriter/BufferedWriter
과 함께 사용하여 파일에 내용을 저장합니다. 이 같은 일반 문자열로 작동합니다Nullpointer 예외 오류 - JTextArea에서 텍스트를 가져 오려고 시도
String stored = "Example";
그러나 나는 항상 다음을 사용하려고 nullpointer 예외 오류가 발생합니다 :
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
HeadBar retrieve = new HeadBar();
BufferedWriter bw = null;
try {
String stored = retrieve.txtarea.getText();
FileWriter fw = new FileWriter("H:/UserInput.txt");
bw = new BufferedWriter(fw);
bw.write(stored);
bw.flush();
System.out.println("Text saved successfully.");
} catch (IOException ex){
System.out.println("Error:" + ex.getMessage());
}
}
});
방법에 Headbar
클래스의 일부 :
txtarea = new JTextArea();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 800;
c.weighty = 5.0;
c.anchor = GridBagConstraints.CENTER;
c.gridx = 0;
c.gridwidth = 20;
c.gridy = 20;
panel.add(txtarea, c);
내가 잘못했거나 getText()
잘못된 방법이 있습니까?
HeaderBar 클래스의 코드를 게시 할 수 있습니까? 또한 어떤 라인이 NPE를 유발합니까? – jr593
가능한 [NullPointerException이란 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it)) –