出现空指针异常错误,找不到原因

Lincoln0520 2017-07-19 05:52:28
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Ftest extends JFrame
{
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextArea jTextArea = null;
private JPanel controlPanel = null;
private JButton openButton = null;
private JButton closeButton = null;
private JButton getOpenButton()
{
if(openButton == null)
{
openButton = new JButton("写入文件");
openButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
File file = new File("word.txt");
try
{
FileWriter out = new FileWriter(file);
String s = jTextArea.getText();
out.write(s);
out.close();
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
});
}
openButton.setBounds(80,80,20,18);
return openButton;
}
private JButton closeButton()
{
if(closeButton == null)
{
closeButton = new JButton("读取文件");
closeButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
File file = new File("word.txt");
try
{
FileReader in = new FileReader(file);
char[] byt = new char[1024];
int len = in.read(byt);
jTextArea.setText(new String(byt,0,len));
in.close();
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
});
}
openButton.setBounds(80,100,20,18);
return closeButton;
}
public Ftest()
{
super();
initialize();
}
private void initialize()
{
this.setSize(300,200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane()
{
if(jContentPane == null)
{
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJTextArea(),BorderLayout.CENTER);
jContentPane.add(getControlPanel(),BorderLayout.SOUTH);
}
return jContentPane;
}
private JTextArea getJTextArea()
{
return jTextArea;
}

private JPanel getControlPanel()
{
controlPanel.add(openButton);
controlPanel.add(closeButton);
return controlPanel;
}
public static void main(String[] args)
{
Ftest f = new Ftest();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}



大神们,我看了好久还是找不出错误
...全文
318 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lincoln0520 2017-07-19
  • 打赏
  • 举报
回复
简而言之。。有两个函数我返回的对象没有实例化导致gg改了一下就好了
private JTextArea getJTextArea()
{
if (jTextArea == null) {
jTextArea = new JTextArea();
}
return jTextArea;
}
private JPanel getControlPanel() {
if (controlPanel == null) {
FlowLayout flowLayout = new FlowLayout();
controlPanel = new JPanel();
controlPanel.setLayout(flowLayout);
controlPanel.add(getOpenButton(), null);
controlPanel.add(getCloseButton(), null);
}
return controlPanel;
}
qq_35942223 2017-07-19
  • 打赏
  • 举报
回复
错误信息呢
Lincoln0520 2017-07-19
  • 打赏
  • 举报
回复
我知道了。。有两个函数我定义错了
逗比123号 2017-07-19
  • 打赏
  • 举报
回复
你看错误信息,空指针异常的错误信息很明显的,你只要看一下信息中哪里出错就行了
Lincoln0520 2017-07-19
  • 打赏
  • 举报
回复
怎么个debug法,初学者
zycxnanwang 2017-07-19
  • 打赏
  • 举报
回复
错误不是看出来的,你 debug一下

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