[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cardlayout and Nullpointer exception

Posted on 2004-04-12
7
Medium Priority
?
331 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 700 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 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