Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

java.lang.NullPointerException in My SwingWIthCommonFeatures program

$
0
0
The program is suppose to display :
-->Frame with six labels
-- Each label with white background
-- Foreground of the labels to black, blue,
cyan, green, magenta, and orange, respectively
-- Font of each label to TimesRoman, bold, and 20 pixels
-- Border of each label to a line border with the yellow color
-- Text and tool tip text of each label to the name of its foreground color.

The code below is my source code which turns out with this error :-
Exception in thread "main" java.lang.NullPointerException
at p1.SwingCommonFeature.<init>(SwingCommonFeature.java:14)
at p1.SwingCommonFeature.main(SwingCommonFeature.java:43)

package p1;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class SwingCommonFeature extends JFrame{
	public SwingCommonFeature(){
		setLayout(new GridLayout(2,3));
		
		Border lineBorder = new LineBorder(Color.YELLOW);
		Font font = new Font("TimesRoman",Font.BOLD,20);
		
		JLabel[] label = new JLabel[6];
		for(int i=0;i<6;i++)
			label[i].setBackground(Color.WHITE);
		
		label[0].setForeground(Color.BLACK);
		label[1].setForeground(Color.BLUE);
		label[2].setForeground(Color.CYAN);
		label[3].setForeground(Color.GREEN);
		label[4].setForeground(Color.MAGENTA);
		label[5].setForeground(Color.ORANGE);
		
		for(int i=0;i<6;i++){
			label[i].setBorder(lineBorder);
			label[i].setFont(font);
		 }
		
		label[0].setText("Black");
		label[0].setToolTipText("black");
		label[1].setText("Blue");
		label[1].setToolTipText("blue");
		label[2].setText("Cyan");
		label[2].setToolTipText("cyan");
		label[3].setText("Green");
		label[3].setToolTipText("green");
		label[4].setText("Magenta");
		label[4].setToolTipText("magenta");
		label[5].setText("Orange");
		label[5].setToolTipText("orange");
	}
	
	public static void main(String[] args){
		SwingCommonFeature frame =new SwingCommonFeature();
		
		frame.setTitle("Swing Common Features");
		frame.setSize(400, 250);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>