aspose file tools*
The moose likes Java Micro Edition and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

vishal mulik
Greenhorn

Joined: Oct 26, 2007
Posts: 2
hi everbody,
i am jsut begineer for J2me programming and when i write this program it generate following error. please help me, if anybody know about that
After proper compilation, it generate NullPointerException at run time?
package CreateN;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class CreateForm extends MIDlet implements CommandListener
{
//diclaration
private Display display;
private TextBox textbox;
private Command exit;
private Form form;

public CreateForm()
{

try
{
display=Display.getDisplay(this);
exit=new Command("Quit",Command.SCREEN,1);
StringItem Messages[]=new StringItem[5];
System.out.println("In TRy blok");
Message[0]=new StringItem("I m in First Message","Good Morning");
Message[1]=new StringItem("I m In Second Message","Good Afternoon");
form=new Form(" Form menu", Messages);
System.out.println("After Form");
form.addCommand(exit);
//form.append(Messages);
//form.append(Messages);
form.setCommandListener(this);
}
catch(java.lang.NullPointerException ee){
//System.printStackTrace(ee);
}

}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean bb)
{

}
public void commandAction(Command qcommand, Displayable dis)
{
if(qcommand ==exit)
{
destroyApp(false);
notifyDestroyed();
}
}
Jitendra Takalkar
Greenhorn

Joined: Jan 12, 2007
Posts: 9
Hi,

Please try with following code ....



package com.capgemini.midlet;

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.StringItem;
import javax.microedition.midlet.MIDlet;

public class CreateForm extends MIDlet implements CommandListener {

// diclaration
private Display display;

private Command exit;

private Form form;

public CreateForm() {

try {
display = Display.getDisplay(this);
exit = new Command("Quit", Command.SCREEN, 1);
StringItem Messages[] = new StringItem[2];
System.out.println("In TRy blok");
Messages[0] = new StringItem("I m in First Message", "Good Morning");
Messages[1] = new StringItem("I m In Second Message","Good Afternoon");
form = new Form(" Form Menu", Messages);
//form.append(new StringItem("I m in First Message", "Good Morning"));
//form.append(new StringItem("I m in Second Message", "Good Morning"));
System.out.println("After Form");
form.addCommand(exit);
//form.append(Messages);
//form.append(Messages);
form.setCommandListener(this);
} catch (java.lang.NullPointerException ee) {
ee.printStackTrace();
}

}

public void startApp() {
display.setCurrent(form);
}

public void pauseApp() {
}

public void destroyApp(boolean bb) {

}

public void commandAction(Command qcommand, Displayable dis) {
if (qcommand == exit) {
destroyApp(false);
notifyDestroyed();
}
}

}
Sukitha R Udugamasooriya
Greenhorn

Joined: Jul 27, 2007
Posts: 14
Hey Vishal,

What Jithendra posted is working properly. NullPointer Exception comes because of the StringItem array construction. Your have constructed your array for 5 elements and you have initialized only two. Exception comes because of the other three null elements. (When you try to put it to the Form)

***StringItem Messages[] = new StringItem[2];***

All the best


Sukitha Udugamsooriya<br />(SCJP,SCMAD,SCWCD,(SCBCD-final touch))
bakiyalakshmi dhanraj
Greenhorn

Joined: Nov 02, 2007
Posts: 24
for 2 reason nullpointerexception occur
1.If that element does not refer to a real object
2.If the image does not exists means...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException