hi all,

i am new to programing forums, i am here to ask a help, please if any of u r able to help me i would be great full to u and may be able to help u or others in the future.

i am making a pacman game on J2ME for my programming project, and i am stuck at displaying the tiles/objects on the mobile screen, i made it an applet first and it was working, but when i tried to convert it to midlet i was stuck and had problems in the canvas class.

this is my pain method which worked fine as an applet but not in midlet.

public void paint(Graphics gr)
	{
        
        
        
		//g will get picture which will get the images
		Graphics g = picture.getGraphics();
		for(int i=0;i<Farm.board.length;i++){
			for(int j=0;j<Farm.board[i].length();j++){
			//	String ImageName=null;
				String s;
				if (pacman.isAt(i,j))
                    s= pacman.Show();
                else
                    s = Farm.fieldArray[i][j].Show();
				
				char c = s.charAt(1);
					
				Image im = null;
				if(c=='W')
					im = wallpix;
				else if(c=='P')
					im = pacmanpix;
				else if(c=='o')
					im = orangepix;
				else im = fieldpix;
			
			if (im != null) 	
				g.drawImage(im,j*10,i*10,Graphics.TOP | Graphics.LEFT);
			}
		}

	gr.drawImage(picture, 0, 0, 200, 200, null);
        // Copy the Image to the screen
        g.drawImage(picture, 0, 0, Graphics.TOP | Graphics.LEFT);
          
	}

this is my email please help me and send me or reply to me here but am not sure how to check it, am new here.
<snipped email>

after i gain the necessary experience i intend to make more games and post it in as many programming sites as possible so others like me can find help easier in programming.

Please attach your JME source code to your next post and I will have look at it and see what can be done

I think you would use Graphics Object from paint() method parameter, not from picture(i dont know what it is)

ok so i will post the complete canvas,midlet classes and the error which i get from NetBeans when i run the "player.class", i have other classes like farm or wall etc but they r not part of the problem, the problem is mostly in the canvas class and maybe in the midlet class

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Canvas;

public class PlayerCanvas extends Canvas{

static Image picture;
Pacman pacman;
static  Image pacmanpix,wallpix,orangepix,fieldpix;


public void keyPressed(int key){   

           switch(key)
    {
        case Canvas.KEY_NUM1:
            pacman.move(1);             
            break;

        case Canvas.KEY_NUM2:
            pacman.move(2);
            break;

        case Canvas.KEY_NUM3:
            pacman.move(3);
            break;

        case Canvas.KEY_NUM5:
            pacman.move(5);
            break;          
     }
    //after you give the direction it should repaint to display the new position
     repaint();
}


/**
 * paint
 */
public void paint(Graphics g)
{

    try{
       pacmanpix = Image.createImage("/pacman.png"); 
       fieldpix = Image.createImage("/field.png");
       orangepix = Image.createImage("/orange");
       wallpix = Image.createImage("/wall");
   } catch (IOException ioex){
       System.out.println("ioex");
   }

    //g will get picture which will get the images
    /*Graphics*/ g = picture.getGraphics();
    for(int i=0;i<Farm.board.length;i++){
        for(int j=0;j<Farm.board[i].length();j++){
        //  String ImageName=null;
            String s;
            if (pacman.isAt(i,j))
                s= pacman.Show();
            else
                s = Farm.fieldArray[i][j].Show();

            char c = s.charAt(1);

            Image im = null;
            if(c=='W')
                im = wallpix;
            else if(c=='P')
                im = pacmanpix;
            else if(c=='o')
                im = orangepix;
            else im = fieldpix;

        if (im != null)     
            g.drawImage(im,j*10,i*10,Graphics.TOP | Graphics.LEFT); //the 10's is for the size of the images
        }
    }

//g.drawImage(picture, 0, 0, 200, 200, null); //the 200's is for the size of the board
    //gr.drawImage(picture, 0, 0, 200, 200, null);
    // Copy the Image to the screen
    g.drawImage(picture, 0, 0, Graphics.TOP | Graphics.LEFT);
        }

}  

this is my midlet class which there is no errors in it when i run it and i think its working good

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Canvas;



public class Player extends MIDlet implements CommandListener {
    Pacman pacman;
    public static boolean PersonOn; 
    Farm pfarm;
    int width, height;
   // Image pacmanpix,wallpix,orangepix,fieldpix;
  //  Image picture;
    boolean flag = false;
    private String[] examples = { "Play Pacman","Help" }; 
    // The example selection list
    private List examplesList;   
    // The Canvases used to demonstrate different Items
    private Canvas[] canvases;    
  // private PlayerCanvas playercanvas;
   // private Canvas canvas;
    // The MIDlet's Display object
    private Display display;    
    // Flag indicating first call of startApp
    protected boolean started;
    // Exit command
    private Command exitCommand;
    // Back to examples list command
    private Command backCommand;
    //private Command helpCommand;
    private Command okCommand;
    private Command helpCommand;

// now i need to handle the commandAction to launch the midlet

public void startApp() {

    if (!started) {
        started = true;
        display = Display.getDisplay(this); 

        // Create the common commands
        createCommands();
        //setCommandListener(this);

        // Create the canvases
        createCanvases();

        // Create the list of examples
        createList();

   // Start with the List
        display.setCurrent(examplesList);

    }


}

public void commandAction(Command c, Displayable d) {
    if (d == examplesList) {
        // New example selected
        int index = examplesList.getSelectedIndex();
        display.setCurrent(canvases[index]);
    } else if (c == exitCommand) {
        // Exit. No need to call destroyApp
        // because it is empty.
        notifyDestroyed();
    } else if (c == backCommand) {
        // Go back to main selection list
        display.setCurrent(examplesList);
    }
}
/*

private void createCommands() {
    exitCommand = new Command("Exit", Command.EXIT, 0);
    okCommand = new Command("Play", Command.OK, 1);
    backCommand = new Command("Back", Command.BACK, 1);
    helpCommand = new Command("Help", Command.HELP, 1);
}
*/
 private void createCommands() {
    exitCommand = new Command("Exit", Command.EXIT, 1);
    backCommand = new Command("Back", Command.BACK, 1);
}

// createlist is for getting all example list to appear 
private void createList(){
    examplesList = new List("Select Example", List.IMPLICIT);
    for (int i = 0; i < examples.length; i++) {
        examplesList.append(examples[i], null);
    } 
    examplesList.setCommandListener(this);
}

public void exitMIDlet() {
    destroyApp(true);
    notifyDestroyed();
}


// this method is to create different actions in the selection screen
private void createCanvases() {
    canvases = new Canvas[examples.length];
    canvases[0] = createpacman();

}

  private void addCommands(Displayable d) {
    d.addCommand(exitCommand);
    d.addCommand(backCommand);
    d.setCommandListener(this);
}

  // here where is the action is called
  private Canvas createpacman() {
    Canvas canvas = new PlayerCanvas();        
    addCommands(canvas);
    return canvas;
  }



public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}

and this is the error i get when i launch the player midlet and choose "Play Pacman"

ioex
java.lang.NullPointerException
    at Pacman_Game.PlayerCanvas.paint(PlayerCanvas.java:137)
    at javax.microedition.lcdui.Canvas.callPaint(Canvas.java:1074)
    at javax.microedition.lcdui.Display.repaint(Display.java:1566)
    at javax.microedition.lcdui.Display.registerNewCurrent(Display.java:1872)
    at javax.microedition.lcdui.Display.screenChange(Display.java:1310)
    at javax.microedition.lcdui.Display$DisplayManagerImpl.screenChange(Display.java:2961)
    at com.sun.midp.lcdui.EmulEventHandler.screenChangeEvent(EmulEventHandler.java:180)
    at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(DefaultEventHandler.java:659)

please please, help me, i have to finish the game as soon as possible! many thanks for ur help and time, i will one day(when i get better) do the same in this website!

Why I suggested to upload your source code it was because I know you are using some images for once, secondly it will make my life lot easier to just upload "src" file to my IDE and see what is going on.
Would you mind to provide the "src" directory from your project? (Just put it into ZIP and attach to next post, you need to be in advanced editing mode "Go Advanced" to be able to upload through "Manage Attachment")

@peter_budo , i would really like to give the complete source but i cant, because this is my thesis work, my final year project work, and i will get the final mark from this project, and if by any chance my source code is found on the internet i will fail and my project will be canceled, thats the only reason why i cant post all my code now, however after i graduate i will post it all and explain everything in it!! and for now will post the 2 main classes which the problems can occur in and i will make comments on it line by line so u would understand it more! my main problem is that i know i made something wrong in th paint method but i dont know what i did wrong and what it needs to be done!

my code explaination: i have so far 7 classes, and they r: Farm,Field,Orange,Pacman,Player,PlayerCanvas,Wall.

Pacman class handles the movement of the pacman

Orange class is just for making oranges/dots objects on the screen

Killer class is still empty

Field class is is to handle the background and also makes field objects

Wall class is doing what Orange class is doing.

Farm class contains the map/board of the game and initiates everything's position on the map and checks the directions if free or not and draws and creates all objects

so the rest is done in the player class, and when i wanted to convert it to midlet then i got some problems.

i will post the code with comments on it

