Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:40:09+00:00 2026-06-13T09:40:09+00:00

So I have the following code: public class Minesweeper extends MIDlet implements CommandListener {

  • 0

So I have the following code:

public class Minesweeper extends MIDlet implements CommandListener {
  public static String error = "";

  public void startApp() throws MIDletStateChangeException {
    Display display = Display.getDisplay(this);

    canvas = new MCanvas();

    canvas.addCommand(exitCommand);
    canvas.addCommand(okCommand);
    canvas.addCommand(newCommand);

    canvas.setCommandListener(this);

    try{
        display.setCurrent(canvas);
    } catch (Exception e) {
        error = e.toString();
    }
  }
}

When I leave display.setCurrent(canvas); outside of the try block, the app fails with a NullPointerException. When I comment out that line, the app works (although obviously no canvas is added). So the error is caused by that line, or something that that line causes.

So I suround that line with try/catch. Although the error is caused by that line, the error still happens when the line is surrounded by try/catch. How can I catch the error? (I’ve tried this using Throwable as well, and it is still not caught.

MCanvas:

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class MCanvas extends Canvas {

protected void paint(Graphics g){
    //Minesweeper.p("repaint");

    Space[] data = Minesweeper.topaint;

    for(int x=0; x<data.length; x++){
        data[x].print();

        int r = data[x].row * 10;
        int c = data[x].col * 10;
        int v = data[x].value;
        String s = "";

        //System.out.println("r:"+Integer.toString(r)+" c:"+Integer.toString(c)+" s:"+Integer.toString(v));

        g.setColor(250, 0, 0);

        //Minesweeper.p("if");

        if(data[x].open){
            switch(v){
            case 0:
                g.setColor(50, 50, 50);
                break;
            case 1:
                g.setColor(100, 50, 50);
                s = "1";
                break;
            case 2:
                g.setColor(150, 50, 50);
                s = "2";
                break;
            case 3:
                g.setColor(200, 50, 50);
                s = "3";
                break;
            case 4:
                g.setColor(250, 50, 50);
                s = "4";
                break;
            case 5:
                g.setColor(250, 100, 100);
                s = "5";
            break;
            case 6:
                g.setColor(250, 125, 125);
                s = "6";
                break;
            case 7:
                g.setColor(250, 150, 150);
                s = "7";
                break;
            case 8:
                g.setColor(250, 175, 175);
                s = "8";
                break;
            case 9:
                g.setColor(250, 200, 200);
                break;
            default:
                g.setColor(250, 100, 100);
            }
        } else {
            g.setColor(0,0,0);
        }


        g.fillRect(c, r, 10, 10);

        g.setColor(250, 250, 250);
        Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);  
        g.setFont(font);
        g.drawString(s, c+5, r+8, Graphics.HCENTER | Graphics.BASELINE);

        if(data[x].hover){
            g.setColor(250, 250, 250);
            g.drawLine(c, r, c, r+9);
            g.drawLine(c, r, c+9, r);
            g.drawLine(c+9, r, c+9, r+9);
            g.drawLine(c, r+9, c+9, r+9);
        }

        //Minesweeper.p("here?");
    }

    //Minesweeper.p("here");

    //Minesweeper.p(Minesweeper.error);

    if(Minesweeper.error != null){
        g.drawString(Minesweeper.error, 10, 10, Graphics.HCENTER | Graphics.BASELINE);
    }

    Minesweeper.p("msg:"+Minesweeper.message);

    g.setColor(0, 0, 0);
    Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);  
    g.setFont(font);
    g.drawString(Minesweeper.message, this.getWidth()/2, this.getHeight()-10, Graphics.HCENTER | Graphics.BASELINE);

    Font fontsm = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);  
    g.setFont(fontsm);
}

protected void keyPressed(int keyCode) {
    int gameaction = getGameAction(keyCode);

    int c = Minesweeper.selected.col;
    int r = Minesweeper.selected.row;

    switch (gameaction) {
    case UP:
        Minesweeper.p("UP");

        if(r>0){
            Minesweeper.selected.leavehere();
            Minesweeper.getSpace(Minesweeper.selected.row - 1, Minesweeper.selected.col).gohere();
        }
        break;
    case DOWN:
        Minesweeper.p("DOWN");

        if(r<Minesweeper.height-1){
            Minesweeper.selected.leavehere();
            Minesweeper.getSpace(Minesweeper.selected.row + 1, Minesweeper.selected.col).gohere();
        }
        break;
    case LEFT:
        Minesweeper.p("LEFT");

        if(c>0){
            Minesweeper.selected.leavehere();
            Minesweeper.getSpace(Minesweeper.selected.row, Minesweeper.selected.col - 1).gohere();
        }
        break;
    case RIGHT:
        Minesweeper.p("RIGHT");

        if(c<Minesweeper.length-1){
            Minesweeper.selected.leavehere();
            Minesweeper.getSpace(Minesweeper.selected.row, Minesweeper.selected.col + 1).gohere();
        }
        break;
    }
    repaint();
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:40:10+00:00Added an answer on June 13, 2026 at 9:40 am

    As explained in Display.setCurrent API javadocs,

    …The setCurrent() method returns immediately, without waiting for the change to take place…

    Because of above, exceptions that may occur in calls triggered by setCurrent may (and most likely will) slip through your try-catch.

    To be able to catch and report such exceptions, one should study what calls are triggered by setCurrent (in your case, these are explained in API javadocs for Canvas, Event Delivery section), cover these by try-catch blocks where appropriate and design the appropriate way to report exceptions if these occur.

    In your case, try-catch could likely surround code in MCanvas.paint (this is where NPE likely occurs) and exceptions could be reported for example by showing appropriate screen with error message (eg Alert) by invoking setCurrent for that screen from catch block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: public class Test extends JFrame implements ActionListener{ private static
I have the following code: public class Tests { public static void main(String[] args)
I have the following code: public class boolq { public static void main(String[] args)
I have the following code: public class TestSynch extends Activity { public static ArrayList<HashMap<String,String>>
I have the following code: public class Test { public static void main(String[] args)
I have the following code: public class App { public static void main(String[] args)
Hi I have following code block public class Driver { static String x =
Let's assume I have the following code: public class MainClass { public static void
I have following code: public class readSensorsData extends Activity implements SensorListener { /** Called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.