Hello All,

I use create image method: createImage(byte[] bytes, int offset, int len). but that method does not works. here is my code:
Code:
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
//import javax.microedition.lcdui.Image;
import com.sun.lwuit.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class Midlet extends MIDlet {

	TestForm form;
	public byte[] imgBytes = null;
	ByteArrayOutputStream baos = null;
	InputStream in = null;
	FileConnection file = null;
	private Display display;
	
	public Midlet() {
		System.out.println("Midlet running");
	}

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

	}

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

	}

	protected void startApp() throws MIDletStateChangeException {
//		Display.init(this);
//		form = new TestForm();
//		form.show();
		
		display = Display.getDisplay(this);
		byte[] arrByte = loadFromApp();
		System.out.println("byte array: "+arrByte.length);
		System.out.println("imgBytes: "+arrByte);
		in = this.getClass().getResourceAsStream("/logo.png");
		Image imgLogo = Image.createImage(arrByte, (int)0, (int)arrByte.length);
		System.out.println("imgLogo array: "+imgLogo);
		
//		Form A = new Form("ByteArray Image");
//		A.append(imgLogo);
//		display.setCurrent(A);
	}

	/**
	 * Reads the local image resource and returns an array of bytes representing
	 * the image
	 * 
	 * @param imgPath
	 *            path to the image inside the app
	 * @return array of bytes representing the image
	 */
	public byte[] loadFromApp()
	{
		StringBuffer sb = new StringBuffer();
		
		try {
			baos = new ByteArrayOutputStream();
			//this image is inside my mobile application
			in = this.getClass().getResourceAsStream("/logo.png");
			//byte[] buffer = new byte[1024];
			int n = 0;
			
			while ((n = in.read()) != -1) {
				//System.out.println("ayam");
				sb.append((char)n);
			}
			
			imgBytes = sb.toString().getBytes();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//whatever happends close the streams
			if (baos != null) {
				try {
					baos.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			
			if(in != null)
			{
				try {
					in.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}
		
		return imgBytes;
	}
}
My goal is to convert the image into bytearray and then create image from bytearray. But the above code produce NullPointerException when create image. Is this a bug from lwuit Image..??. Because i used Image class from lcdui, it's working perfectly.




Best Regards,
Iqbal