calling alert from 1st class.

I want to call an alert from another class.
Let say I have Answer.java .:

public void commandAction(Command c, Displayable d)
{
            
if (c == nextCommand)
        {
              if(gameOver == true)
              {                    
                    Display.getDisplay(Millionaire.instance).setCurrent(new AlertExample());
              }  
              else
              Display.getDisplay(Millionaire.instance).setCurrent(new PlayScreen(nextQuest));
            
        }
       
      }


and AlertExample.java:

import javax.microedition.lcdui.*;
import java.io.IOException;

public class AlertExample extends Form implements CommandListener {
private Display display;
   
public AlertExample() {
try
{
         
 Alert myalert = new Alert("== Alert Example ==",  "Game Over.",  null, AlertType.INFO);
   myalert.setTimeout(5000); // milliseconds
  display.setCurrent(myalert);      
}  
catch (Exception e)
}
e.printStackTrace();
}
}

}


Are they valid ?
redcoderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
try:

mport javax.microedition.lcdui.*;
import java.io.IOException;

public class AlertExample {
   
public static void ShowAlert(Display display) {
try
{
       
 Alert myalert = new Alert("== Alert Example ==",  "Game Over.",  null, AlertType.INFO);
   myalert.setTimeout(5000); // milliseconds
  display.setCurrent(myalert);    
}  
catch (Exception e)
}
e.printStackTrace();
}
}

}

and call it using:

AlertExample.ShowAlert(display);
Leo EikelmanDirector, IT and Business DevelopmentCommented:
It looks valid to me, considering I can't see the rest of the code


What is the problem you are having?
redcoderAuthor Commented:
Do I need to put extends and implements ?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

redcoderAuthor Commented:
You mean replace

Display.getDisplay(Millionaire.instance).setCurrent(new AlertExample());

with

AlertExample.ShowAlert(display); ?
Leo EikelmanDirector, IT and Business DevelopmentCommented:
objects, he is using the AlertExampel class in this line

Display.getDisplay(Millionaire.instance).setCurrent(new AlertExample());

So I am asuming he is expecting an AlertExample type as a parementer for the setCurrent method.  From which he then gets the message.


What is the problem you are actually having?
Mick BarryJava DeveloperCommented:
yes I missed that. In that case pass the display

import javax.microedition.lcdui.*;
import java.io.IOException;

public class AlertExample extends Form implements CommandListener {
   
public AlertExample(Display display) {
try
{
       
 Alert myalert = new Alert("== Alert Example ==",  "Game Over.",  null, AlertType.INFO);
   myalert.setTimeout(5000); // milliseconds
  display.setCurrent(myalert);    
}  
catch (Exception e)
}
e.printStackTrace();
}
}

}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
redcoderAuthor Commented:
I got this error :

:\WTK22\apps\Millionaire(2)\src\AlertExample.java:8: AlertExample is not abstract and does not override abstract method commandAction(javax.microedition.lcdui.Command,javax.microedition.lcdui.Displayable) in javax.microedition.lcdui.CommandListener
public class AlertExample extends Form implements CommandListener {
       ^
C:\WTK22\apps\Millionaire(2)\src\AlertExample.java:10: cannot resolve symbol
symbol  : constructor Form ()
location: class javax.microedition.lcdui.Form
public AlertExample(Display display) {
                                     ^
C:\WTK22\apps\Millionaire(2)\src\AnswerScreen.java:90: cannot resolve symbol
symbol  : variable display
location: class AnswerScreen
                    AlertExample.ShowAlert(display);
                       
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Ok.

in your Answer.java class could you not just create a new instance of this class?

public void commandAction(Command c, Displayable d)
{

AlertExample alertEx = new AlertExample();
         
if (c == nextCommand)
        {
             if(gameOver == true)
             {                  
                  Display.getDisplay(Millionaire.instance).setCurrent(alertEx);
             }  
             else
             Display.getDisplay(Millionaire.instance).setCurrent(new PlayScreen(nextQuest));
         
        }
       
     }

try that


Leo
redcoderAuthor Commented:
Get this error :

C:\WTK22\apps\Millionaire(2)\src\AlertExample.java:8: AlertExample is not abstract and does not override abstract method commandAction(javax.microedition.lcdui.Command,javax.microedition.lcdui.Displayable) in javax.microedition.lcdui.CommandListener
public class AlertExample extends Form implements CommandListener {
       ^
C:\WTK22\apps\Millionaire(2)\src\AlertExample.java:12: cannot resolve symbol
symbol  : constructor Form ()
location: class javax.microedition.lcdui.Form
public AlertExample() {
                      ^
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Oh wait,

your AlertExample implements CommandListener

You did not post a method or methods satisfying all of the methods in the CommandListener class.


hope that helps,

Leo
redcoderAuthor Commented:
Yes,, my AlertExample Is :
import javax.microedition.lcdui.*;
import java.io.IOException;

public class AlertExample extends Form implements CommandListener {
 
private Display display;
   
public AlertExample() {
try
{
       
 Alert myalert = new Alert("== Alert Example ==",  "Game Over.",  null, AlertType.INFO);
   myalert.setTimeout(5000); // milliseconds
  display.setCurrent(myalert);    
}  
catch (Exception e)
{

e.printStackTrace();
}
}

}
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Take a look at this class that implement CommandListner

class FirstForm implements CommandListener {
 
  MIDlet theMIDlet;
 
  Command a = new Command("A",Command.SCREEN,1);
  Command b = new Command("B",Command.SCREEN,1);
 
  FirstForm(MIDlet theMIDlet) {
    super("Form 1");
    this.theMIDlet = theMIDlet;
    addCommand(a);
    addCommand(b);
    setCommandListener(this);
  }
 
  public void commandAction(Command c, Displayable d) {
    if (c == a) {
      SecondForm f = new SecondForm(theMIDlet,0);
      Display.getDisplay(theMIDlet).setCurrent(f);
    }
    else if (c == b) {
      SecondForm f = new SecondForm(theMIDlet,1);
      Display.getDisplay(theMIDlet).setCurrent(f);
    }
  }
}


You need to have commandAction as a method void not as a constructor to satisfy your implementation of CommandListener


Leo
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Notice the

public void commandAction(Command c, Displayable d)

This needs to be IN THE CLASS that is implementing CommandListener

so in your AlertExample class you would need this method in order for it to be able to implement CommandListener

Hope that helps,

Leo
redcoderAuthor Commented:
Yes, I already add in :

public void commandAction(Command c, Displayable d)
{
      if(c == exitCommand)
      {
            Display.getDisplay(Millionaire.instance).setCurrent(Millionaire.instance.getForm());
      }
       
}


and declaring all necessary variable, but still receiving error :

C:\WTK22\apps\Millionaire(2)\src\AlertExample.java:15: cannot resolve symbol
symbol  : constructor Form ()
location: class javax.microedition.lcdui.Form
public AlertExample() {
                      ^

redcoderAuthor Commented:
OK I got the program compiled by adding -- super("");
, but having running error :

java.lang.NullPointerException
      at AlertExample.<init>(+63)
      at AnswerScreen.commandAction(+7)
      at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)
      at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
      at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
      at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
      at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)
What is this error mean ?

The program is running but background running (ToolKit) is giving this error ...
Leo EikelmanDirector, IT and Business DevelopmentCommented:
which line is this happening at?

redcoderAuthor Commented:
I am looking at the execution sequence.. It could be that my execution sequence have problem..
Anyway, I will open a new topic if I couldn't solve it by then..
redcoderAuthor Commented:
Why is the alert is not showing ?
redcoderAuthor Commented:
It is not showing when it is triggered.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.