Security Exception..why?
hey,
I have a problem that I have no clue about.
Basically I have a MIDlet, and a java class
I want to access the java class functions from within the midlet so I created an object of the java class in the midlet (I declared it as one of the private datamembers at the initial declaration, but I had a problem that the application would not start). Now I created the object later in the code, only when it is needed to be used as you will see in the code below, but I'm getting a security exception at the object creation time
The Midlet Code is:
/*
* FetchPageMidlet.java
*
* Created on May 29, 2006, 9:59 PM
*/
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class FetchPageMidlet extends MIDlet implements CommandListener
{
private StreamConnection c = null;
private Display display;
private InputStream is = null;
private List list;
private Command exitCmd;
private Command backCmd;
private Command cancelCmd;
private TextBox tb;
private StringBuffer sb = new StringBuffer();
public FetchPageMidlet()
{
exitCmd = new Command("Exit", Command.EXIT, 0);
backCmd = new Command("Back", Command.BACK, 1);
try {
c = (StreamConnection)Connector.open("http://www.xxx.com/coe.txt");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public void startApp ()
{
display = Display.getDisplay(this);
String []elements = {"Create Profile", "Show Current Profiles"};
list = new List("Menu Items", List.IMPLICIT,elements,null);
list.addCommand(exitCmd);
list.addCommand(backCmd);
list.setCommandListener(this);
display.setCurrent(list);
System.out.println("here startApp");
}
public void mainMenu()
{
Display.getDisplay(this).setCurrent(list);
}
public void getInfor()
{
try
{
is = c.openInputStream();
int c;
while((c = is.read())!= -1)
{
sb.append((char)c);
}
String s = new String(sb);
tb = new TextBox("Text File Contents:", s, 100, TextField.ANY);
display.setCurrent(tb);
}
catch(IOException e)
{
System.out.println ("IOEXP .:"+ e);
e.printStackTrace ();
}
finally
{
if (c!=null)
{
try {
c.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
if(is !=null)
{
try {
is.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
public void commandAction(Command cmd, Displayable arg1) {
if (cmd == exitCmd)
{
destroyApp(true);
}
else if (cmd == backCmd)
{
destroyApp(true);
}
else if (cmd == cancelCmd)
{
System.out.println("here cancel");
mainMenu();
}
List list1 = (List)display.getCurrent();
createNewProfile cNP = new createNewProfile();
int x = list1.getSelectedIndex();
switch (x)
{
case 0: cNP.createProfile();
break;
case 1: showProfiles();
break;
}
}
public void showProfiles() {
// TODO Auto-generated method stub
System.out.println("here");
}
}
The java class code:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
public class createNewProfile implements CommandListener {
private Display display;
private List menuMarkets;
private TextField profileName;
private RecordStore rst = null;
RecordEnumeration rEnum = null;
// private List alert;
// private List autoSell;
// private List autoBuy;
private Form createNewProfile;
private String newProfileInfo;
private Command okStock = new Command("Back",Command.OK,1);
private Command cancelStock = new Command("Cancel", Command.CANCEL ,1);
private Command okCmd;
private Command cancelCmd;
private FetchPageMidlet fPM = new FetchPageMidlet();
public createNewProfile()
{
try {
rst = RecordStore.openRecordStore("Profiles",true);
rEnum = rst.enumerateRecords(null,null,true);
}
catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
public void createProfile()
{
profileName = new TextField("Profile Name","",10,TextField.ANY );
createNewProfile = new Form("New Profile");
createNewProfile.append(profileName);
createNewProfile.addCommand(okCmd);
createNewProfile.addCommand(cancelCmd);
display.setCurrent(createNewProfile);
}
public void selectStocks()
{
String []elements = {"DFM","ADFM","Qatar FM"};
menuMarkets = new List("Available Markets",List.IMPLICIT,elements, null);
menuMarkets.addCommand(okStock);
menuMarkets.addCommand(cancelStock);
display.setCurrent(menuMarkets);
}
public void commandAction(Command cmd, Displayable arg1) {
// TODO Auto-generated method stub
if (cmd == okCmd)
{
if(profileName.getString() !=null)
{
newProfileInfo = new String(profileName.getString());
selectStocks();
}
}
else if (cmd == cancelCmd)
{
fPM.mainMenu();
}
else if (cmd == okStock)
{
//do something to save selected stocks
fPM.mainMenu();
}
else if(cmd == cancelStock)
{
fPM.mainMenu();
}
List list1 = (List)display.getCurrent();
int x = list1.getSelectedIndex();
switch (x)
{
case 0: DFM();
break;
case 1: ADFM();
break;
case 2: QFM();
break;
}
}
public void ADFM() {
// TODO Auto-generated method stub
}
public void QFM() {
// TODO Auto-generated method stub
}
public void DFM() {
// TODO Auto-generated method stub
}
}
So basically the statement in bold and underlined causes the following exception:
java.lang.SecurityException: Application not authorized to access the restricted API
I wonder why is that..sorry but I'm new to J2ME
Thanks a bunch

