Solved

Cardlayout and Nullpointer exception

Posted on 2004-04-12
7
308 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 132
github account with ecipse 1 47
tomcat not starting 6 45
mysql jsp example issue 32 35
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

773 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