Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}

}
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for 2 reason nullpointerexception occur
1.If that element does not refer to a real object
2.If the image does not exists means...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic