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 8719107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:42:40+00:00 2026-06-13T06:42:40+00:00

I am new to Java and I am getting a null pointer exception. I

  • 0

I am new to Java and I am getting a null pointer exception. I know where it is occuring but cannot determine the why. It is in the below section of code. The code is validating the time wage and format of wage and time.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javagui.enterButtonActionPerformed(javagui.java:167)
    at javagui$1.actionPerformed(javagui.java:86)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

Here’s the code: –

import java.awt.Component;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Andy
 */
public class javagui extends javax.swing.JPanel {


private JLabel tutorTimeLabel;
private JLabel paymentLabel;
private JScrollPane jScrollPane1;
private JTextArea reportTextArea;
private JTextField tutorTime;
private JTextField payment;
private JButton enterButton;
private JButton reportButton;
private JButton quitButton;
private Component paymentText;
private Component tutorTimeText;
//2d array
double[][] timeWage;
//position in array
int timeWageIndex = 0;
//minimum wage
double minimumWage = 6.55;
static final String lineSeparator = System.getProperty("line.separator");







/** Creates new form GUI */
public javagui() {
    initComponents();



}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {



    tutorTimeLabel = new javax.swing.JLabel();
    paymentLabel = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    reportTextArea = new javax.swing.JTextArea();
    tutorTimeText = new javax.swing.JTextField();
    paymentText = new javax.swing.JTextField();
    enterButton = new javax.swing.JButton();
    reportButton = new javax.swing.JButton();
    quitButton = new javax.swing.JButton();

    tutorTimeLabel.setText("Enter total tutoring time in minutes:");

    paymentLabel.setText("Enter total amount earned this session:");

    reportTextArea.setColumns(20);
    reportTextArea.setRows(5);
    jScrollPane1.setViewportView(reportTextArea);

    enterButton.setText("Enter");
    enterButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            enterButtonActionPerformed(evt);
        }
    });

    reportButton.setText("Report");
    reportButton.addActionListener(new java.awt.event.ActionListener() {
        @Override
    public void actionPerformed(java.awt.event.ActionEvent evt) {
            reportButtonActionPerformed(evt);
        }
    });

    quitButton.setText("Quit");
    quitButton.addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            quitButtonActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(23, 23, 23)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(enterButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(reportButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(quitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(tutorTimeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(paymentLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(paymentText)
                        .addComponent(tutorTimeText, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))))
            .addContainerGap(58, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(tutorTimeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(24, 24, 24)
                    .addComponent(tutorTimeLabel)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(paymentLabel)
                .addComponent(paymentText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(enterButton)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(reportButton)
                    .addGap(18, 18, 18)
                    .addComponent(quitButton)
                    .addContainerGap(320, Short.MAX_VALUE))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)))
    );
}// </editor-fold>




public void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    //select to close the Calculator
    System.exit(0);

}                                          

public void reportButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:

    reportTextArea.setText(""); // clear previous report
        String newline = "\n";
        double timeTotal = 0;
        double wageTotal = 0;


        //Loops through each entry
        for(int i = 0; i<=timeWageIndex-1; i++){
            //Gets current entry time
            double time = timeWage[i][0];
            //Gets current entry wage
            double wage = timeWage[i][1];
            //Adds to sums
            timeTotal += time;
            wageTotal += wage;

            reportTextArea.append("Minutes = " + time + "    Earnings = $" + wage + newline);
        }

        DecimalFormat df = new DecimalFormat("#.##");
        double average = wageTotal / (timeTotal/60);
        //Displays report
        reportTextArea.append(newline + newline + "*****************************" + newline + newline + newline);
        reportTextArea.append("Report of your wages to Date:" + newline + newline);
        reportTextArea.append("Total Minutes Spent Tutoring = " + timeTotal + newline);
        reportTextArea.append("Total Earnings = $" + wageTotal + newline);
        reportTextArea.append("Average Per Hour Wage = $" + df.format(average) + newline + newline);
        reportTextArea.append("Minimum Wage is currently: $" + minimumWage + newline);

    if(average<minimumWage) {
                    reportTextArea.append("Your average wages are less than average");
                }
        else if(average>minimumWage && average < minimumWage*2.0) {
                    reportTextArea.append("Your average wages are average");
                }
        else if(average >= minimumWage*2.0) {
                    reportTextArea.append("Your average wages are above average");
                }
    }


   public void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:



    //Code Exception error messages when data keyed incorrectly for both textfields
                    double time = 0;
        double wage = 0;


                    //Validates time
        try {
            time = Double.parseDouble(tutorTime.getText());
        } catch (NumberFormatException e1) {
            JOptionPane.showMessageDialog(null, "The time must be a decimal number", "Invalid Input",JOptionPane.ERROR_MESSAGE);
        }

        //Validate wage
        try {
            wage = Double.parseDouble(payment.getText());
        } catch (NumberFormatException e1) {
            JOptionPane.showMessageDialog(null, "The wage must be a decimal number", "Invalid Input",JOptionPane.ERROR_MESSAGE);
        }

        //Time greater than 0 and less than 4 hours
        if(time<=0 || time>240){
            JOptionPane.showMessageDialog(null, "The time must greater than 0 and less than or equal to 4 hours (240 min)", 
                    "Invalid Input",JOptionPane.ERROR_MESSAGE);
        }
        else{
            //Wage greater than 0
            if(wage<=0){
                JOptionPane.showMessageDialog(null, "The wage must greater than 0", 
                        "Invalid Input",JOptionPane.ERROR_MESSAGE);
            }
            else{
                //update array values
                timeWage[timeWageIndex][0] = time;
                timeWage[timeWageIndex][1] = wage;

                //increment index
                timeWageIndex++; 

                //Reset text for user entry
                tutorTime.setText("");
                payment.setText("");
            }
                    } 
   }

}

  • 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-06-13T06:42:42+00:00Added an answer on June 13, 2026 at 6:42 am

    These two text fields are defined:

    private JTextField tutorTime;
    private JTextField payment;
    

    However, they are not initialized, instead, these two are initialized and used in the panel:

    private Component paymentText;
    private Component tutorTimeText;
    

    Still, in enterButtonActionPerformed there is a reference to tutorTime and payment which are null and these calls result in NullPointerException. Two fix, replace references to tutorTime with tutorTimeText. Same for payment. Not sure why these fields are defined as Component, so you’d need a cast to JTextField, ie:

    time = Double.parseDouble(((JTextField) tutorTimeText).getText());
    

    Also note that timeWage array is also not initialized, next you’ll have to deal with yet another NullPointerException.

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

Sidebar

Related Questions

I am having trouble with java.util.LinkedList. I am getting a null pointer exception upon
New to android programming. Getting Following error. 04-07 14:49:05.452: ERROR/AndroidRuntime(1566): FATAL EXCEPTION: main java.lang.OutOfMemoryError
I'm getting a Null Pointer Exception while in executing a macro to do some
I am getting a Null Pointer Exception Error at line 23 and line 78
I'm new to regex in Java and am having trouble getting it to work
So I have a null pointer exception when run. I am supposed to create
so i am using the Processing IDE and keep getting this strange null pointer
I was trying a Producer/Consumer Problem, but I don't know why I am getting
We're getting this error java.lang.NullPointerException at java.util.ArrayList.<init>(Unknown Source) at de.mystuff.ExtendedArrayList.<init>(ExtendedArrayList.java:38) where ExtendedArrayList:38 is new
Hi i'm new bee to android. while executing my application I'm getting nullpoint exception

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.