Re: read and show CSV-files
Tobias Wendorff wrote:
Hi Jeff,
I'm getting this in J2ME:
java.lang.InstantiationException: Class not a MIDlet
at com.sun.midp.midlet.MIDletState.createMIDlet(+66)
at com.sun.midp.midlet.Selector.run(+22)
How can I fix it?
Best,
Tobias
Hi Tobias,
First let me thank you for providing me the
impetus for looking at J2ME, your OP was the
first that I've examined it - having fun!
I don't know - have you extended the MIDlet class?
Here's what I have so far - please don't use this
as a good example.
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.ConnectionNotFoundException;
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.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ReadDisplayFile extends MIDlet
implements CommandListener, ItemCommandListener{
private Form mMainForm;
private Command cmNext =
new Command("Next", Command.SCREEN, 1);
private Command cmExit =
new Command("Exit", Command.EXIT, 1);
private Command cmDial =
new Command("Dial", Command.ITEM, 1);
private StringItem siFirstname =
new StringItem(null,null);
private StringItem siSurname =
new StringItem(null,null);
private StringItem siStreet =
new StringItem(null,null);
private StringItem siPhone =
new StringItem(null,null);
private int nextRecord = 0;
private int recordCount = 0;
private String[][] records;
public ReadDisplayFile() {
records = splitFile();
mMainForm = new Form("Phonebook");
siFirstname.setText("Firstname: " +
records[nextRecord][1] + "\n");
siSurname.setText("Surname: " +
records[nextRecord][0] + "\n");
siStreet.setText("Street: " +
records[nextRecord][2] + "\n");
siPhone.setText("Phone: " +
records[nextRecord][3] + "\n");
siPhone.addCommand(cmDial);
siPhone.setItemCommandListener(this);
nextRecord++;
mMainForm.append(siFirstname);
mMainForm.append(siSurname);
mMainForm.append(siStreet);
mMainForm.append(siPhone);
mMainForm.addCommand(cmNext);
mMainForm.addCommand(cmExit);
mMainForm.setCommandListener(this);
}
protected void destroyApp(boolean arg0)
throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void startApp()
throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(mMainForm);
}
public void commandAction(Command c, Displayable arg1)
{
if (c == cmNext)
{
if(nextRecord <= recordCount)
{
siFirstname.setText("Firstname: " +
records[nextRecord][1] + "\n");
siSurname.setText("Surname: " +
records[nextRecord][0] + "\n");
siStreet.setText("Street: " +
records[nextRecord][2] + "\n");
siPhone.setText("Phone: " +
records[nextRecord][3] + "\n");
nextRecord++;
}
else
{
nextRecord = 0;
siFirstname.setText("Firstname: " +
records[nextRecord][1] + "\n");
siSurname.setText("Surname: " +
records[nextRecord][0] + "\n");
siStreet.setText("Street: " +
records[nextRecord][2] + "\n");
siPhone.setText("Phone: " +
records[nextRecord][3] + "\n");
nextRecord++;
}
}
else if (c == cmExit)
{
notifyDestroyed();
}
}
private String[][] splitFile()
{
InputStream is =
getClass().getResourceAsStream("records.txt");
try
{
StringBuffer sb = new StringBuffer();
int chr = 0;
while ((chr = is.read()) != -1)
{
sb.append((char) chr);
}
is.close();
String[] data = split(sb.toString(),"\r\n");
String[][] records = new String[data.length][4];
for(int recordIndex = 0;
recordIndex < data.length;
recordIndex++)
{
for(int fieldIndex = 0;
fieldIndex < 4;
fieldIndex++)
{
records[recordIndex] = split(data[recordIndex],";");
}
recordCount = recordIndex;
}
return records;
}
catch (IOException e)
{
System.err.print(e.toString());
}
return null;
}
static private String[] split(String original, String separator)
{
Vector nodes = new Vector();
int index = original.indexOf(separator);
while(index>=0)
{
nodes.addElement( original.substring(0, index) );
original = original.substring(index+separator.length());
index = original.indexOf(separator);
}
nodes.addElement( original );
String[] result = new String[ nodes.size() ];
if( nodes.size()>0 )
{
for(int loop=0; loop<nodes.size(); loop++)
result[loop] = (String)nodes.elementAt(loop);
}
return result;
}
public void commandAction(Command c, Item arg1) {
if(c == cmDial)
{
try {
platformRequest("tel:<1-(555)555-5555>");
} catch (ConnectionNotFoundException e) {
e.printStackTrace();
}
}
}
}