public class PlayerCanvas extends Canvas{

static Image picture;
Pacman pacman; // making a pacman object to control the pacman movement
static Image pacmanpix,wallpix,orangepix,fieldpix;// these suppose to be the variables which contains the images


public void keyPressed(int key){ // this is to control the pacman object and we use the move() method in it


switch(key)
{
case Canvas.KEY_NUM1:
pacman.move(1);
break;

case Canvas.KEY_NUM2:
pacman.move(2);
break;

case Canvas.KEY_NUM3:
pacman.move(3);
break;

case Canvas.KEY_NUM5:
pacman.move(5);
break;
}
//after you give the direction it should repaint to display the new position
repaint(); // this suppose to repaint the screen after the movement is done
}


/**
* paint
*/
public void paint(Graphics gr) // here is the paint method i used to display the applet successfully but when i copied it to the canvas/midlet paint method i had the problems
{

try{ // here i tried to load the images to those variables
pacmanpix = Image.createImage("/pacman.png");
fieldpix = Image.createImage("/field.png");
orangepix = Image.createImage("/orange");
wallpix = Image.createImage("/wall");
} catch (IOException ioex){
System.out.println("ioex");
}

        //g will get picture which will get the images
        Graphics g = picture.getGraphics();
        for(int i=0;i<Farm.board.length;i++){
            for(int j=0;j<Farm.board[i].length();j++){
            //  String ImageName=null;
                String s;
                if (pacman.isAt(i,j))
                    s= pacman.Show();
                else
                    s = Farm.fieldArray[i][j].Show();

                char c = s.charAt(1);

                Image im = null;
                if(c=='W')
                    im = wallpix;
                else if(c=='P')
                    im = pacmanpix;
                else if(c=='o')
                    im = orangepix;
                else im = fieldpix;

            if (im != null)     
                g.drawImage(im,j*10,i*10,10,10,null); //the 10's is for the size of the images
            }
        }

    gr.drawImage(picture, 0, 0, 200, 200, null); //the 200's is for the size of the board

    }
}


my midlet class is already posted and the problem is not there i think, because all i wanted it to do is just to take the displayCurrent to the playercanvas class and it did that successfully.

however if someone wants to check my midlet class then here it is:

public class Player extends MIDlet implements CommandListener {
Pacman pacman;
public static boolean PersonOn;
Farm pfarm;
int width, height;
// Image pacmanpix,wallpix,orangepix,fieldpix;
// Image picture;
boolean flag = false;
private String[] examples = { "Play Pacman","Help" };
// The example selection list
private List examplesList;
// The Canvases used to demonstrate different Items
private Canvas[] canvases;
// private PlayerCanvas playercanvas;
// private Canvas canvas;
// The MIDlet's Display object
private Display display;
// Flag indicating first call of startApp
protected boolean started;
// Exit command
private Command exitCommand;
// Back to examples list command
private Command backCommand;
//private Command helpCommand;
private Command okCommand;
private Command helpCommand;


// now i need to handle the commandAction to launch the midlet

public void startApp() {

if (!started) {
started = true;
display = Display.getDisplay(this);

// Create the common commands
createCommands();
//setCommandListener(this);

// Create the canvases
createCanvases();

// Create the list of examples
createList();

// Start with the List
display.setCurrent(examplesList);

}


}

public void commandAction(Command c, Displayable d) {
if (d == examplesList) {
// New example selected
int index = examplesList.getSelectedIndex();
display.setCurrent(canvases[index]);
} else if (c == exitCommand) {
// Exit. No need to call destroyApp
// because it is empty.
notifyDestroyed();
} else if (c == backCommand) {
// Go back to main selection list
display.setCurrent(examplesList);
}
}
/*

private void createCommands() {
exitCommand = new Command("Exit", Command.EXIT, 0);
okCommand = new Command("Play", Command.OK, 1);
backCommand = new Command("Back", Command.BACK, 1);
helpCommand = new Command("Help", Command.HELP, 1);
}
*/
private void createCommands() {
exitCommand = new Command("Exit", Command.EXIT, 1);
backCommand = new Command("Back", Command.BACK, 1);
}

// createlist is for getting all example list to appear
private void createList(){
examplesList = new List("Select Example", List.IMPLICIT);
for (int i = 0; i < examples.length; i++) {
examplesList.append(examples[i], null);
}
examplesList.setCommandListener(this);
}

public void exitMIDlet() {
destroyApp(true);
notifyDestroyed();
}


// this method is to create different actions in the selection screen
private void createCanvases() {
canvases = new Canvas[examples.length];
canvases[0] = createpacman();

}

private void addCommands(Displayable d) {
d.addCommand(exitCommand);
d.addCommand(backCommand);
d.setCommandListener(this);
}

// here where is the action is called
private Canvas createpacman() {
Canvas canvas = new PlayerCanvas();
addCommands(canvas);
return canvas;
}



public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}

and when u run NetBeans i get this Error:

ioex
java.lang.NullPointerException
at Pacman_Game.PlayerCanvas.paint(PlayerCanvas.java:137)
at javax.microedition.lcdui.Canvas.callPaint(Canvas.java:1074)
at javax.microedition.lcdui.Display.repaint(Display.java:1566)
at javax.microedition.lcdui.Display.registerNewCurrent(Display.java:1872)
at javax.microedition.lcdui.Display.screenChange(Display.java:1310)
at javax.microedition.lcdui.Display$DisplayManagerImpl.screenChange(Display.java:2961)
at com.sun.midp.lcdui.EmulEventHandler.screenChangeEvent(EmulEventHandler.java:180)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(DefaultEventHandler.java:659)

please someone help me

I will have look at it and see what can be done and provide resources as necessary

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.