To use a static variable in another class,but report NullPointerException

when TableMain is running,I run testRecord so that let TableMain add a occur informatin and
happened time in a row in TableMain,but when I run testRecord,java report a NullPointerException and I dont know how to solve this problem,thanks for helping me to check my code;(error report info is in end)
import java.awt.event.*;
import javax.swing.table.*;
import java.io.*;
public class TableMain extends JFrame{
JTable table;
static OwnModel model;
String[] columnHeader={"occur","time"};
class OwnModel extends DefaultTableModel{
public OwnModel(Object[] columnNames,int numRows){
super(columnNames,numRows);
public boolean isCellEditable(int row,int column){
return false;
public TableMain(){
model=new OwnModel(columnHeader,0);
table=new JTable(model);
JScrollPane scroll=new JScrollPane(table);
JButton save=new JButton("save record");
JButton delete=new JButton("delete record");
JPanel buttons=new JPanel();
buttons.add(save);
buttons.add(delete);
JLabel sign=new JLabel("occur record");
Container cp=getContentPane();
cp.add(BorderLayout.NORTH,sign);
cp.add(BorderLayout.CENTER,scroll);
cp.add(BorderLayout.SOUTH,buttons);
this.setSize(300,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static void main(String[] args){
new TableMain();
import java.util.Vector;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class testRecord{
public static void main(String[] args){
SimpleDateFormat simpledf=new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
String tableName="friends";
Calendar occurtime=Calendar.getInstance();
Vector record=new Vector();
record.add("send 'desc' sql, "+"operated table is :"+tableName);
record.add(simpledf.format(occurtime.getTime()));
System.out.println("model is "+Guide.model);
System.out.println("record is "+record);
Guide.model.addRow(record);
error report:
model is null
record is [send 'desc' sql,operated table is :friends, 2004/05/26 11:39:03]
Exception in thread "main" java.lang.NullPointerException
at testRecord.main(testRecord.java:14)

but I just use this constructor once,never use twice
with same jvm;I thinks my idea is not too very badIt's a public constructor (if I'm looking in the right
place) - it could be called from anywhere. To
intialize a static variable outside the declaration,
use a static initializer:private static String whatever;static {
whatever = "whatever";
}Or simply (in this case):private static String whatever = "whatever";

Similar Messages

  • How to update and use the values of variables of another class

    I can we update or use the values of the variables of another class. For example, if we have class A
    public class A //(situated in package view)
    public s0,s1;
    public void runFunction()
    ...some coding and proceedings
    s0="Hi";s1"Hello";
    ......some coding
    RequestDispatcher dispatcher = request.getRequestDispatcher("/MainUser.jsp?alert=F");
    dispatcher.forward(request, response);
    ARunner.jsp
    <jsp:useBean id="a" class="view.A" scope="session"/>
    <%
    a.runFunction();
    %>
    MainUser.jsp
    <jsp:useBean id="a" class="view.A" scope="session"/>
    <%
    System.out.println("S0:"+a.s0+" S1:"+a.s1); //should print S0:Hi S1:Hello, but printing S0:null S1:null
    %>
    A.class has some procedures and String variables which can be updated and later can be used in JSP pages. The project starts with ARunner.jsp which uses the A.class and updates the values of string variables s0 and s1of A to hi and hello respectively.And then redirects the page to MainUser.jsp.
    Now what I want is ,when I call those string variables(s0 & s1 of A.class) in any another jsp likeMainUser.jsp it should give me the value of hi and hello respectively not null as it is giving right now. Could you refine the coding for this one?

    public class A //(situated in package view)
    public String s0,s1;
    public void runFunction()
    ...some coding and proceedings
    s0="Hi";s1"Hello";
    ......some coding
    RequestDispatcher dispatcher = request.getRequestDispatcher("/MainUser.jsp");
    dispatcher.forward(request, response);
    ARunner.jsp
    <jsp:useBean id="a" class="view.A" scope="session"/>
    <%
    a.runFunction();
    %>
    MainUser.jsp
    <jsp:useBean id="a" class="view.A" scope="session"/>
    <%
    System.out.println("S0:"+a.s0+" S1:"+a.s1); //should print S0:Hi S1:Hello, but printing S0:null S1:null
    %>
    giving code again to remove the typing errors. Please guide.

  • Using a static variable declared in an applet in another class

    Hi guys,
    I created an applet and i want to use one of the static variables declared in teh applet class in another class i have. however i get an error when i try to do that...
    in my Return2 class i try to call the variable infoPanel (declared as a static JPanel in myApplet...myApplet is set up like so:
    public class myApplet extends JApplet implements ActionListener, ListSelectionListener
    here are some of the lines causing a problem in the Return2 class:
    myApplet.infoPanel.removeAll();
    myApplet.infoPanel.add(functionForm2.smgframeold);
    myApplet.infoPanel.validate();
    myApplet.infoPanel.repaint();
    here are some of the errors i get
    dummy/Return2.java [211:1] package myApplet does not exist
    myApplet.infoPanel.removeAll();
    ^
    dummy/Return2.java [212:1] package myApplet does not exist
    myApplet.infoPanel.add(functionForm2.smgframeold);
    ^
    dummy/Return2.java [213:1] package myApplet does not exist
    myApplet.infoPanel.validate();
    ^
    dummy/Return2.java [214:1] package myApplet does not exist
    myApplet.infoPanel.repaint();
    ^
    please help! thanks :)

    I don't declare any packages though....i think it just doesn't recognize myApplet for some reason..
    other errors i got compiling are:
    dummy/Return2.java [82:1] cannot resolve symbol
    symbol : variable myApplet
    location: class Return2
    updateDesc.setString(3, myApplet.staticName);
    I Don't get why i'm getting this error cuase they worked fine when myApplet was a standalone application, not an applet.
    myApplet is in the same folder as Return2 and it compiles properly.

  • Using a variable from another class

    hello friends, I have a class with the follow variable: dbcolTempMax, and the value of this variable I need in another class, how can do to use the value of the variable...thanks

    Both people above described the solution, but from the question I take it you are somewhat new to programming. Let me give you a code example which may help.
         public class ClassWithVariable {
              private int dbcolTempMax;
              public int getDbcolTempMax() {
                   return dbcolTempMax;
         public class SomeOtherClass {
              ClassWithVariable cwv = new ClassWithVariable();
              cwv.getDbcolTempMax(); // This gets the value of the variable
         }Now if the variable is static, you can provide a static "accessor" method to ge the variable. This will save you the trouble of constructing an object of the class.
    Cheers,
    Cypher

  • How to read static variable defined java class from flex?

    This is a beginner question. If I use remoteClass to map a java class and a flex class, how can I access a static variable defined in java class from the flex code?
    Thanks!

    Static propeties are by default ignored in the blazeds for serialization. You can try using another global property in the java bean which maps to the value stored in the static property( Hopefully it should work)
    eg,
    public String instanceValue = ClassName.staticValue;
    Ref: http://livedocs.adobe.com/blazeds/1/blazeds_devguide/help.html?content=serialize_data_3.ht ml

  • Calling a non-static method from another Class

    Hello forum experts:
    Please excuse me for my poor Java vocabulary. I am a newbie and requesting for help. So please bear with me! I am listing below the program flow to enable the experts understand the problem and guide me towards a solution.
    1. ClassA instantiates ClassB to create an object instance, say ObjB1 that
        populates a JTable.
    2. User selects a row in the table and then clicks a button on the icon toolbar
        which is part of UIMenu class.
    3. This user action is to invoke a method UpdateDatabase() of object ObjB1. Now I want to call this method from UIMenu class.
    (a). I could create a new instance ObjB2 of ClassB and call UpdateDatabase(),
                                      == OR ==
    (b). I could declare UpdateDatabase() as static and call this method without
         creating a new instance of ClassB.With option (a), I will be looking at two different object instances.The UpdateDatabase() method manipulates
    object specific data.
    With option (b), if I declare the method as static, the variables used in the method would also have to be static.
    The variables, in which case, would not be object specific.
    Is there a way or technique in Java that will allow me to reference the UpdateDatabase() method of the existing
    object ObjB1 without requiring me to use static variables? In other words, call non-static methods in a static
    way?
    Any ideas or thoughts will be of tremendous help. Thanks in advance.

    Hello Forum:
    Danny_From_Tower, Encephalatic: Thank you both for your responses.
    Here is what I have done so far. I have a button called "btnAccept" created in the class MyMenu.
    and declared as public.
    public class MyMenu {
        public JButton btnAccept;
         //Constructor
         public MyMenu()     {
              btnAccept = new JButton("Accept");
    }     I instantiate an object for MyMenu class in the main application class MyApp.
    public class MyApp {
         private     MyMenu menu;
         //Constructor     
         public MyApp(){
              menu = new MyMenu();     
         public void openOrder(){
               MyGUI MyIntFrame = new MyGUI(menu.btnAccept);          
    }I pass this button all the way down to the class detail02. Now I want to set up a listener for this
    button in the class detail02. I am not able to do this.
    public class MyGUI {
         private JButton acceptButton;
         private detail02 dtl1 = new detail02(acceptButton);
         //Constructor
         public AppGUI(JButton iButton){
         acceptButton = iButton;
    public class detail02{
        private JButton acceptButton;
        //Constructor
        public detail02(JButton iButton){
          acceptButton = iButton;
          acceptButton.addActionListener(new acceptListener());               
       //method
        private void acceptListener_actionPerformed(ActionEvent e){
           System.out.println("Menu item [" + e.getActionCommand(  ) + "] was pressed.");
        class acceptListener implements ActionListener {       
            public void actionPerformed(ActionEvent e) {
                   acceptListener_actionPerformed(e);
    }  I am not able to get the button Listener to work. I get NullPointerException at this line
              acceptButton.addActionListener(new acceptListener());in the class detail02.
    Is this the right way? Or is there a better way of accomplishing my objective?
    Please help. Your inputs are precious! Thank you very much for your time!

  • Calling a static method from another class

    public class Command
        public static void sortCommands(String command, String order, List object)
             if(command.equalsIgnoreCase("merge") && order == "a")
                  object.setAscending(true);
                  Mergesort.mergesort(object.list);                  // line 85
    }and
    public class Mergesort
         public static void mergesort (int[] a)
              mergesort(a, 0, a.length-1);
         private static void mergesort (int[] a, int l, int r)
         private static void merge (int[] a, int l, int m, int r)
    }Error:
    Command.java:85: cannot find symbol
    symbol : variable Mergesort
    location: class Command
    Mergesort.mergesort(object.list)
    What am I doing wrong here? Within the Command class I am calling mergesort(), a static method, from a static method. I use the dot operator + object so that the compiler would recognize the 'list' array. I tried using the
    Mergesort.mergesort(object.list);notation in hopes that it would work like this:
    Class.staticMethod(parameters);but either I am mistaken, misunderstood the documentation or both. Mergesort is not a variable. Any help would be appreciated, I have been hitting a brick wall for hours with Java documentation. Thanks all.

    [Javapedia: Classpath|http://wiki.java.net/bin/view/Javapedia/ClassPath]
    [How Classes are Found|http://java.sun.com/j2se/1.5.0/docs/tooldocs/findingclasses.html]
    [Setting the class path (Windows)|http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html]
    [Setting the class path (Solaris/Linux)|http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/classpath.html]
    [Understanding the Java ClassLoader|http://www-106.ibm.com/developerworks/edu/j-dw-javaclass-i.html]
    java -cp .;<any other directories or jars> YourClassNameYou get a NoClassDefFoundError message because the JVM (Java Virtual Machine) can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.
    javac -classpath .;<any additional jar files or directories> YourClassName.javaYou get a "cannot resolve symbol" message because the compiler can't find your class. The way to remedy this is to ensure that your class is included in the classpath. The example assumes that you are in the same directory as the class you're trying to run.

  • How to call a variable in another class????

    if i have two classes in a package. From one class, how do i get the value of one of the variable in the other class.
    For example;
    Class A have a variable call totalNum which have a value initialise.
    Class B wan to get the value variable totalNum in class A.
    So do i go abt calling it?
    Cheers

    For example;
    Class A have a variable call totalNum which have a
    value initialise.
    Class B wan to get the value variable totalNum in
    class A.Your explanation is far from clear. Is the variable in class A a field? If yes, is it a static field? Is it declared public, private, etc.?
    If you don't understand the above questions, you should do the following:
    1) Learn the basics of the Java language from the Java Tutorial and/or from an entry-level book: http://java.sun.com/docs/books/tutorial/
    2) Post future question in the New To Java Technology forum.

  • Static variables in anonymous classes ..

    How can i access a static variable from an anonymous class ..
    class A{
    outerMeth(){
    Interface anon=new Interface(){
    static int i;
    // how can i acess 'i' ..
    thanx in advance ..

    sorry .. not a valid question ..
    static var are not allowed in anonymous/inner classes ..
    sorry ..
    ignore this one ..

  • Using SAP Exit Variable twice on the same report

    Hello all,
    I need to use the same variable -Current Calendar Day (SAP Exit)- to filter against 2 characteristics on the same report.
    Unfortunately it's not possible, here the error:
    " Variable 0DAT is used in both (structure) element 11T4741S4XTWZUAJ9IQ6DUDE1, and element AZLK9YCS4XGJ940MRPMLXMDS9 for various characteristics (ZCBBIDATE, ZCBPHTDT). This is not permitted as the characteristic is required for the F4 value help structure."
    May you please suggest me a way to do it ? Thanks in advance.

    Hi
    As the 0DAT varaible check the other standard varaible which provides the same function in business content.
    Or create a varaible with Customer exit and use it.
    In CMOD write the code in like this
    WHEN 'ZDAT'
    L_ZDAT = Sy-datum.
    L_S_RANGE-low = L_ZDAT.
    and follow the other codes as you define for other varaibles.
    REgards
    M.A

  • Using variable in another class

    Im currently writting a Quiz program and i want to pass the varible holding the users score from the class "All.java" to "Results.java".
    Any ideas how i can do this?

    You can call a method in the other class and pass the value as an argument.
    You can call a setter method in the other class.
    You can move the knowledge and manipulation of that variable to the local class.
    You can use a helper class.
    All depends on what you're trying to do really, and I know that isn't much help. I struggled with the same question for a long time when I first used Java.

  • Updating a frame / use of static variables

    Hello,
    I am working with a program (installer) that walks the user through a series of question/response frames. My first question has to do with refreshing the frame with the new contents whenever an event is triggered by a button. I have a method that works, but I am wondering if it is the best way of doing this. I have done much searching on this, but only seem to find instruction on using revalidate/repaint(), which I understand to be for graphics, or examples for things like changing the text on a button using setText().
    I actually don't have a problem with doing it the way that I am, but wonder,
    1) is this the most elegant (or correct) way,
    2) is this violating the philosophy of encapsulation since I have to make
    the panels and frame static fields to make this work, because (as
    far as I can tell) there is no way to pass parameters using event listeners.
    3) am I just being really paranoid about the encapsulation thing?
    Thanks for your input, here is a stripped down example of my code:
    Allasso
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class FrameDemo extends JFrame
        implements ActionListener {
        JLabel label1;
        JLabel label2;
        JButton button1;
        JButton button2;
        static JPanel panel1;
        static JPanel panel2;
        static FrameDemo frame = new FrameDemo();
        FrameDemo(){};
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("quit")) {
                System.exit(0);
            if (e.getActionCommand().equals("openFrameTwo")) {
                frame.createFrame2();
        //code to set the frame size and location, etc HERE....
        //frame creating methods...
        public void createFrame1(){
            panel1 = new JPanel();
            panel2 = new JPanel();
            label1 = new JLabel("Message for frame ONE");
            panel1.add(label1);
            button1 = new JButton("open frame two");
            button1.setActionCommand("openFrameTwo");
            button1.addActionListener(this);
            button2 = new JButton("quit");
            button2.setActionCommand("quit");
            button2.addActionListener(this);
            panel2.add(button1);
            panel2.add(button2);
            frame.getContentPane().add(panel1,BorderLayout.NORTH);
            frame.getContentPane().add(panel2,BorderLayout.CENTER);
            frame.pack();  //actually not used in my program; just in this demo
            frame.setVisible(true);
        //Button on frame 1 pressed, action event calls createFrame2()...
        public void createFrame2(){
            frame.getContentPane().remove(panel1);      // this is the code I am mainly talking about,
            frame.getContentPane().remove(panel2);      // used to "refresh" the frame.
            panel1 = new JPanel();
            panel2 = new JPanel();
            label1 = new JLabel("Message for frame TWO");
            panel1.add(label1);
            button2 = new JButton("quit");
            button2.setActionCommand("quit");
            button2.addActionListener(this);
            panel2.add(button2);
            frame.getContentPane().add(panel1,BorderLayout.NORTH);
            frame.getContentPane().add(panel2,BorderLayout.CENTER);
            frame.pack();  //actually not used in my program; just in this demo
            frame.setVisible(true);
        public static void main(String s[]) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    FrameDemo fd = new FrameDemo();
                    //fd.codeToSetThingsUp();
                    fd.createFrame1();      //display the first frame, and start the daisy chain...
    }Edited by: Allasso on Feb 19, 2010 7:32 AM

    Joerg22 wrote:
    I don't get two independent frames, I only get one at a time.Correct. I got fooled by the method name.
    Yeah, sorry about that...
    But my basic statement is not shaken: You create and destroy several panels, whereas one panel would be sufficient.Yes, I am starting to see what you mean. See below:
    Your additional actions (JFileChooser, JTextArea) are no hindrance for using a card layout, since I assume you will operate always from a specific panel.
    Okay.
    Still, it may turn out that you are most happy with your original design, for I assume the creation and destruction of multiple panels will not become a performance issue in your case.
    This is largely an academic experience (even though I intend to use the program I am working on). Basically, I am exploring Java, and also trying to get a feel for what means are congruent with the spirit of OO programming. So if something is a performance issue, or any other issue, I am interested in knowing about it.
    This is my first experience with GUI in any language, and essentially I am new to OO design as well. All of my previous experience has been with scripting.
    Bye
    JörgYour input is much appreciated, thanks.
    Allasso
    And now, this is what I have found so far, in regard to (not) creating and destroying panels. One note here as well, is that I found that if I placed my instructions for changing the panels (whether it be destroy/create or just changing the contents thereof) directly in the actionPerformed() method, rather than making them each separate methods and calling those methods from the actionPerformed() method, I did not have to declare the panel variables as static. Also, I declare the frame upon creation of it, rather than making it a field. So the result is I have eliminated all static fields.
    For academic sake, however, I still do not understand why using frame.removeAll() behaves the way it does (see reply 2 above).
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class FrameDemo extends JFrame
        implements ActionListener {
        JLabel label1;
        JLabel label2;
        JButton button1;
        JButton button2;
        JPanel panel1;
        JPanel panel2;
        FrameDemo(){};
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("quit")) {
                System.exit(0);
            //Button on frame 1 pressed, action event calls frameDisplay2()...
            if (e.getActionCommand().equals("openFrameTwo")) {
                panel1.removeAll();
                panel2.removeAll();
                label1 = new JLabel("Message for frame TWO");
                panel1.add(label1);
                panel1.revalidate();
                panel1.repaint();
                button2 = new JButton("quit");
                button2.setActionCommand("quit");
                button2.addActionListener(this);
                panel2.add(button2);
                panel2.revalidate();
                panel2.repaint();
        //code to set the frame size and location, etc HERE....
        //frame creating methods...
        public void frameDisplay1(){
            FrameDemo frame = new FrameDemo();
            panel1 = new JPanel();
            panel2 = new JPanel();
            label1 = new JLabel("Message for frame ONE");
            panel1.add(label1);
            button1 = new JButton("open frame two");
            button1.setActionCommand("openFrameTwo");
            button1.addActionListener(this);
            button2 = new JButton("quit");
            button2.setActionCommand("quit");
            button2.addActionListener(this);
            panel2.add(button1);
            panel2.add(button2);
            frame.getContentPane().add(panel1,BorderLayout.NORTH);
            frame.getContentPane().add(panel2,BorderLayout.CENTER);
            frame.pack();  //actually not used in my program; just in this demo
              frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        public static void main(String s[]) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    FrameDemo fd = new FrameDemo();
                    //fd.codeToSetThingsUp();
                    fd.frameDisplay1();      //display the first frame, and start the daisy chain...
    }

  • Compilation error while calling static method from another class

    Hi,
    I am new to Java Programming. I have written two class files Dummy1 and Dummy2.java in the same package Test.
    In Dummy1.java I have declared a static final variable and a static method as you can see it below.
    package Test;
    import java.io.*;
    public class Dummy1
    public static final int var1= 10;
    public static int varDisp(int var2)
    return(var1+var2);
    This is program is compiling fine.
    I have called the static method varDisp from the class Dummy2 and it is as follows
    package Test;
    import java.io.*;
    public class Dummy2
    public int var3=15;
    public int test=0;
    test+=Dummy1.varDisp(var3);
    and when i compile Dummy2.java, there is a compilation error <identifier > expected.
    Please help me in this program.

    public class Dummy2
    public int var3=15;
    public int test=0;
    test+=Dummy1.varDisp(var3);
    }test+=Dummy1.varDisplay(var3);
    must be in a method, it cannot just be out somewhere in the class!

  • How can I call a variable from another class

    hi
    If I have two classes : one and two
    In class two I have a variable called : action
    In class one I want to check what is the value of action.
    How can I call action?

    Thank you scorbett
    what you told me worked fine, but my problem is that MyClass2 is an application by itself that I don't want to be executed.
    Creating myClass2 as in the following:
    MyClass2 myClass2 = new MyClass2();
    [/code]
    executes myClass2.
    Can I prevent the exectuion of MyClass2, or is there another way to call the variable (action)?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • J2ME error to get value from variable in another class

    hi,,
    i write code for a mobile application like a tour guide..
    i had create 5 class for this application, KutaBeachDictionary MIDlet class, ListMainMenu List class, ListMenuHotel List class, HotelDes01 Form class and the last Hotel class that store all information.
    the problem is, i have [choose] variable in ListMenuHotel that contain getSelectedIndex() value, i want to take [choose] value from ListMenuHotel class to [index] variable in HotelDes01, so it determine the name of hotel that will display in form.but the index always 0 although i had choosen different menu from ListMenuHotel..
    here is all code
    in MIDlet class
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.midlet.MIDlet;
    public class KutaBeachDictionary extends MIDlet {
        public KutaBeachDictionary() {
            lstMenuUtama = new ListMenuUtama(this);
            lstMenuHotel = new ListMenuHotel(this);
            htlDes1 = new HotelDes01(this);
            frmLoading = new FormLoading(this);
        public Display getDisplay() {
            return Display.getDisplay(this);
        public void exitMIDlet() {
            switchDisplayable(null, null);
            destroyApp(true);
            notifyDestroyed();
        public void startApp() {
            if (midletPaused) {
                resumeMIDlet();
            } else {
                initialize();
                startMIDlet();
            midletPaused = false;
            getDisplay().setCurrent(frmLoading);
            new Thread(frmLoading).start();
            frmLoading.done = false;
            frmLoading.gLoading.setValue(0);
        public void pauseApp() {
            midletPaused = true;
        public void destroyApp(boolean unconditional) {
        private boolean midletPaused = false;
        public ListMenuHotel lstMenuHotel;
        public ListMenuRestaurant lstMenuRestaurant;
        ListMenuUtama lstMenuUtama;
        FormAbout frmAbout;
        public HotelDes01 htlDes1;
        FormLoading frmLoading;
    }the code in ListMenuHotel
    import java.io.IOException;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Image;
    import javax.microedition.lcdui.List;
    import javax.microedition.lcdui.Ticker;
    public class ListMenuHotel extends List implements CommandListener {
        Hotel objHotel = new Hotel();
        ListMenuHotel(KutaBeachDictionary run) {
            super("List Hotel", List.IMPLICIT);
            this.run = run;
            try {
                btnImage = Image.createImage("/btnImage.png");
            } catch (IOException e) {
            tickerMenu = new Ticker("Daftar Hotel di Pantai Kuta Bali");
            setTicker(tickerMenu);
            for (int i = 0; i < objHotel.namaHotels.length; i++) {
                append(objHotel.namaHotels, btnImage);
    addCommand(new Command("Select", Command.OK, 0));
    addCommand(new Command("Back", Command.BACK, 0));
    setCommandListener(this);
    public void commandAction(Command cmd, Displayable dsp) {
    if (cmd == SELECT_COMMAND) {
    choose = getSelectedIndex();
    Display.getDisplay(run).setCurrent(run.htlDes1);
    switch (cmd.getCommandType()) {
    case Command.BACK:
    Display.getDisplay(run).setCurrent(run.lstMenuUtama);
    break;
    public int getChoose() {return choose;}
    private KutaBeachDictionary run;
    private Image btnImage;
    private Ticker tickerMenu;
    private int choose;
    the code in HotelDes01 Form classimport java.io.IOException;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Image;
    import javax.microedition.lcdui.ImageItem;
    public class HotelDes01 extends Form implements CommandListener {
    private KutaBeachDictionary run;
    private int index;
    private Hotel isiHotel = new Hotel();
    public HotelDes01(KutaBeachDictionary run) {
    super("Inna Kuta Hotel");
    index = run.lstMenuHotel.getChoose(); //here is the problem..the value remain 0 althought i choose another??!!
    this.run = run;
    Image imgHotel = null;
    String namaHotel = "\n" + isiHotel.namaHotels[index] + "\n";
    try {
    imgHotel = Image.createImage("/HotelImage/interface/htlDes1.png");
    } catch (IOException e) {
    append(new ImageItem(null, imgHotel, ImageItem.LAYOUT_CENTER, null));
    append(namaHotel);
    addCommand(new Command("Back", Command.BACK, 0));
    setCommandListener(this);
    public void commandAction(Command cmd, Displayable dsp) {
    switch (cmd.getCommandType()) {
    case Command.BACK:
    Display.getDisplay(run).setCurrent(run.lstMenuHotel);
    break;
    can someone fix the code,, i had tried so many ways,,but completely failure.. T_T
    Edited by: diaca on Mar 22, 2010 7:55 AM
    Edited by: diaca on Mar 22, 2010 8:01 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    hei bro,i had solved the problem :)
    the problem is , i had constructed the form of hotelDes01 each time the program starting with this code
    public class KutaBeachDictionary extends MIDlet {
        public KutaBeachDictionary() {
            //.. declaring several new object
            htlDes1 = new HotelDes01(this);
        //..another procedure like startApp, pauseApp, etc
    public HotelDes01 htlDes1;
    }i think if i declare this class in the beginning it never updated the index value
    so i delete the code above, and i declare the htlDes1 in the listMenuHotel like this:
    public class ListMenuHotel extends List implements CommandListener {
        //..several code just like before
    public void commandAction(Command cmd, Displayable dsp) {
            if (cmd == cmdSelect) {
               objHotel.setChoose(getSelectedIndex()); //i change the code like this
               showInformation();
            if (cmd == cmdBack) {
                Display.getDisplay(run).setCurrent(run.lstMenuUtama);
        public void showInformation() {
            HotelDescription htlDes1 = new HotelDescription(run); //reconstruct the object in the class ListMenuHotel
            Display.getDisplay(run).setCurrent(htlDes1); //display the object
        }so the form of hotelDes01 now always reconstruct whenever user change the election in ListMenuHotel class and now index value get updated.. :D
    thanks for trying to fix the code qnat..
    this is my coursework, so i get to finish it ASAP..

Maybe you are looking for