0

我正在尝试创建一个 JFormatted Text 字段,但是当我尝试添加它时,会向我抛出 NullPointerException。我检查了这些东西的顺序是否正确,或者某些东西是否不为空,但似乎并非如此。不过我可能是错的。

这是代码块

public static void showTopic(String path) throws IOException{
    path = "topics/"+path+".txt";
    System.out.println(path);
    String[] topicFile = Login.readFile(path);
    int b =  Login.readLines(path);

    Topic.main(null);
    JFormattedTextField formattedTextField = new JFormattedTextField();
    formattedTextField.setEditable(false);
    formattedTextField.setBounds(50, 50, 100, 100);


    for (int i=0;i<b;i++)
        formattedTextField.setText(topicFile[i]);

    Topic.Contentframe.getContentPane().add(formattedTextField);


}

和窗框

import java.awt.EventQueue;


public class Topic {

    static JFrame Contentframe;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Topic window = new Topic();
                    window.Contentframe.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Topic() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        Contentframe = new JFrame();
        Contentframe.setBounds(100, 100, 550, 539);
        Contentframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Contentframe.getContentPane().setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.setBounds(435, 0, 89, 23);
        Contentframe.getContentPane().add(btnNewButton);

    }

}

提前致谢 :)

4

0 回答 0