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 7726635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:15:55+00:00 2026-06-01T05:15:55+00:00

I’m a newbie in java and I have a small problem. I want to

  • 0

I’m a newbie in java and I have a small problem. I want to access a variable in one class from another. I have three classes and I want to be able to access a variable in the main class to enable me read the array.

The error I am getting is

    java.lang.SecurityException: MIDlet not constructed by createMIDlet

Please see the example below. Please bear in mind they’re all in the same package.

    package tungPackage;

    import com.sun.lwuit.*;
    import com.sun.lwuit.animations.CommonTransitions;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;
    import javax.microedition.midlet.MIDlet;


    public class TungMidlet extends MIDlet implements ActionListener {
    private Command       back                = new Command("Back");
    private Command       ok                  = new Command("Ok");

    public ActionListener commandlistListener = new ActionListener() {
        public void actionPerformed(ActionEvent cmd) {

            // check which command cliked
            if (cmd.getCommand() == back) {

                // go back to previous form
                mainForm.show();
            } else if (cmd.getCommand() == ok) {

                // go forward
            }
        }
    };

    private List              list;
    private Form              mainForm;
    private Label             promptLabel;

    private housesClass houseClassObject = new housesClass();

    public int counter; //this is the variable I want to access in a class called calculate class object.

    private int sumAmmt;

    public TungMidlet tungMidletObject;
    public calculateClass calculateClassObject;



    public TungMidlet() {
        Display.init(this);
    }
    private ActionListener applistListener = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {

            if(list.getSelectedIndex()==0){

                counter++;

                if (counter>5)
                {
                    //check sum price.
                    sumAmmt = calculateClassObject.calculateSum();
                    Dialog x = new Dialog("info");
                    Label label = new Label("Maximum reached.");
                    Label label2 = new Label("Sum ammt = "+sumAmmt);
                    x.addComponent(label);
                    x.addComponent(label2);
                    x.addCommand(ok);
                    x.show();
                }
                else

                {
                    //calculate the price
                    String info = houseClassObject.randomHouse();
                    Dialog x = new Dialog("info");
                    Label label = new Label(info);
                    x.addComponent(label);
                    x.addCommand(ok);
                    x.show();
                }

            }
        }
    };


    public void startApp() {
      //calculateClassObject = new calculateClass();

       //sumAmmt = calculateClassObject.calculate(sumAmmt);

        mainForm     = new Form("Investment Categories");
        promptLabel = new Label("choose category");

        list = new List();
        list.addItem("House");
        list.addItem("Cars");
        list.addItem("Schools");
        list.addItem("Schools");
        list.addItem("Supermarkets");
        list.addItem("Stocks");
        list.addItem("Land");

        list.addActionListener(applistListener);

        mainForm.addComponent(promptLabel);
        mainForm.addComponent(list);
        mainForm.addCommand(back);
        mainForm.addCommandListener(commandlistListener);
        mainForm.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 1000));
        mainForm.show();
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}


    public void actionPerformed(ActionEvent ae) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    }

The class I want to access the “counter” variable using is shown below.

    package tungPackage;

     import java.util.Random;


     public class housesClass {
     public Random   generator  = new Random();
     public String[] houseArray = new String[5];
     public housesClass housesClassObject;

     public calculateClass calcobj;// = new calculateClass();

     public housesClass()
        {
        }

     public String randomHouse() {

       housesClassObject = new housesClass();

        houseArray[0] = "Bungalow - 20,000,000 Shillings";
        houseArray[1] = "Microhouse - 10,000,000 Shillings";
        houseArray[2] = "Flat - 200,000,000 shillings";
        houseArray[3] = "Garage apartment  - 7,000,000 shillings";
        houseArray[4] = "Studio apartment  - 13,000,000 shillings";

        int rnd = generator.nextInt(houseArray.length);

        housesClassObject.housePrices(rnd);///noma

        String house = houseArray[rnd];

        return house;
    }
     void housePrices(int houseNumber) {
     calcobj = new calculateClass();
     TungMidlet tungmidobj = new TungMidlet();
     int counter = tungmidobj.counter;
     int[] housePriceArray = new int[5];
     housePriceArray[0] = 20000000;
     housePriceArray[1] = 10000000;
     housePriceArray[2] = 200000000;
     housePriceArray[3] = 7000000;
     housePriceArray[4] = 13000000;

     int price = housePriceArray[houseNumber];

     calcobj.storePrice(counter,price);
    }
    }

The other supporting class is shown below.

     package tungPackage;

     public class calculateClass {
     int[] storeArray = new int[5];



     public calculateClass()
     { 
     }

     public void storePrice(int counter, int number2)
     {
     storeArray[counter] = number2;      
     }

    public int calculateSum()
    {
            int sum =0;

           for(int i=1; i<6; i++){
              sum= sum+storeArray[i];
         }
           return sum;
     }
    }
  • 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-01T05:15:56+00:00Added an answer on June 1, 2026 at 5:15 am
    1. remove TungMidlet constructor. If there was something useful to do there, you could also declare it protected – but this is not the case with your code snippet, see below.
      Wherever you try to invoke that constructor directly, remove code that does this and find another way to do what you need. If needed, study code examples provided in LWUIT Tutorial – Introduction for how typical things are done in LWUIT.
    2. put statement Display.init() in the beginning of the startApp method,
      just like it is done in LWUIT Tutorial – Hello, LWUIT! example code

    The reason why you are getting SecurityException is because you invoke TungMidlet constructor directly. Don’t do that.

    • MIDP API documentation for MIDlet constructor states:

    Throws:
    SecurityException – unless the application management software is creating the MIDlet.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.