1 Replies - 1104 Views - Last Post: 12 July 2009 - 10:25 AM

#1 sandar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-July 09

using list

Post icon  Posted 11 July 2009 - 10:04 AM

Hello everybody,
I am writing a program that copy a file from a source to the desired destination.
But before that part, I encounter a problem and can not go forward.
When I select browse, the root diretories must be displayed in the list.
But I got the message
“Uncaught exception: java.lang.NullPointerException: 0”
I got that message when the program try to access the list.

What is the problem?
And what I have written is just a small part but I think the way I wrote is very complex. Does is it necessary to pass the static variable as I have written?
Please help me. Now I got headache and don’t know how to correct it.
//FileCopyMIDlet.java
package fileCopy;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;



public class FileCopyMIDlet extends MIDlet 
{
	static FileCopyMIDlet fcMid;
	FileCopyForm fcForm=new FileCopyForm("File Copy",this);
	//FileCopyForm fcForm=new FileCopyForm("File Copy");
	List objRoot = new List("Rootsssss",ChoiceGroup.IMPLICIT);
	
	
	
	
	public FileCopyMIDlet() 
	{
		objRoot.append("Text", null);
		// TODO Auto-generated constructor stub
	}

	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException 
	{
		Display disp=Display.getDisplay(this);
		disp.setCurrent(fcForm);
		
		

	}

}





//FileCopyForm.java
package fileCopy;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;

public class FileCopyForm extends Form
{
	static FileCopyForm fcForm;
	TextField tfSource=new TextField("Source","",30,TextField.ANY);
	TextField tfDestination=new TextField("Destination","",30,TextField.ANY);
	Command cmdBrowse=new Command("Browse",Command.SCREEN,1);
	Command cmdCopy=new Command("Copy",Command.SCREEN,1);
	
	//List objRoot = new List("Rootsssss",ChoiceGroup.IMPLICIT);

	

	public FileCopyForm(String title,FileCopyMIDlet fcMid) 
	{
		super(title);
		append(tfSource);
		append(tfDestination);
		
		addCommand(cmdBrowse);
		addCommand(cmdCopy);
		//setCommandListener(new CommandHandler(fcMid));
		setCommandListener(new CommandHandler(fcForm,fcMid));
		
	}	
}




//CommandHandler.java
package fileCopy;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;




public class CommandHandler implements CommandListener {
	
	FileCopyForm fcForm;
	FileCopyMIDlet fcMid;
	public CommandHandler(FileCopyForm fcForm,FileCopyMIDlet fcMid)
	{		
		this.fcForm=fcForm;
		this.fcMid=fcMid;
	}

	public void commandAction(Command c, Displayable d) 
	{
		FileThread objFileThread = null;
		//String cmd = c.getLabel();
		if(c.getLabel().equals("Browse"))			
		{
			System.out.println("inside List Roots");
			objFileThread = new FileThread(null,fcForm,fcMid);
		}	
		
		Thread objThread = new Thread(objFileThread);
		objThread.start();
	}

}




//FileThread.java
package fileCopy;

import java.util.Enumeration;

import javax.microedition.io.file.FileSystemRegistry;

public class FileThread implements Runnable
{
String root;
FileCopyForm fcForm;
FileCopyMIDlet fcMid;
	
	public FileThread(String root,FileCopyForm fcForm,FileCopyMIDlet fcMid)
	{
		this.root = root;
		this.fcForm=fcForm;
		this.fcMid= fcMid;
	}
	public void run() 
	{
		//FileCopyMIDlet.fcMid.objRoot.deleteAll();
		System.out.println("*****");
		if(root == null)
		{
			
			System.out.println("*****");
			getRoots( );
		}

}
	private void getRoots() 
	{
		Enumeration en = FileSystemRegistry.listRoots();
		while (en.hasMoreElements())
		{
			System.out.println("inside loop");
			String root=(String)en.nextElement();
			System.out.println("*****");
			System.out.println(root);
			
			FileCopyMIDlet.fcMid.objRoot.append(root, null);
			
		}	
		
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: using list

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: using list

Posted 12 July 2009 - 10:25 AM

Hi, Welcome to </dream.in.code> :)

Quote

But I got the message “Uncaught exception: java.lang.NullPointerException: 0”I got that message when the program try to access the list.

Which line would that be? I just glanced through your code so it's quite probable that I skipped that line.

Quote

Does is it necessary to pass the static variable as I have written?

You need to pass a reference of the MIDlet object to objects belonging to other classes that deal with displaying data on the screen.

As for the objects of other classes, don't pass references. Either make it public or add public get and set functions which can be accessed outside the class.

Either way, you're not doing it right. You don't need to pass a static object so there's no need to create an object of your class within its definition.

Instead, just pass the this pointer to functions that require a MIDlet object like this:
//For this function:
public FileThread(String root, FileCopyMIDlet fcMid)
	{
		this.root = root;
		this.fcMid= fcMid;
	}

//pass your parameters like this from a function in the MIDlet's class:
FileThread ft = new FileThread("c:\", this);



Hope this helps. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1