Hi, there I get this error while compiling my code the problem is in the main method, last line when it tries to check the 2 int's myModel.getImageRect(9,9);
this is to check if the model size is good for my image size.
this compiles but gives me an error when I RUNCode:import java.awt.image.*;
import javax.imageio.*;
import java.io.File;
import javax.swing.*;
public class SlidingBlockModel
{
BufferedImage original_image; // the image to be cut up into blocks
int numCols; // number of columns in the grid of blocks
int numRows; // number of rows in the grid of blocks
int empty_col; // holds the column index of the one empty grid element
int empty_row; // holds the row index of the one empty grid element
BufferedImage[][] puzzle; // the two '[][]' allows a 2-D array
// to be created - each element is an image
SlidingBlockModel myModel;
public static void main(String[] args)
{
SlidingBlockModel myModel;
myModel = new SlidingBlockModel(5, 4, "KTM.jpg");
myModel.getImageRect(9,9);
}
public SlidingBlockModel (int input_numCols, int input_numRows, String image_filename)
{
numCols = input_numCols;
numRows = input_numRows;
BufferedImage original_image = null;
try
{
original_image = ImageIO.read(new File(image_filename));
System.out.println("image " + image_filename + " loaded in ok." );
System.out.println("High is " + original_image.getHeight() + " and width is " + original_image.getWidth());
}
catch (Exception e)
{
System.out.println("Sorry - couldn't load in file " + image_filename);
}
}
public BufferedImage getImageRect(int thisCol, int thisRow)
{
int w = original_image.getWidth()/numCols;
int h = original_image.getHeight()/numRows;
int x = thisCol*w;
int y = thisRow*h;
return original_image;
}
public BufferedImage getSubimage(int x, int y,int w,int h)
{
return original_image;
}
}
image KTM.jpg loaded in ok.
High is 214 and width is 319
Exception in thread "main" java.lang.NullPointerException
at SlidingBlockModel.getImageRect(SlidingBlockModel.j ava:51)
at SlidingBlockModel.main(SlidingBlockModel.java:24)
Thanks
Pedro

