944,205 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4592
  • Java RSS
Sep 11th, 2007
0

Multi class multi form j2me app getting NullPointerException

Expand Post »
Please help!!

I am creating this midlet (non vital coding excluded) but I feel like a noob!


here is the code:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.*;

public class MainMenu
	extends MIDlet
	implements CommandListener
{
	public Display mDisplay;
	private List mlstMainMenu;
	private Calculator mobjCalculator;
	private Command mcmdOK;



	public MainMenu()
	{

		//create Items
		String[] strMenuItems = {"Calculator", "Other Stuff"};
		mlstMainMenu = new List("Main Menu", List.IMPLICIT, strMenuItems, null);


		//create commands
		mcmdOK = new Command("Ok", Command.SCREEN, 0);
		Command cmdExit = new Command("Exit", Command.EXIT, 1);

		//add commands to the form
		mlstMainMenu.addCommand(mcmdOK);
		mlstMainMenu.addCommand(cmdExit);
		mlstMainMenu.setCommandListener(this);

		mobjCalculator = new Calculator(mDisplay);


	}


	public void startApp()
	{
		if (mDisplay == null) mDisplay = Display.getDisplay(this);
		mDisplay.setCurrent(mlstMainMenu);

	}

	public void pauseApp() { }
	public void destroyApp(boolean unconditional) { }

	public void commandAction(Command c, Displayable s)
	{
		if (c.getCommandType() == Command.EXIT)
			notifyDestroyed();

		else if(c == List.SELECT_COMMAND || c == mcmdOK)
		{
			int index = mlstMainMenu.getSelectedIndex();
			if (mlstMainMenu.getString(index).equals("Calculator"))
			{
				mDisplay.setCurrent(mobjCalculator.ShowCalculator(mDisplay.getCurrent()));
				//mlstMainMenu.set(index, "WORKED!", null);
			}
		}


	}
}

public class Calculator
	implements CommandListener
{
	private Form mfrmCalc;
	private Command mcmdClear, mcmdCalculate, mcmdMainMenu;
	private StringItem mstrPoints;
	private Spacer mSpacer;
	private Display mDisplay;
	private Displayable mParent;


	public Calculator(Display objDisplay)
	{
		mDisplay = objDisplay;

		//create forms
		mfrmCalc = new Form("Total Points:  ");


		//create commands
		mcmdClear = new Command("Clear", Command.SCREEN, 1);
		mcmdCalculate = new Command("Calc", "Calculate", Command.SCREEN, 0);
		Command cmdExit = new Command("Exit", Command.EXIT, 1);

		//add commands to the form
		mfrmCalc.addCommand(mcmdClear);
		mfrmCalc.addCommand(mcmdCalculate);
		mfrmCalc.addCommand(cmdExit);

		mfrmCalc.setCommandListener(this);

	}

	public Form ShowThisForm(Displayable objParent)
	{
		mParent = objParent;
		//mDisplay.setCurrent(mfrmCalc);
		return mfrmCalc;
	}


	public void commandAction(Command c, Displayable s)
	{
		if (c.getCommandType() == Command.EXIT)
		{
			try
			{
				mDisplay.setCurrent(mParent);
			}		catch (NullPointerException npe)
			{
				String strTitle =  mParent.getTitle();
				mfrmCalc.setTitle(strTitle);

			}	
		}

	}
}

This works perfectly to open the calculator form and the functionality on the calculator works perfectly. but once in the calculator form and the Exit command is clicked, I get NullPointerException error. Please help!
Last edited by chef.boyardy; Sep 11th, 2007 at 9:55 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chef.boyardy is offline Offline
2 posts
since Sep 2007
Sep 12th, 2007
0

Re: Multi class multi form j2me app getting NullPointerException

the ShowThis Form method should be named ShowCalculator. I changed it for posting purposes
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chef.boyardy is offline Offline
2 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help with SQL embedded in Java
Next Thread in Java Forum Timeline: Else If statement again. ~.~





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC