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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:30:16+00:00 2026-05-28T01:30:16+00:00

I am trying to get a value from my Calculator (which is in its

  • 0

I am trying to get a value from my Calculator (which is in its own separate class) to the JTextPane in my other class. My only concern is that I am not able to do so because of the design of my program.

In my main class I have an inner class that opens up another frame when the JMenuItem is clicked.


public class Notepad extends JFrame
{
    ...
    // Opens when the user clicks Calculator 
    // (JMenuItem in the JFrame of the Notepad class)
    private class Calculator implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            Calculate c = new Calculate();
            c.buildGUI();

            // I've tried creating a reference to the Insert class and tried to
            // retrieve the value from the JLabel in the Calculator but continuously
            // receive a NullPointerException
        }
    }
    ...
}

And in my other class I have an inner class for the Insert button (which allows the user to insert their answer into the JTextPane if they wish).

***I’ve tried many things here such as creating a “getter” and “setter” that passes the value onto the Notepad class but have discovered that they don’t work due to the setup of my program.


public class Calculate extends JFrame
{
    ...
    /* Insert
     * Inserts the answer into the 
     * text pane of the Notepad class
     */
    private class Insert implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String answer = proposal.getText(); // from the JLabel
            if (answer.isEmpty()) JOptionPane.showMessageDialog(frame, "Enter two numbers and hit the desired operator, please");
            // else, insert the answer
            // ***
        }
    }
    ...
}

Another one of my problems is that when the JMenuItem (Calculator) in the frame for my Notepad is clicked I receive a NullPointerException because there is no value in the JLabel (Answer for the calculator).

So how do I grab the value of the JLabel from the Calculator and put it into the JTextPane of my Notepad frame when Insert is clicked? Also if my program is not setup to perform such an action, do you have any re-design suggestions?

  • 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-28T01:30:17+00:00Added an answer on May 28, 2026 at 1:30 am

    Simplest way is to pass a reference to the NotePad into the Calculator class. The Calculator class will then look like this:

      public class Calculator extends JFrame{
    
             Notepad notepad;
    
             public Caluclator(Notepad np){
                 this();
                 notepad = np;
                 //any other code you need in your constructor
             }
    
             ...
    
             private class Insert implements ActionListener
             {
               public void actionPerformed(ActionEvent e)
               {
                  String answer = proposal.getText(); // from the JLabel
                    if (answer.isEmpty()) JOptionPane.showMessageDialog(frame, "Enter two numbers and hit the desired operator, please");
                    else{
                         notepad.myJTextPane.setText(answer);
                    }
                // ***
             }
    
    
    }
    

    And to call it in the Notepad class:

       Calculate c = new Calculate(Notepad.this);
    

    Having said that, it would be a good idea to look up some design patterns such as Observer which are precisely for updating one class when another is changed.

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

Sidebar

Related Questions

While i am trying to get value from arraylist,i got the following errors.My sample
i am trying to get the value from my methods and display it in
I'm trying to get the onreadystate value from the browser control do detect when
I'm trying to get a specific value from a mysql_fletch_assoc array. I explain. Here
I'm trying to get the returned value from a java method, but it returns
Im trying to work out how to get the value from one setting in
I'm trying to figure out how to get the value from a method in
I'm trying to get the $url value to display from the MySQL database but
I'm trying to get an RGB value of a pixel from a CGImageRef object.
I am trying to add value to CoreData from .plist and get from coreData

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.