Hello everybody I have a problem with my code. On two lines (14 & 17) I get this error: cannot return a value from method whose result type is void.

But I don't have add void to my class.

Thanks in advance.

Greets

MeandJava

Here's my code:

package oop_po2;

import java.awt.Image;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class OOP_PO2_Test {
    
    public ImageIcon(String filename){
        
        java.net.URL imgURL = ImageIcon.class.getResource(filename);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + filename);
        return null;
        }
    }

    public Image getImage(String path){
        Image image = null;
        // TODO OOP_PO2_A1: Insert code here
        return image;
    }

    public static void main(String args[]){
        OOP_PO2_Test app = new OOP_PO2_Test();
        Image image = app.getImage(""); // TODO OOP_PO2_A2: Fill in the path to an existing image
        if(image != null && image instanceof Image){
            System.out.println("Successfully loaded the image file.");
        }
        else{
            System.out.println("Nothing loaded. Try again.");
        }
    }

   
}

What return type should this method be or what should the method be "called"

public ImageIcon(String filename){

you either haven't declared a return type or have not "named" it.

What return type should this method be or what should the method be "called"

public ImageIcon(String filename){

you either haven't declared a return type or have not "named" it.

How do you mean, I don't understand what you are saying? Could you say it on a different manner so I know what you meant. I have to return the value of String filename. So getImage could read it. Thanks for your help :)

Greets

MeandJava

How do you mean, I don't understand what you are saying? Could you say it on a different manner so I know what you meant. I have to return the value of String filename. So getImage could read it. Thanks for your help :)

Greets

MeandJava

How far along are you suppossed to be in your class?

In any case, what difference do you see between

public ImageIcon(String filename){

and this

public Image getImage(String path){

The second one is correct, declaring both a return type and a name. The first one is missing one of those (seemingly the name).

Edited 5 Years Ago by masijade: n/a

Thanks Masijade I'll get it and I have fixed it. Thanks for your help man now I can go on.

This article has been dead for over six months. Start a new discussion instead.