I've got a NullPointerException at line 50 (and the init on line 12). Can't figure out why.

private String gradientText[] = { "Gradient State", "Gradient Cols" };
  private String lineText[] = { "Line Width", "Dash State", "Dash Length" };
  private String shapeText[] = { "Shape Type", "Fill State" };

  // Constructor
  public Config() {
    
    // Set the layout of the panel
    setLayout( new GridLayout(4,1,5,5) );
    
    // Initialize panels into grid
    newPanel( newLabel(shapeText), shapeState() );
    newPanel( newLabel(gradientText), gradientState() );
    newPanel( newLabel(lineText), lineState() );
    
  }
  
  private JPanel newPanel( JPanel labels, JPanel content ) {
    
    // Initialize panel and set its layout
    JPanel panel = new JPanel();
    panel.setLayout( new BorderLayout() );
    
    panel.add( labels, BorderLayout.NORTH );
    panel.add( content, BorderLayout.CENTER );
    
    // Add panel into JFrame
    add( panel );
    
    // Return the new panel
    return panel;
    
  }
  
  private JPanel newLabel( String[] text ) {
    
    // Get the length of the array
    int numLabels = text.length;
    // Create an array of labels
    JLabel label[] = new JLabel[ numLabels ];
    
    // Create new JPanel to store all labels into
    // Give innerPanel rows depending on the number of labels
    JPanel innerPanel = new JPanel();
    innerPanel.setLayout( new GridLayout(1,numLabels) );
    
    // Set label text and
    // put labels into the grid
    for( int i = 0; i < numLabels; i++ ) {
      label[i].setText( text[i] );
      innerPanel.add( label[i] );
    }
    
    // Return the label(s)
    return innerPanel;
    
  }

Thanks for any help.

Recommended Answers

All 2 Replies

The line

JLabel label[] = new JLabel[ numLabels ];

creates an array of "numLabels" length containing references of the Type JLabel, but with value null. You still have to initiate the individual elements. I.E.

label[i] = new JLabel();

The line

JLabel label[] = new JLabel[ numLabels ];

creates an array of "numLabels" length containing references of the Type JLabel, but with value null. You still have to initiate the individual elements. I.E.

label[i] = new JLabel();

Yes, thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.