954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

cannot return a value from method whose result type is void

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.");
        }
    }

   
}
MeandJava
Light Poster
41 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

MeandJava
Light Poster
41 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

maybe this one (if you needed pass path in String form)

http://www.java2s.com/Code/Java/2D-Graphics-GUI/DisplayimagesupportedbyImageIO.htm

try {
      image = ImageIO.read(new File(filename));
    } catch (IOException ie) {
      ie.printStackTrace();
    }
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

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).

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

MeandJava
Light Poster
41 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: