I cannot for the life of me get my GUI to comeup with my deckPanel displayed along with my JButton that I intented to add an Action Listener to. THanks for looking. I am sure it has something to do with my object creation and method calls.

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

public class OrderCalc extends JFrame 
{


    private deckPanel decks; 
    private truckPanel trucks;
    private wheelPanel wheels;
    private extraPanel extras; 

    private JButton button; 


    //Constructor

    public OrderCalc()
    {

    deckPanel dp = new deckPanel(); 

        //set title
        setTitle("Decks, Trucks, Wheels & Accessories Calculator");

        //specify an action for the close button
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //add a flow layout manager
        setLayout(new BorderLayout());

        //create the object panels

        trucks = new truckPanel();
        wheels = new wheelPanel();
        extras = new extraPanel(); 

        //create the button panel 
        buildButtonPanel();
        dp.deckPanel();


        //add the components to the pane
        add(decks, BorderLayout.NORTH); 
        add(trucks, BorderLayout.EAST);
        add(wheels, BorderLayout.WEST);
        add(extras, BorderLayout.SOUTH);
        add(button, BorderLayout.CENTER); 

        //pack and display the window
        pack();
        setVisible(true); 


    }

    //buildDeckPanel mothod adds a list containing the names of the decks to the panel.

    private void buildButtonPanel()
    {

        button = new JButton(); 

        button = new JButton("Calculate"); 

        button.addActionListener(new JButtonListener()); 

    }


    public class JButtonListener 
                            implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {

        }
    }

    //main

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

}




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



    public class deckPanel extends JPanel{


        private String[] BOARDS = { "The Master Thrasher: $60", "The Dictator: $45",
            "The Street King: $50"};

        private JPanel deckPanel;
        private JList deckList;

            //constructor 
          public void deckPanel()
        { 

            deckList = new JList(BOARDS); 

            deckList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 

            deckPanel.add(deckList); 


        }


    }

I can't see how that could run without throwing Exceptions - eg line 117, deckPanel is not initialised, so that will throw a null pointer exception.

How are you trying to run this? javaw or what?

Two errors.

Error #1:

OrderCalc
In line 10, you declare "decks".

private deckPanel decks; 

In line 47, you attempt to use "decks":

add(decks, BorderLayout.NORTH); 

However, you haven't created a new instance of "decks":

deckPanel decks = new deckPanel();

before attempting to use it.

In line 23, you have:

deckPanel dp = new deckPanel(); 

and in line 43:

dp.deckPanel();

Did you mean to have two instances of deckPanel? Or did you decide to change the name at some point and forgot to rename some things.

The fix:

deckPanel decks = new deckPanel(); //added

//add the components to the pane
add(decks, BorderLayout.NORTH);

Error #2:

deckPanel
In line 106, you declare:

 private JPanel deckPanel;

In line 117, you attempt to use deckPanel:

deckPanel.add(deckList); 

without creating a new instance first:

deckPanel = new JPanel();

The fix:

    deckPanel = new JPanel(); //added

    deckPanel.add(deckList); 

Edited 2 Years Ago by cgeier

Thanks a ton. No I did not realize what I was doing wrong. I merged this program into seperated classes from one original class and things got dicey.

This question has already been answered. Start a new discussion instead.