Results 1 to 5 of 5
  1. #1
    phancuong87 is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default NullPointerException help me!

    I have proplem in code here, please help me!


    import java.io.InputStream;
    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Image;
    import javax.microedition.midlet.MIDlet;

    public class ImageLoaderMIDlet extends MIDlet implements Runnable {

    showCanvas gui;
    Thread t;
    Display display;
    Image img;

    public void startApp() {
    display = Display.getDisplay(this);
    gui = new showCanvas(this);
    display.setCurrent(gui);



    }
    public void threadStart(){
    t = new Thread(this);
    t.start();
    }


    public void run() {
    try {
    String URL = "http://www.unwiredview.com/wp-content/uploads/2009/10/Samsung-WeatherBug-weather-widget-2.jpg";
    gui.setImg(loadImage(URL));
    } catch (IOException ioe) {
    }

    }

    public Image loadImage(String url) throws IOException {
    HttpConnection hpc = null;
    DataInputStream dis = null;
    try {
    hpc = (HttpConnection) Connector.open(url);
    InputStream is = hpc.openInputStream();
    Image img = Image.createImage(is);
    return img;
    } finally {
    if (hpc != null) {
    hpc.close();
    }
    if (dis != null) {
    dis.close();
    }
    }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
    }

    class showCanvas extends Canvas {

    ImageLoaderMIDlet e;
    Image img;

    public showCanvas(ImageLoaderMIDlet athis) {
    e = athis;
    }

    public void setImg(Image athis) {
    img = athis;
    }

    protected void paint(Graphics g) {
    e.threadStart();
    g.drawImage(img, -70, 0, Graphics.LEFT | Graphics.TOP);
    }
    }

    thank!

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    put printStackTrace and find out the error in the line
    Ramya:cool:

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default

    Exactly. You are simply eating an exception here, for example:

    Java Code:
    try {
        String URL = "http://www.unwiredview.com/wp-content/uploads/2009/10/Samsung-WeatherBug-weather-widget-2.jpg";
        gui.setImg(loadImage(URL));
    } catch (IOException ioe) {
    [B]Put something in here (ie printStackTrace()) so you at least see the exception when it happens.[/B]
    }

  4. #4
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    Image img;

    no where in your code img does have a reference to any image obj.
    img is null.
    Give it some reference and everything works fine.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default

    Image is only not being set because (I suspect) there was an earlier exception in the run() method, preventing the gui.setImg() call working. Hence pointing out that they're eating exceptions.

Similar Threads

  1. NullPointerException: I can't get rid of it.
    By mcashe in forum AWT / Swing
    Replies: 2
    Last Post: 08-17-2009, 09:16 PM
  2. NullPointerException
    By Aika in forum New To Java
    Replies: 8
    Last Post: 11-18-2008, 11:34 PM
  3. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-03-2008, 11:19 PM
  4. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 04:20 PM
  5. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •