Java,什么是底层文档

时间:2011-04-16 12:26:10

标签: java swing nullpointerexception

我正在尝试构建一个简单的Java GUI(我已经学习了一个星期)。我创建了一个Textfield,用户必须输入一个数字。我想用数字做一些事情,但现在问题是当我调用方法textfield.getText()时,我得到一个'null指针除外。在文档中,它表示当底层文档为null时,您可以获得异常。我不知道“不正当文件”是什么。

有任何帮助吗? 代码:

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.BadLocationException;

import java.awt.*;
import java.awt.event.*;

public class GUI extends JFrame {
    String [] aFull1 = new String [500];
    String [] aFull2 = new String [500];
    String [] aFull3 = new String [500];
    String [] aFull4 = new String [500];
    String [] aList1 = new String [15];
    String [] aList2 = new String [15];
    String [] aList3 = new String [15];
    String [] aList4 = new String [15];
    int pop1 = 0;
    int pop2 = 0;
    int pop3 = 0;
    int pop4 = 0;

//JTextField tfNr = new JTextField ();
JTextField tfNr = new JTextField ("");
JButton bStart = new JButton ("Start");
JButton bPause = new JButton ("Pause");
JButton bStop = new JButton ("Stop");
JList lList1 = new JList (aList1);
JList lList2 = new JList (aList2);
JList lList3 = new JList (aList3);
JList lList4 = new JList (aList4);
JButton bOK = new JButton ("OK");
JButton bRemove = new JButton ("Remove last item");
JTextArea taTijd = new JTextArea ("00:00:00", 10, 4);
JPanel panel = new JPanel();
Timer timer = new Timer(1000, new ActionListener () {
    public void actionPerformed (ActionEvent e){
        time++;
        dispTime (); }}
);

final JTextArea taStrtNr = new JTextArea ("Voer startnummer in:");

static int time = 0;
static int pTime;

GUI () {
    // Set title //
    super ("Ultraloop");
    addToWindow ();
    setPos ();  
    setProperties ();

    // Add ActionListeners //
    handler HO = new handler ();
    bStart.addActionListener(HO);
    bPause.addActionListener(HO);
    bStop.addActionListener(HO);
    bOK.addActionListener(HO);

    /*for (int i = 0; i<100; i++){
        addOne(1,"One");
    }
    addOne(1,"Two");
    scrollDown(1);

    addOne(1,"One");
    addOne(2,"Two");
    addOne(3,"Three");
    addOne(4,"Four");*/

    // Final add //
    add(panel);
}

private class handler implements ActionListener {
    public void actionPerformed (ActionEvent e){
        if (e.getSource()==bStart){
            if (!timer.isRunning()){
                timer.start();
            }
        }
        else if (e.getSource () == bPause){
            if (timer.isRunning()){
                pTime = time;
                timer.stop();
            }
            else if (pTime != 0){
                timer.start();
                time = pTime;
            }
        }
        else if (e.getSource() == bStop){
            if (timer.isRunning()){
                timer.stop();
                time = 0;
                dispTime();
            }
        }
        else if (e.getSource() == bOK){
            try {
                if (tfNr.getDocument().getText(0,2) != null){
                    addOne(Integer.getInteger(tfNr.getDocument().getText(0,2)));
                }
            } catch (BadLocationException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
}

private void addToWindow (){
    add(tfNr);
    add(bStart);
    add(bPause);
    add(bStop);
    add(lList1);
    add(lList2);
    add(lList3);
    add(lList4);
    add(bOK);
    add(bRemove);
    add(taTijd);
    add(taStrtNr);
}
private void setPos () {
    tfNr.setBounds(20,25, 75,25);
    bStart.setBounds(500,255, 75,25);
    bPause.setBounds(500,290, 75,25);
    bStop.setBounds(500,325, 75,25);
    lList1.setBounds(20,75, 60,272);
    lList2.setBounds(80,75, 50,272);
    lList3.setBounds(130,75, 150,272);
    lList4.setBounds(280,75, 150,272);
    bOK.setBounds(110, 25, 75, 25);
    bRemove.setBounds(80,20, 150,20);
    taTijd.setBounds(445,10, 150,50);
    taStrtNr.setBounds(20, 5, 150, 25);
}
private void setProperties () {
    // Set Editable //
    taTijd.setEditable(false);
    taStrtNr.setEditable(false);

    // Set Font //
    taTijd.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 28));
    bRemove.setFont (new Font("Arial", Font.PLAIN, 12));

    // Set List properties //
    lList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    lList2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    lList3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    lList4.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    lList1.setVisibleRowCount(15);
    lList2.setVisibleRowCount(15);
    lList3.setVisibleRowCount(15);
    lList4.setVisibleRowCount(15);
    lList1.setBorder(BorderFactory.createLineBorder(Color.black));
    lList2.setBorder(BorderFactory.createLineBorder(Color.black));
    lList3.setBorder(BorderFactory.createLineBorder(Color.black));
    lList4.setBorder(BorderFactory.createLineBorder(Color.black));
    lList1.setBackground(new Color(180,180,180));
    lList2.setBackground(new Color(180,180,180));
    lList3.setBackground(new Color(180,180,180));
    lList4.setBackground(new Color(180,180,180));

    panel.setBackground(Color.WHITE);
    tfNr.setBackground(new Color(230,230,230));
    tfNr.setBorder(BorderFactory.createLineBorder(Color.BLACK));
}

private void addOne (int nr) {
    System.out.print(String.format("%d",nr));
}
private void removeOne (){

}

private void dispTime () {
    int h, m, s;
    h = time/3600;
    m = (time%3600)/60;
    s = ((time%3600)%60);
    taTijd.setText(String.format("%02d:%02d:%02d",h,m,s));
}

private void scrollDown (int a) {

}

}

3 个答案:

答案 0 :(得分:4)

如果你在这一行得到一个空指针:

if (tfNr.getText() != null){

然后看起来tfNr为空。确保在该行之前为该变量赋值。

答案 1 :(得分:4)

tfNr.getText()使用Document(PlainTeocument for JTextField)。所以它与调用tfNr.getDocument().getText(0, tfNr.getDocument().getLenght())

相同

答案 2 :(得分:0)

此行抛出异常:

addOne(Integer.getInteger(tfNr.getDocument().getText(0,2)));

这是因为此行的以下部分被评估为null:

Integer.getInteger(tfNr.getDocument().getText(0,2))

。以下方法不接受null作为参数:

private void addOne (int nr) {

因为基本类型与空指针不兼容。 因此,抛出NullPointerException。

要摆脱异常(但不是问题),你可以改变 int到上面一行中的Integer。

至于问题,你应该看看getInteger()方法是什么  实际上(检查javadoc)。 我猜你真正想做的是:

addOne(Integer.parseInt(tfNr.getText(0,2)));

或者也许:

addOne(Integer.parseInt(tfNr.getText()));

获取全文。

另请注意您不需要getDocument()。

P.S。你可能想改写一下你的问题吗?