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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:16:21+00:00 2026-05-27T02:16:21+00:00

Basically I have a GUI that inherits from the JFrame class and has its

  • 0

Basically I have a GUI that inherits from the JFrame class and has its own main method.

It gives the error

Exception in thread "main" java.lang.NullPointerException
    at MilesPerGallonApp.buildPanel(MilesPerGallonApp.java:33)
    at MilesPerGallonApp.<init>(MilesPerGallonApp.java:20)
    at MilesPerGallonApp.main(MilesPerGallonApp.java:58)

Here is the source code

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

public class MilesPerGallonApp extends JFrame
{
    private JPanel panel;
    private JLabel messageLabel1;
    private JLabel messageLabel2;
    private JTextField distanceTextField;
    private JTextField gallonTextField;
    private JButton calcButton;
    private final int WINDOW_WIDTH = 500;
    private final int WINDOW_HEIGHT = 280;

    public MilesPerGallonApp()
    {
        super("Fuel Economy Calculator");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);
    }

    private void buildPanel()
    {
        messageLabel1 = new JLabel("Enter maximum distance.");
        messageLabel2 = new JLabel("Enter tank capacity.");
        distanceTextField = new JTextField(8);
        gallonTextField = new JTextField(4);
        calcButton =  new JButton("Calculate MPG");

        panel.add(messageLabel1);
        panel.add(messageLabel2);
        panel.add(distanceTextField);
        panel.add(calcButton);
    }

    private class CalcButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String gallonString;
            String milesString;
            double MPG;

            gallonString = gallonTextField.getText();
            milesString = distanceTextField.getText();

            MPG = Double.parseDouble(milesString) / Double.parseDouble(gallonString);

            JOptionPane.showMessageDialog(null, "The fuel economy is " + MPG + " miles per gallon.");
        }
    }

    public static void main(String[] args)
    {
        new MilesPerGallonApp();
    }
}

I checked that all of my variables were declared properly. I am not sure what exactly is wrong. Could anyone who is more of an expert in debugging help?

Thanks!

  • 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-27T02:16:22+00:00Added an answer on May 27, 2026 at 2:16 am

    Because panel is null and you try to call some methods of it (panel.add(messageLabel1);), you need to initialize it:

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

Sidebar

Related Questions

basically, I have a program that has a class to create a basic GUI,
Basically I have a method in my Gui class that prints and appends text
Basically I have my Emacs set up so it has a GUI specific elisp,
I have just made my first proper little desktop GUI application that basically wraps
I have a static std::map<std::string, CreateGUIFunc> in a class that basically holds strings identifying
So basically I have a plugin dll that is loaded by a GUI-Application. In
Basically I have a bunch of unmanaged VC++ static libraries. And the VC++ GUI
I basically have a page which shows a processing screen which has been flushed
I basically have 7 select statements that I need to have the results output
I am developing a GUI that has a canvas and a bunch of controls

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.