×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Guatemala
    Posts
    186

    IllegalArgumentException when calling Display.setCurrent(...)

    Hello everybody.

    I'm starting to learn Java ME, and am trying to run an example game called SheepDog (which is part of a developer guide in the SDK's documentation).

    I'm getting an "Uncaught exception java/lang/IllegalArgumentException: Param is null." message in the console when calling the setCurrent method, and the exception is not being caught even though the code is inside a try-catch block.

    Here's the relevant portion of the code:

    Code:
            	//SplashScreen s = new SplashScreen(this);
        		//Display d = Display.getDisplay(this);
            	try {
            		//d.setCurrent(s);
            		Display.getDisplay(this).setCurrent(new SplashScreen(this));
            	}
            	catch (IllegalArgumentException ex)
            	{
            		ex.printStackTrace();
            	}
            	catch (IllegalStateException ex)
            	{
            		ex.printStackTrace();
            	}
            	catch (NullPointerException ex)
            	{
            		ex.printStackTrace();
            	}
    I've tried separating the instructions, as shown by the commented lines, and the one that is causing the error is the call to setCurrent. When doing so, I've checked d (Display) and s (SplashScreen), and both have non-null values.

    This is the SplashScreen's constructor:

    Code:
        SplashScreen(SheepDogMIDlet midlet)
        {
            this.midlet = midlet;
            setFullScreenMode(true);
           	splashImage = SheepDogMIDlet.createImage("/splash.png");
            new Thread(this).start();
        }
    And this is the createImage method from the SheepDogMIDlet class:
    Code:
        static Image createImage(String filename)
        {
            Image image = null;
            try
            {
                image = Image.createImage(filename);
            }
            catch (java.io.IOException ex)
            {
                ex.printStackTrace();
            }
            return image;
        }
    So, I have two questions:

    1) What could be the reason for the IllegalArgumentException exception ?
    2) Why the exception is not being caught even though I have a catch block for this kind of exception ?

    Any comments and suggestions are much welcome.

    Thank you.
    -CarlosDL

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,085

    Re: IllegalArgumentException when calling Display.setCurrent(...)

    Try to catch(Exception e) {} instead at every place and display the exception on the console to identify the issue.

    It may be due to issue with the image resource or its path.
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  3. #3
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: IllegalArgumentException when calling Display.setCurrent(...)

    Hey CarlosDL,

    1) The reason for the exception would most likely be incomplete implementation of your SplashScreen class. In your code snippets you don't provide the code that runs in your Thread, i.e. the part of the program that follows
    Code:
    new Thread(this).start();
    You will need a run() block where your Thread code should be placed. You will also need a paint block where your splash screen will be painted.

    For example a SplashScreenMidlet

    Code:
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Image;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
    
    public class SplashScreenMidlet extends MIDlet {
    
    	protected void destroyApp(boolean unconditional)
    			throws MIDletStateChangeException {
    	}
    
    	protected void pauseApp() {
    	}
    
    	protected void startApp() throws MIDletStateChangeException {
    
                Display.getDisplay(this).setCurrent(new SplashScreen(this));
    	}
    
    
    	void splashScreenDone()
    	{
    		//Resume Code from here
    	}
    	
    	static Image createImage(String filename)
    	{
    		Image image = null;
    	    try
    	    {
    	    	image = Image.createImage(filename);
    	    }
    	    catch (java.io.IOException ex)
    	    {
    	    	System.out.println(ex.getMessage());ex.printStackTrace();
    	    }
    	    return image;
    	}
    
    }
    And your SplashScreen class should look like this

    Code:
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Image;
    
    public class SplashScreen extends Canvas implements Runnable
    {
        private final SplashScreenMidlet midlet;
        private Image splashImage;
        private volatile boolean dismissed = false;
        
        SplashScreen(SplashScreenMidlet midlet)
        {
            this.midlet = midlet;
            setFullScreenMode(true);
            splashImage = SplashScreenMidlet.createImage("/splash.png");
            new Thread(this).start();
        }
        
        public void run()
        {
            synchronized(this)
            {
                try
                {
                    wait(3000L);   // 3 seconds
                }
                catch (InterruptedException e)
                {
                    // can't happen in MIDP: no Thread.interrupt method
                }
                dismiss();
            }
        }
        public void paint(Graphics g)
        {
            int width = getWidth();
            int height = getHeight();
            g.setColor(0x00FFFFFF);  // white
            g.fillRect(0, 0, width, height);
            g.setColor(0x00FF0000);  // red
            g.drawRect(1, 1, width-3, height-3);  // red border one pixel from edge
            if (splashImage != null)
            {
                g.drawImage(splashImage,
                            width/2,
                            height/2,
                            Graphics.VCENTER | Graphics.HCENTER);
                splashImage = null;
            }
        }
    
        public synchronized void keyPressed(int keyCode)
        {
            dismiss();
        }
    
        private void dismiss()
        {
            if (!dismissed)
            {
                dismissed = true;
                midlet.splashScreenDone();
            }
        }
    
    }
    Except for the SDK documentation there is additional information regarding splash screens in the Wiki

    http://www.developer.nokia.com/Commu...een_in_Java_ME

    and here using eSWT (not applicable for S40 though)

    http://www.developer.nokia.com/Commu...Screen_in_eSWT

  4. #4
    Registered User
    Join Date
    May 2009
    Location
    Guatemala
    Posts
    186

    Re: IllegalArgumentException when calling Display.setCurrent(...)

    Quote Originally Posted by im2amit View Post
    Try to catch(Exception e) {} instead at every place and display the exception on the console to identify the issue.

    It may be due to issue with the image resource or its path.
    I have tried that also, but the exception is not being caught. Odd.

    I've debugged the app, and the image is loaded correctly. When the error occurs I see the image on the emulator's screen.

    Thank you. I appreciate your answer.
    -CarlosDL

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    Guatemala
    Posts
    186

    Re: IllegalArgumentException when calling Display.setCurrent(...)

    Thanks for your answer, skalogir.

    I didn't post the complete code from the SplashScreen class, but the run and paint methods are implemented (and are pretty much like the ones you posted).

    Here's the code:

    Code:
    	public void run() {
    		synchronized (this) {
    			try {
    				wait(3000L); // 3 seconds
    			} catch (InterruptedException e) {
    				// can't happen in MIDP: no Thread.interrupt method
    			}
    			dismiss();
    		}
    	}
    
    	public void paint(Graphics g) {
    		int width = getWidth();
    		int height = getHeight();
    		g.setColor(0x00FFFFFF); // white
    		g.fillRect(0, 0, width, height);
    		g.setColor(0x00FF0000); // red
    		g.drawRect(1, 1, width - 3, height - 3); // red border one pixel from
    													// edge
    		if (splashImage != null) {
    			g.drawImage(splashImage, width / 2, height / 2, Graphics.VCENTER
    					| Graphics.HCENTER);
    			splashImage = null;
    			midlet.splashScreenPainted();
    		}
    	}
    I've put exception handlers all over the place, but nothing seems to catch this error.

    Any other suggestion will be very welcome.

    Thanks,
    -CarlosDL

  6. #6
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: IllegalArgumentException when calling Display.setCurrent(...)

    I cannot reproduce the error based on the snippets you have provided. I assume that you are using some modified version of the SheepDogMIDlet example. Can you please provide the entire code for your SheepDogMIDlet.java and SplashScreen.java?

  7. #7
    Registered User
    Join Date
    May 2009
    Location
    Guatemala
    Posts
    186

    SOLVED: IllegalArgumentException when calling Display.setCurrent(...)

    Quote Originally Posted by skalogir View Post
    I cannot reproduce the error based on the snippets you have provided. I assume that you are using some modified version of the SheepDogMIDlet example. Can you please provide the entire code for your SheepDogMIDlet.java and SplashScreen.java?
    Thanks, skalogir. Your comments made me take a closer look at the paint method, which led me to the problem and solution.

    The problem was that the splashImage's height was an odd number, making this line raise an error:

    Code:
    g.drawImage(splashImage, width / 2, height / 2, Graphics.VCENTER | Graphics.HCENTER);
    I encountered some other similar errors as well, for example, when constructing a TiledLayer from an image whose width was not a multiple of the tiles width.

    However, I still find very annoying the fact that even when the problematic code was inside try-catch blocks, exceptions were not being caught.

    I appreciate your help.

    Best,
    -CarlosDL

  8. #8
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: IllegalArgumentException when calling Display.setCurrent(...)

    Regarding the exception handling, have you tried im2amit's suggestion to remove your multiple exception handlers and use a single Exception catch block as follows?
    Code:
             
       try
                {
                   //the code that throws the exception here
                }
                catch (Exception e)
                {
                    // The code that is being executed when an exception is thrown
                }

  9. #9
    Registered User
    Join Date
    May 2009
    Location
    Guatemala
    Posts
    186

    Re: IllegalArgumentException when calling Display.setCurrent(...)

    Quote Originally Posted by skalogir View Post
    Regarding the exception handling, have you tried im2amit's suggestion to remove your multiple exception handlers and use a single Exception catch block as follows?
    Code:
             
       try
                {
                   //the code that throws the exception here
                }
                catch (Exception e)
                {
                    // The code that is being executed when an exception is thrown
                }
    Yes, I tried that from the very beginning.
    -CarlosDL

Similar Threads

  1. Display.setCurrent and Display.getCurrent
    By kruncher in forum Mobile Java General
    Replies: 2
    Last Post: 2009-11-18, 03:49
  2. Please help: Display.setCurrent() delay
    By dschuller in forum Mobile Java General
    Replies: 5
    Last Post: 2008-04-17, 06:13
  3. Display.setCurrent(Display, Displayable) on Nokia 6600
    By arilzo in forum Mobile Java General
    Replies: 0
    Last Post: 2004-05-26, 13:30
  4. IllegalArgumentException in Display.setCurrent()
    By shmoove in forum Mobile Java General
    Replies: 1
    Last Post: 2003-03-26, 20:32
  5. Problem with display.setCurrent()
    By psomerka in forum Mobile Java General
    Replies: 3
    Last Post: 2003-03-24, 18:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×