Solved

Cardlayout and Nullpointer exception

Posted on 2004-04-12
7
316 Views
Last Modified: 2010-05-18
hi!

I am trying to implement cardlayout and I have Nullpointerexception.
I don't understand why....

------------------------------------------------
public class HW5
    extends JApplet
    implements ActionListener {

  private Container container;
  private GridBagLayout layout;
  private GridBagConstraints constraints;

  private JFrame wizardFrame;

  private JScrollPane scrollPane;
  private JTextArea textArea;
  private JButton backBtn;
  private JButton nextBtn;
  private JButton finishBtn;
  private JButton cancelBtn;

  private CardLayout cardManager;
  private JPanel screen;
  private JButton controls[];
  private String namesBtn[] = {
      "Back", "Next", "Finish", "Cancel"};
//----------------
//private FlowLayout layout = new FlowLayout();

  public HW5() {
  }

  public void init() {
    wizardFrame = new JFrame("Configure Plotpad");
    layout = new GridBagLayout();
    //container.setLayout(layout);
    container = wizardFrame.getContentPane();
    constraints = new GridBagConstraints();
    // backBtn= new JButton("Back");
    // nextBtn = new JButton("Next");
    // finishBtn= new JButton("Finish");
    // cancelBtn= new JButton("Cancel");

    textArea = new JTextArea(5, 30);

    scrollPane = new JScrollPane(textArea);
    container.add(scrollPane, BorderLayout.NORTH);
    // constraints.fill = GridBagConstraints.BOTH;
    //arrangeComponent(textArea, 0,0,1,1);


    //PSizes pSizes = new PSizes(ppc);
    //container.add(pSizes, BorderLayout.CENTER);

//--------------------------------

   // controls = new JButton[namesBtn.length];

    for (int count = 0; count < 4; count++) {
      controls[count] = new JButton(namesBtn[count]);          //<--------------------Nullpointerexception!!!
    }
    controls[0].addActionListener(this);//back btn
    controls[1].addActionListener(this);//next btn

    JPanel southPanel = new JPanel();
    southPanel.add(backBtn);
    southPanel.add(nextBtn);
    southPanel.add(finishBtn);
    southPanel.add(cancelBtn);
    container.add(southPanel, BorderLayout.SOUTH);

    wizardFrame.setSize(500, 400);
    wizardFrame.setVisible(true);

//------------------------------------------------------
    screen = new JPanel();
    cardManager = new CardLayout();
    screen.setLayout(cardManager);

    PlotPadConfig ppc = new PlotPadConfig();

    PDataSin pDataSin = new PDataSin(ppc);
    screen.add(pDataSin);

    PDataCos pDataCos = new PDataCos(ppc);
    screen.add(pDataCos);

    PSizes pSizes = new PSizes(ppc);
    screen.add(pSizes);

    PExtent pExtent = new PExtent(ppc);
    screen.add(pExtent);

    PAxisFormat pAxisFormat = new PAxisFormat(ppc);
    screen.add(pAxisFormat);

    PTitles pTitles = new PTitles(ppc);
    screen.add(pTitles);

    container.add(screen, BorderLayout.CENTER);
...
...
...

  public void actionPerformed(ActionEvent event) {
    if (event.getSource() == controls[0]) {
      cardManager.previous(screen);
    }
    if (event.getSource() == controls[1]) {
      cardManager.next(screen);
    }
  }
...
...
...
0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10806260
controls = new JButton[namesBtn.length];

should not be commented out
0
 

Author Comment

by:dkim18
ID: 10806333
Actually, it wan't the cause of the problem. I uncommented out and still have same problem, but this time complier complains on this line.

southPanel.add(backBtn);

I put buttons an array for cardlayout's behavior. Does it has anything to do with this problem?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 175 total points
ID: 10806367
That should be

southPanel.add(controls[0]);

but i wouldn't hard-code the index, nor would i call the array of buttons 'controls' (not specific enough)
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:dkim18
ID: 10806466
Thank you CHEJ!
Now,  my code doen't displays these panels.
why is that?(I raised 50 points more b/c I am asking another question..)

   screen = new JPanel();
    cardManager = new CardLayout();
    screen.setLayout(cardManager);

    PlotPadConfig ppc = new PlotPadConfig();

    PDataSin pDataSin = new PDataSin(ppc);
    screen.add(pDataSin);

    PDataCos pDataCos = new PDataCos(ppc);
    screen.add(pDataCos);

    PSizes pSizes = new PSizes(ppc);
    screen.add(pSizes);

    PExtent pExtent = new PExtent(ppc);
    screen.add(pExtent);

    PAxisFormat pAxisFormat = new PAxisFormat(ppc);
    screen.add(pAxisFormat);

    PTitles pTitles = new PTitles(ppc);
    screen.add(pTitles);

    container.add(screen, BorderLayout.CENTER);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10806513
You're not using the layout properly. You should be adding a String to identify which card to show. See how to use at:

http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html
0
 

Author Comment

by:dkim18
ID: 10807104
Thanks!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10807120
8-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question