Sizemj
Sizemj

Reputation: 322

Problems calling a method from another Java class

I am having a runtime error that states Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Status.copyState(Status.java:29) This is a class that is call the an instance of the Status calls (st): Main class is:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
 *
 * 
 * @author (Jason Sizemore) 
 * @version (11-20-09 HW09)
 */
public class BetterCalculator extends Calculator 
{   
    //attributes
    private JButton undo; 
    private String undoText;
    private Status st;

    public BetterCalculator()
    {
        super();
        st = new Status();

    }
    public void createUserInterface3()
    {
        createUserInterface2();
        undo = new JButton("undo");
        jPanel.add(undo);
        undo.setBackground(Color.red);
       undo.setToolTipText("This is the undo feature");
       undo.addActionListener(this);

    }
     public void actionPerformed(ActionEvent e)
     {
     super.actionPerformed(e);
     while(displayBox.getText() != null)
     {
         st.copyState();

        }
     if(e.getSource() == undo)
      {

        Status st;
        st = new Status();
       undoText = st.returnState();
        displayBox.setText(undoText);


      }

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

        BetterCalculator myCalc;
        myCalc = new BetterCalculator();
        myCalc.createUserInterface3();
    }
}

It is call the an instance of Status (st) Here is the Status class

import java.util.*;
import java.awt.event.*;
import java.awt.*;
/**
 * Write a description of class Status here.
 * 
 * @author (Jason Sizemore ) 
 * @version (HW09 11-21-09)
 *  This is a class to get the status for the undo feature
 */
public class Status extends BasicCalculator
{   
    //attributes
   private ArrayList<String> lastState;
   public String ls;

   public String rls;  
    //constructors

    public Status()
    {
       lastState = new ArrayList<String>(10);

    }

    //Methods
    public void copyState()
    {
         //operand1 = Double.parseDouble(displayBox.getText());
          ls = displayBox.getText();
        lastState.add(ls);

    }
    public String returnState()
    {
       int sizeOfArrayList; 
       sizeOfArrayList = lastState.size();
        rls = lastState.get(sizeOfArrayList);
        return rls;

    }
}

I know the issue is with the line ls = displayBox.getText(); I have a object take the return of a method on a object. what am I missing here.

Thanks for any help.

Upvotes: 0

Views: 459

Answers (3)

TofuBeer
TofuBeer

Reputation: 61536

As others have said it is likely that the displayBox variable is null.

The best way to deal with this is at compile time... if you get into the following habit you will catch that sort of mistake early:

public class TestFrame
    extends JFrame
{
    private final JButton aButton;
    private JButton bButton;

    public TestFrame()
    {
        // fails to compile since aButton is not instantiated
        // aButton = new JButton("A"); 
    }

    public init()
    {
        add(aButton);
        add(bButton);
        aButton.addActionListener(....);

        // crash at runtime since bButton is null
        bButton.addActionListener(....); 
    }
}

by declaring variables as "final" the compiler forces you to give them a value. Since the buttons (and other GUI items) are not likely to change you should be able to make them all final. Once you have that you will have the com;iler help you out to avoid this sort of thing.

Upvotes: 1

Chip Uni
Chip Uni

Reputation: 7504

Try adding the following line as the first line of Status.Status():

super();

Let me know if that fixes the problem!

Upvotes: 0

David Hedlund
David Hedlund

Reputation: 129832

My guess is that displayBox is not set to anything by the time copyState is called. Where is displayBox even declared?

Upvotes: 2

Related Questions