here is my snippet.It returns a null pointer exception..jlabel1 is already set and working
JLabel j[]=new JLabel[10];
i=0;
j[i]=new JLabel();
j[i].setText("1");
this.add(j[i]);
Edit (from the comments)
JLabel j[]=new JLabel[50];
try {
Rectangle bounds = jLabel1.getBounds();
j[i].setText("1");
this.add(j[i]);
j[i].setBounds(jLabel1.getX(),jLabel1.getY()+50 ,jLabel1.getHeight(),jLabel1.getWidth()); } catch(Exception e) {
System.out.println(e.toString());
}
The problem is that you forgot the
in the snippet you posted in the comments (although it was included in your question).
A
NullPointerExceptionis typically easy to debug as you just have to follow the stack trace to discover what exactly isnull. When in doubt, you just place a breakpoint on that line and use a debugger.To obtain the stacktrace, you can use
e.printStackTrace()or simply remove thetry catchblock (as it is bad practice to simply have atry catchblock for all exceptions)