Solved

Cardlayout and Nullpointer exception

Posted on 2004-04-12
7
307 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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

18 Experts available now in Live!

Get 1:1 Help Now