Solved

Cardlayout and Nullpointer exception

Posted on 2004-04-12
7
305 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now