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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:51:04+00:00 2026-05-31T00:51:04+00:00

I receive a java.lang.NullPointerException when I click the button in this java application. import

  • 0

I receive a java.lang.NullPointerException when I click the button in this java application.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MRA_JFrame extends JFrame implements ActionListener
{
    private JTextField ageField;
    private JTextField smokesField;
    private JTextField overweightField;
    private JButton reportButton;
    private JTextArea log;
    private Patient patient;

    public MRA_JFrame()
    {
        super("GDM's Medical risk assessment");
        setSize(500, 300);
        setLocation(200, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setupGUI();

        setVisible(true);
        ageField.requestFocus();
    }

    public void setupGUI()
    {
        JPanel westPanel = setupWestPanel();
        JPanel centrePanel = setupCenterPanel();

        getContentPane().setLayout(new BorderLayout(5, 5));
        getContentPane().setBackground(Color.DARK_GRAY);
        getContentPane().add(westPanel, BorderLayout.WEST);
        getContentPane().add(centrePanel, BorderLayout.CENTER);
    }

    public JPanel setupCenterPanel()
    {
        Font f = new Font("Courier New", Font.PLAIN, 12);

        log = new JTextArea(5, 20);
        log.setEditable(false);
        log.setFont(f);

        JPanel p = new JPanel(new GridLayout(1, 1));
        p.add(new JScrollPane(log));
        return p;
    }

    public JPanel setupWestPanel()
    {
        JPanel wnPanel = setupWestNorthPanel();
        JPanel wsPanel = setupWestSouthPanel();

        JPanel p = new JPanel(new BorderLayout());
        p.add(wnPanel, BorderLayout.NORTH);
        p.add(wsPanel, BorderLayout.SOUTH);
        return p;
    }

    public JPanel setupWestNorthPanel()
    {
        ageField = new JTextField(5);
        smokesField = new JTextField(5);
        overweightField = new JTextField(5);

        JPanel agePanel = new JPanel(new GridLayout(1, 2));
        agePanel.add(new JLabel("Age: ", JLabel.RIGHT));
        JPanel p = new JPanel();
        p.add(ageField);
        agePanel.add(p);

        JPanel smokesPanel = new JPanel(new GridLayout(1, 2));
        smokesPanel.add(new JLabel("Smokes: ", JLabel.RIGHT));
        p = new JPanel();
        p.add(smokesField);
        smokesPanel.add(p);

        JPanel overweightPanel = new JPanel(new GridLayout(1, 2));
        overweightPanel.add(new JLabel("Overweight: ", JLabel.RIGHT));
        p = new JPanel();
        p.add(overweightField);
        overweightPanel.add(p);

        JPanel np = new JPanel();
        np.setLayout(new BoxLayout(np, BoxLayout.Y_AXIS));
        np.add(agePanel);
        np.add(smokesPanel);
        np.add(overweightPanel);

        return np;
    }

    public JPanel setupWestSouthPanel()
    {
        reportButton = new JButton("Report");
        reportButton.addActionListener(this);

        JPanel p = new JPanel();
        p.add(reportButton);

        return p;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == reportButton)
        {
            System.out.println("I'm Clicked!");
            patient.setAge(ageField, log);
        }
    }

the Patient class is as follows.

import javax.swing.*;

public class Patient 
{
    private int age;
    private String smoker;
    private String overweight;

    // Consturctor
    public Patient()
    {
        age = 0;
        smoker = "n";
        overweight = "n";  
    }

    public void setAge(JTextField age, JTextArea log)
    {
        try
        {
            this.age = Integer.parseInt(age.getText());

        }
        catch(NumberFormatException nfe)
        {
            log.append("Age must be an integer");

        }
    }

    public void setSmoker(JTextField smoker, JTextArea log)
    {
        if(smoker.getText().equalsIgnoreCase("y") || smoker.getText().equalsIgnoreCase("n"))
        {
            this.smoker = smoker.getText();

        }
        else
        {
            log.append("Smokes must be one 'y', 'Y', 'n' or 'N'");
        }
    }

    public void setOverweight(JTextField overweight, JTextArea log)
    {
        if(overweight.getText().equalsIgnoreCase("y") || overweight.getText().equalsIgnoreCase("n"))
        {
            this.overweight = overweight.getText();

        }
        else
        {
            log.append("overweight must be one 'y', 'Y', 'n' or 'N'");
        }
    }
}        

The application will list the variables of the patient reported and tell the user if they have used incorrect inputs in their report.

  • 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-31T00:51:06+00:00Added an answer on May 31, 2026 at 12:51 am

    Problem is in the actionPerformed() method. The class variable patient is null.

    You can do a null check like this…

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == reportButton && patient != null)
        {
            System.out.println("I'm Clicked!");
            patient.setAge(ageField, log);
        }
    }
    

    Or you can initalize the variable…

    public void actionPerformed(ActionEvent e)
    {
        if (patient == null)
        {
            patient = new Patient();
        }
    
        if(e.getSource() == reportButton)
        {
            System.out.println("I'm Clicked!");
            patient.setAge(ageField, log);
        }
    }
    

    Or you initalize the variable when you declare it…

    private Patient patient = new Patient();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app sometimes I receive this error : java.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1@4806a4a8
I need to be able to receive a fax in a java application. I
In my application, sometimes the following exception is thrown: 07-28 14:49:25.398: ERROR/AndroidRuntime(8097): java.lang.IllegalStateException: The
When I try to run Java application, I receive the following error: Exception in
I receive the exception Failed to convert property value of type [java.lang.String] to required
The application will run fine unless I add the following code. public class TFView
i'm getting the following exception: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cs.workshop.solvedroid/cs.workshop.solvedroid.SolveCaptureActivity}: java.lang.NullPointerException when i
I've received a crash report with the following log content: java.lang.NullPointerException at android.webkit.PluginFullScreenHolder.show(PluginFullScreenHolder.java:85) at
I created a broadcast receiver to start activity onReceive like this: public class BroadcastReceiver1
In Java you can do File.listFiles() and receive all of the files in a

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.