Cosntrutor com throws erro construtor

9 respostas
T

pessoal boa tarde.
eu tenho uma classe com construtor com throw.
o meu problema é pq estou usando a ide netbeans, e como arrastei este componente pra um jframe, ele n me deixa criar este objeto.
da erro.

ja tentei adicionar inclusive clausula try catch no código personalizado.
alguma dica?

public JImagePanel(BufferedImage img) {
        if (img == null) {
            throw new NullPointerException("Buffered image cannot be null!");
        }
        this.background = img;
    }

    public JImagePanel(File imgSrc) throws IOException {
        this(ImageIO.read(imgSrc));

    }


    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jImagePanel1 = new componentsjapa.JImagePanel("c:\\log.bmp");//da erro aqui
....

essa classe eu peguei do vinny (moderador)
vlw…

9 Respostas

A

cara,

posta seu erro.

vc tem realmente tem esse arquivo, c:\log.bmp?

t+

T
run:

Exception in thread AWT-EventQueue-0 java.lang.RuntimeException: Uncompilable source code - unreported exception java.io.IOException; must be caught or declared to be thrown

at br.com.topsoft.topacess.View.FrmLogin.initComponents(FrmLogin.java:115)

at br.com.topsoft.topacess.View.FrmLogin.(FrmLogin.java:35)

at br.com.topsoft.topacess.View.FrmLogin.(FrmLogin.java:31)

at br.com.topsoft.topacess.View.FrmLogin$5.run(FrmLogin.java:240)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
A

cara,

posta esse classe completa.

t+

T
public class JImagePanel extends JPanel {

    BufferedImage background = null;

    /** Creates a new panel with the given background image.
     * @param img The background image. */
    public JImagePanel(BufferedImage img) {
        if (img == null) {
            throw new NullPointerException("Buffered image cannot be null!");
        }
        this.background = img;
    }

    /** Creates a new panel with the given background image.
     * @param img The background image. 
     * @throws IOException, if the image file is not found.
     */
    public JImagePanel(File imgSrc) throws IOException {
        this(ImageIO.read(imgSrc));

    }

    /** Creates a new panel with the given background image.
     * @param img The background image. 
     * @throws IOException, if the image file is not found.
     */
    public JImagePanel(String fileName) throws IOException {
        this(new File(fileName));
    }

    public JImagePanel() {
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.drawImage(background, 0, 0, this.getWidth(), this.getHeight(), null);
        g2d.dispose();
    }
A

cara,

seu construtor da sua classe JImagePanel recebe como parametro um objeto do tipoBufferedImage e vc ta passando uma String, ta ai o motivo do erro.

t+

renanreismartins

amigo veja se vc está tratanto a exception dentro de FrmLogin

alissonvla ele tem sobrecarga de construtores…

abrassss

T

pessoal existe 3 construtores…um string, um file e um bufferedImage.
caso ele passe a string, ele chama o construturo file e o bufferImage
se ele passar um file tem-se a chamada do construtor BufferedImage.
ai onde esta o problema. ao criar um objeto do tipo JImagePanel (independente do construtor) ele precisa de uma clausula try catch.
eu quero excluir esta necessidade.

Entenderam?

abrss

ViniGodoy

O problema está no “throws IOException”. Como é uma exceção verificada, vc deve captura-la.

Há duas opções:
a) Capture dentro do método e lance-a como uma RuntimeException.
b) Coloque a carga do JImagePanel num try…catch.

T

Obrigado pessoal pela ajuda.
Consegui resolver usando a dica de vinniGodoy.
abrss

Criado 26 de outubro de 2011
Ultima resposta 27 de out. de 2011
Respostas 9
Participantes 4