Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4322208
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:50:39+00:00 2026-05-21T08:50:39+00:00

I am trying to build a simple Java GUI (I’ve been learning for only

  • 0

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,"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) {

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T08:50:40+00:00Added an answer on May 21, 2026 at 8:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to build a simple component which should monitor, if the user
I am trying to build a simple java program which creates a db file,
I've been trying to build a simple prototype application in Django, and am reaching
I'm trying to build a very, very simple micro-webapp which I suspect will be
Trying to build a GUI application in Java/Swing. I'm mainly used to painting GUIs
I'm trying to build a simple AWT application in Java. I want all of
I have been struggling trying to test a super simple EJB project in netbeans.
I am trying to build a simple desktop application where the user enters the
I am trying to build a very simple java project in Eclipse SDK Version:
I am trying to build a simple Java project with Maven. In my pom-file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.