开发者

Java, What is an underlying document

开发者 https://www.devze.com 2023-02-25 19:05 出处:网络
I am trying to build a simple Java GUI (I\'ve been learning for only a week). I have made a Textfield, in which the user has to enter a number. I want to do something with the number, but the problem

I am trying to build a simple Java GUI (I've been learning for only a week). I have made a Textfield, in which the user has to enter a number. I want to do something with the number, but the problem right now is that I get a 'null Pointer excepton, when I call the method textfield.getText(). In the documentation it says that you can get an exception when your underlying document is null. I don't know what an "undelying document" is.

Any help? Code:

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,"O开发者_JAVA技巧ne");
    }
    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) {

}

}


If you are getting a null pointer on this line:

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

Then it would appear that tfNr is null. Make sure you are assigning that variable a value before this line.


tfNr.getText() uses Document (PlainDocument for JTextField). So it's the same as calling tfNr.getDocument().getText(0, tfNr.getDocument().getLenght())


The exception is thrown at this line:

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

and this is because the following part of this line is evaluated to null:

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

. The following method does not accept null as an argument:

private void addOne (int nr) {

since primitive types aren't compatible with null pointers. Hence, a NullPointerException is thrown.

To get rid of the exception (but not the problem) you could change int to Integer in the line above.

As for the problem, you should take a look at what the getInteger() method actually does (check out the javadoc). I'm guessing that what you really want to do is:

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

or maybe:

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

to get the whole text.

Also note how you don't need getDocument().

P.S. You might want to rephrase your question?

0

精彩评论

暂无评论...
验证码 换一张
取 消