How to set TextArea after getting it's value from CGI script
Hi,
I hope you all will be fine. Actually when user press send command on form2, a method called that get value from CGI script. When i try to set it's value like
Code:
txtArea2.settext(b.toString());
i got null pointer exception. If i use System.out.println it worked means the value is printing on console but i want the value comes in txtArea2 on form2. Here is the code
Code:
form1.addComponent(sms);
form1.addComponent(internet);
form1.addCommand(cmdSelect);
form1.addCommand(cmdExit);
form1.addCommandListener(this);
form1.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 1000));
//Setup Form 2
form2 = new Form("Form 2");
//set Form layout
form2.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
//Create container that holds component and set its layout
// Container textBoxContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
//create text Area
txtArea1 = new TextArea("", 2, 10, TextArea.ANY);
txtArea1.setEditable(true);
txtArea1.setGrowByContent(true);
txtArea1.getStyle().setBorder(Border.createLineBorder(2));
txtArea1.setFocusable(true);
//create text Area2
txtArea2 = new TextArea(2, 5, TextArea.ANY);
txtArea2.setEditable(false);
form2.addCommand(cmdSend);
form2.addCommand(cmdBack);
form2.addCommandListener(this);
form2.addComponent(txtArea1);
form2.addComponent(txtArea2);
form2.setTransitionInAnimator(Transition3D.createCube(1000, true));
form1.show();
}
public void actionPerformed(ActionEvent evt) {
if ((sms.isEnabled()) && (sms.hasFocus()) ){
form2.show();
}
//check which command cliked
if (evt.getCommand()==cmdExit) {
notifyDestroyed();
//} else if (evt.getCommand()==cmdSelect) {
//} else if ( (sms.isEnabled()) || (((evt.getCommand()==cmdSelect) && (sms.hasFocus()))) ) {
} else if ((((evt.getCommand()==cmdSelect) && (sms.hasFocus()))) ) {
form2.show();
} else if (evt.getCommand()==cmdBack) {
form1.show();
} else if (evt.getCommand()==cmdSend) {
//form2.setFocusable(true);
String text = txtArea1.getText();
System.out.println("txtArea1 contain this text: " + text);
try{
//new SendDataToCGI().getData();
getGrade("http://10.120.10.24/cgi-bin/http.cgi?cmd=baasit");
} catch(IOException e){
e.printStackTrace();
}
}
}
void getGrade(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
StringBuffer b = new StringBuffer();
//TextBox t = null;
try {
c = (HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty("CONTENT-TYPE","application/x-www-form-urlencoded");
c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-CA");
os = c.openOutputStream();
// send input
String str = "name=182016";
byte postmsg[] = str.getBytes();
for(int i=0;i<postmsg.length;i++) {
os.write(postmsg[i]);
}
os.flush();
is = c.openDataInputStream();
int ch;
// receive output
while ((ch = is.read()) != -1) {
b.append((char) ch);
System.out.println((char)ch);
}
System.out.println("after while loo: " + b.toString());
txtArea2.setText("Hi am good");
//form2.addComponent(new Label(b.toString()));
//form2.show();
// t = new TextBox("Final Grades", b.toString(), 1024, 0);
// form2.addComponent(t);
} finally {
if(is!= null) {
is.close();
}
if(os != null) {
os.close();
}
if(c != null) {
c.close();
}
}
// form2.show();
//display.setCurrent(t);
}
Here is the exception
Code:
I am in ChooseOptions2
a
t
i
f
r
e
h
m
a
n
after while loo: atif rehman
TRACE: <at java.lang.NullPointerException: 0>, startApp threw an Exception
java.lang.NullPointerException: 0
at ChooseOptions2.getGrade(ChooseOptions2.java:371)
at ChooseOptions2.startApp(ChooseOptions2.java:63)
at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(), bci=1
at com.sun.midp.midlet.MIDletPeer.startApp(), bci=7
at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=269
at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52
at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8
at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161
at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
java.lang.NullPointerException: 0
at ChooseOptions2.getGrade(ChooseOptions2.java:371)
at ChooseOptions2.startApp(ChooseOptions2.java:63)
at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(), bci=1
at com.sun.midp.midlet.MIDletPeer.startApp(), bci=7
at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=269
at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52
at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8
at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161
at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
when i use System.out.println, the code works fine but form1 is shown after code runs. I want that form2 remain there and value is shown in txtArea2.
How can i do this please guide me
Thanks