Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.ImageIO;

public class Display extends JFrame{
    JButton[] btn = new JButton[56];
    Object[] options ={"New game","Quit"};
    int[] box = new int[56];

    Boolean gameStarted = false;

    public Display(){
        super("Webale Chess");
        JPanel topBoard = new JPanel(new BorderLayout());
        JButton startBtn = new JButton("Start");
        JButton optionBtn = new JButton("Option");

    //topBoard.add(startBtn,BorderLayout.WEST);
    //topBoard.add(optionBtn,BorderLayout.EAST);
    JPanel gameBoard = new JPanel(new GridLayout(8,7));
    for(int i = 0;i<56;i++){
        btn[i] = new JButton();
        btn[i].setFocusable(false);
		btn[i].setBackground(Color.WHITE);
		gameBoard.add(btn[i]);
		if (i == 0) 
		{
			Image img = loadImage(new File("cross.jpeg"));
			//insertImage(iconImage,i);
		}
		//btn[i].addActionListener(this);
    }
    this.setLayout(new BorderLayout());
    add(topBoard,BorderLayout.NORTH);
	add(gameBoard,BorderLayout.CENTER);
	setSize(800,900);
	setVisible(true);
	setResizable(true);
    }
	
	// load image 
	public Image loadImage(File file)
	{
		try
		{
			Image image = ImageIO.read(file);
		}
		catch (IOException e)
		{
			System.out.println(e.getMessage());
		}	
		return image;
	}

	// insert image
	/**public void insertImage(Image img,int i)
	{
 		JLabel label = new JLabel(img);
		btn[i].add(label);
	} */
	
    public static void main(String[] args){
	    new Display();
    }
}


What I have tried:

how do i fix the error that mention cannot find symbol in
return image;
Posted
Updated 28-Aug-20 21:18pm
Comments
Patrice T 29-Aug-20 2:23am    
Give exact error message and position.

The issue is with this code:
Java
public Image loadImage(File file)
{
    try
    {
    	Image image = ImageIO.read(file);
    }
    catch (IOException e)
    {
    	System.out.println(e.getMessage());
    }	
    return image;
}

It's related to scope of variable. You have defined image inside try and then trying to return the same outside it's scope, thus an error.

Try:
Java
public Image loadImage(File file)
{
    Image image = null;
    try
    {
    	image = ImageIO.read(file);
    }
    catch (IOException e)
    {
    	System.out.println(e.getMessage());
    }	
    return image;
}

P.S.: For clarity, I shared the above code with change but this still needs handling around an image object which is not defined in case of error and will be returned. Please make sure you handle the same.
 
Share this answer
 
v2
Look at your code:
Java
public Image loadImage(File file)
{
    try
    {
        Image image = ImageIO.read(file);
    }
    catch (IOException e)
    {
        System.out.println(e.getMessage());
    }
    return image;
}
And think about scoping rules: where is image defined? Where are you using it?

Because you declare image inside the try block, it goes out of scope at the matching close curly bracket, so it isn't available when you hit the return statement.
Move the initial declaration outside the try block, and you'll be fine:
Java
public Image loadImage(File file)
{
    Image image = null;
    try
    {
        image = ImageIO.read(file);
    }
    catch (IOException e)
    {
        System.out.println(e.getMessage());
    }
    return image;
}
 
Share this answer
 
Java
public Image loadImage(File file)
{
    try
    {
        Image image = ImageIO.read(file);
    }
    catch (IOException e)
    {
        System.out.println(e.getMessage());
    }	
    return image;
}

You are declaring the variable image inside your try block, which means that it does not exist after that line of code has been executed. Move the declaration uotside that block as follows:
Java
public Image loadImage(File file)
{
    Image image = null;
    try
    {
        image = ImageIO.read(file);
    }
    catch (IOException e)
    {
        System.out.println(e.getMessage());
    }	
    return image;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900