Estou usando o JCreator , e qnd eu commpilo essas 2 classes ele não me retorna nenhum erro mas qnd eu ponho pra executar ele me retorna o seguinte erro :
Exception in thread “main” java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
at LabelFrame.<init>(LabelFrame.java:28)
at LabelTest.main(LabelTest.java:9)
Me parrece q o erro está relacionado a figura q esta sendo inserida , no livro vem dizendo q o trecho
Icon bug = new ImageIcon( getClass().getResource( “bug1.gif” ) );
encontra a url da figura . É assim msm q funciona ou estou entendendo errado ?Esta figura esta no cd q veio junto do livro , e todos os outros exemplos do livro q esta figura faz parte tb estam dando erro.
// Fig. 11.6: LabelFrame.java
// Demonstrando a classe JLabel.
import java.awt.FlowLayout; // especifica como os componentes são organizados
import javax.swing.JFrame; // fornece recursos básicos de janela
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon; // carrega imagens
public class LabelFrame extends JFrame
{
private JLabel label1; // JLabel apenas com texto
private JLabel label2; // JLabel construído com texto e ícone
private JLabel label3; // JLabel com texto e ícone adicionados
// construtor LabelFrame adiciona JLabels a JFrame
public LabelFrame()
{
super( "Testing JLabel" );
setLayout( new FlowLayout() ); // configura o layout de frame
// Construtor JLabel com um argumento de string
label1 = new JLabel( "Label with text" );
label1.setToolTipText( "This is label1" );
add( label1 ); // adiciona o label1 ao JFrame
// construtor JLabel com string, Icon e argumentos de alinhamento
Icon bug = new ImageIcon( getClass().getResource( "bug1.gif" ) );
label2 = new JLabel( "Label with text and icon", bug,
SwingConstants.LEFT );
label2.setToolTipText( "This is label2" );
add( label2 ); // adiciona label2 ao JFrame
label3 = new JLabel(); // Construtor JLabel sem argumentos
label3.setText( "Label with icon and text at bottom" );
label3.setIcon( bug ); // adiciona o ícone ao JLabel
label3.setHorizontalTextPosition( SwingConstants.CENTER );
label3.setVerticalTextPosition( SwingConstants.BOTTOM );
label3.setToolTipText( "This is label3" );
add( label3 ); // adiciona label3 ao JFrame
} // fim do construtor LabelFrame
} // fim da classe LabelFrame
// Fig. 11.7: LabelTest.java
// Testando LabelFrame.
import javax.swing.JFrame;
public class LabelTest
{
public static void main( String args[] )
{
LabelFrame labelFrame = new LabelFrame(); // cria LabelFrame
labelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
labelFrame.setSize( 275, 180 ); // configura o tamanho do frame
labelFrame.setVisible( true ); // exibe o frame
} // fim de main
} // fim da classe LabelTest
Abçs!