tags:

views:

108

answers:

3

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.

+2  A: 

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

David Hedlund
I'm guessing that `displayBox` is created in the class `Calculator`.
Chip Uni
i'm guessing it's declared there, but not instantiated, as he gets an NPE at `copyState`, it clearly isn't there. If there was something in `getText()` that caused the error, that's where the error would be, in the stack trace, so it's not getting there, and if `ls` is null, that wouldn't matter. So it's definitely a case of `displayBox` being null.
David Hedlund
Yes the displayBox class is created in the BasicCalculator class
Size_J
Assuming the exception is accurate, we can state with 99.999% certainty that displayBox is null in copyState(). Stick a log or a debug println in to prove it and then work back from there. A full stack trace would be necessary to guess further... and access to the superclass where displayBox is created.
PSpeed
A: 

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

super();

Let me know if that fixes the problem!

Chip Uni
... and the same in BasicCalculator, if BasicCalculator subclasses Calculator. If it's the other way round it could work ;)
Andreas_D
I didn't see that Status extended BasicCalculator, but Size_j only provided BetterCalculator. I shouldn't have guessed the hierarchy of classes.
Chip Uni
super() will be called implicitly in subclass constructors if no other super(...) call is specified. It is good practice to call it but not required. It will make no difference here.
PSpeed
+1  A: 

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.

TofuBeer