2

I'm working in java me, I'm trying to switch between visual designs using ok Commands and back Commands. I have a form displayable which I named formA in my main class A.java and a formB in another class B.java. I used an ok Command in formA which on selection, is supposed to take the user to formB. At first when I tried to call the getFormB method from my main class, it didn't work(non-static method cannot be referenced from a static context).

I was adviced to create a reference to B.java so I added this line in my main class A.java

B b = new B(this);

now I could call the getFormB method from my commandAction in formA. At this point, everything worked well but when I tried to add a backCommand which is supposed to take me back to formA in A.java I get ds error again 'non-static method getFormA() cannot be referenced from a static context', so I tried creating a reference in B.java same way I did in A.java, here's the code:

A a = new A(); 

then in command action I did ds on the backCommand:

switchDisplayable(null, a.getFormA()); 

This time, it compiled without errors. But at runtime I get a SecurityException MIDlet Manager ERROR:

Illegal attempt to construct hello.A@e938beb1

'hello' is the package containing both java files.

Can anyone help me out?

laxonline
  • 2,657
  • 1
  • 20
  • 37
degee
  • 173
  • 2
  • 11

1 Answers1

2

It seems that your A class extends MIDlet. If this is the case you should not try to make a new instance of it.
You should add an A attribute to your B class and receive the instance as a constructor parameter or have a setter method.
With this you can call the getFormA() method from the attribute.
Update

public class A extends MIDlet {
  B b;

  public A() {
    b = new B(this);
  }
}

class B {
  A a;

  B(A a) {
    this.a = a;
  }

  public void commandAction(Command c, Displayable d) {
    switchDisplayable(null, a.getFormA());
  }
}
Telmo Pimentel Mota
  • 4,033
  • 16
  • 22
  • Yes the A class extends MIDlet. Can I get a code sample on adding the attribute to the B class pls, and also how to use the setter method pls, so I get an idea how to implement those in code – degee Jan 21 '13 at 15:48
  • It compiled, no errors. But I get a NullPointerException at runtime. Why does that happen and how do I fix it pls – degee Jan 22 '13 at 05:32
  • Do you have a stack trace of the exception? – Telmo Pimentel Mota Jan 23 '13 at 11:07
  • Just got to know about stack trace now. I got sth lyk ds 4rm the output window in my IDE, I hope I'm in line. java.lang.NullPointerException at B.getDisplay(B.java:137) at B.switchDisplayable(B.java:65) at B.commandAction(B.java:106) – degee Jan 23 '13 at 11:36
  • Sorry I typed that from my mobile. The browser doesn't allow me to proper indent and arrange my lines. The word "at" starts every line – degee Jan 23 '13 at 11:43
  • Did you call Display.getDisplay(a) inside switchDisplayable? – Telmo Pimentel Mota Jan 23 '13 at 20:22