Results 1 to 7 of 7
  1. #1
    syarizma is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    i have problem with my code.When i run it, the error Exception in thread "main" java.lang.NullPointerException in line 32 and 108.Plz, i really need help.



    /**
    * @(#)Binary_Image.java
    *
    *
    * @author
    * @version 1.00 2009/8/6
    */

    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class BinaryImage extends JApplet{


    /* this program convert color image to grayscale image*/
    Dimension d;
    Image img;
    int iw,ih;
    int pixels[];
    int w,h;
    int hist[] = new int[256];
    int max_hist = 0;
    int cnt1=0,cnt=0,cnt2=0,h1,h2,h3,c=0;

    public void init()


    {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int screenWidth = screenSize.width;
    int screenHeight = screenSize.height;

    try


    {
    //img = getImage(getDocumentBase(),getParameter("jawi.gif" ));
    //img = getImage(getDocumentBase(),("jawi.gif"));

    MediaTracker t = new MediaTracker(this);
    t.addImage(img,0);
    t.waitForID(0);

    //img = new Image(getClass().getResource("jawi.gif"));


    iw = img.getWidth(null);
    ih = img.getHeight(null);
    pixels = new int[iw*ih];
    PixelGrabber pg = new PixelGrabber(img,0,0,iw,ih,pixels,0,iw);
    pg.grabPixels();

    } catch(InterruptedException e) {};

    for(int i=0;i<iw*ih;i++)


    {
    int p = pixels[i];
    int r = (p>>16) & 0xff;
    int g = (p>>8) & 0xff;
    int b = (p) & 0xff;
    int k = (int)(.56*g + .33*r + .11 * b);
    //pixels[i] = (0xff000000 | k<<16 | k<<8 |k);
    cnt++;
    if(k<36)


    {
    cnt1++;
    pixels[i]=(255 << 24)|(0 << 16)|(0 << 8)|0;
    }

    else


    {
    cnt2++;
    pixels[i]=(255 << 24)|(255 << 16)|(255 << 8)|255;
    }
    }

    System.out.println("Total no of pixels ="+cnt);
    System.out.println("Total no of black pixels ="+cnt1);
    System.out.println("Total no of white pixels ="+cnt2);

    img = createImage(new MemoryImageSource(iw,ih,pixels,0,iw));
    }


    public void paint(Graphics g)


    {
    g.drawImage(img,0,0,this);
    }

    public static void main(String[]args)
    {
    JFrame frame = new JFrame ();

    Binary_Image applet = new Binary_Image();

    frame.add(applet,BorderLayout.CENTER);

    applet.init();

    frame.setSize(500,300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }


    }

  2. #2
    syarizma is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    Exception in thread "main" java.lang.NullPointerException
    at java.applet.Applet.getDocumentBase(Applet.java:158 )
    at Binary_Image.init(Binary_Image.java:32)
    at BinaryImage.main(BinaryImage.java:111)

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

    Default

    Hi,
    There are many loop holes in ur code.

    1. Here the class which extends JApplet is BinaryImage.But,you have instantiated as Binary_Image.How it got compiled? Are you having Binary_Image class outside?

    2.No need to call init method explicitly.As per lifecycle ,applet will call on its own.

    3.You have commented out the picture file.What u are going to acheive?
    Ramya:cool:

  4. #4
    syarizma is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hi,
    There are many loop holes in ur code.

    1. Here the class which extends JApplet is BinaryImage.But,you have instantiated as Binary_Image.How it got compiled? Are you having Binary_Image class outside?

    2.No need to call init method explicitly.As per lifecycle ,applet will call on its own.

    3.You have commented out the picture file.What u are going to acheive?
    thank a lots RamyaSivakanth..

    Actually, my class name is Binary_Image.I want get the width and height of the picture.So i'm try to load the picture using toolkit but still error.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    Can you use the code tags please?
    There's a '#' button at the top of the messgae box that'll do it...it keeps the code formatting then, and makes it easier to see what's going on.

    Also, where in the code is the NPE occurring? That is, point out the line to us.

  6. #6
    syarizma is offline Member
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    Sorry because of that, i'm not familiar with forum before this.The code can run properly but cannot load the image.


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.image.*;
    import java.awt.Image.*;



    public class Binary_Image extends JApplet{

    Image img;
    int iw,ih;
    int pixels[];
    int w,h;
    int hist[] = new int[256];
    int max_hist = 0;
    int cnt1=0,cnt=0,cnt2=0,h1,h2,h3,c=0;

    public void init()


    {


    try


    {


    img = Toolkit.getDefaultToolkit().getImage("jawi.gif");

    iw = img.getWidth(null);
    ih = img.getHeight(null);

    pixels = new int[iw*ih];
    PixelGrabber pg = new PixelGrabber(img,0,0,iw,ih,pixels,0,iw);
    pg.grabPixels();



    } catch(InterruptedException e) {};

    for(int i=0;i<iw*ih;i++)


    {
    int p = pixels[i];
    int r = (p>>16) & 0xff;
    int g = (p>>8) & 0xff;
    int b = (p) & 0xff;
    int k = (int)(.56*g + .33*r + .11 * b);
    //pixels[i] = (0xff000000 | k<<16 | k<<8 |k);
    cnt++;
    if(k<36)


    {
    cnt1++;
    pixels[i]=(255 << 24)|(0 << 16)|(0 << 8)|0;
    }

    else


    {
    cnt2++;
    pixels[i]=(255 << 24)|(255 << 16)|(255 << 8)|255;
    }
    }

    System.out.println("Total no of pixels ="+cnt);
    System.out.println("Total no of black pixels ="+cnt1);
    System.out.println("Total no of white pixels ="+cnt2);

    img = createImage(new MemoryImageSource(iw,ih,pixels,0,iw));
    }

    public static void main(String[]args)
    {
    JFrame frame = new JFrame ();

    Binary_Image applet = new Binary_Image();

    frame.add(applet,BorderLayout.CENTER);

    applet.init();

    frame.setSize(500,300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(false);
    }


    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    (Need to stick the formatted code into the tags by the way)

    What do you mean by "cannot load the image"?

Similar Threads

  1. Replies: 7
    Last Post: 04-18-2011, 02:24 PM
  2. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  3. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  4. Exception in thread "main" java.lang.NullPointerException
    By jyothi.priyanka in forum New To Java
    Replies: 11
    Last Post: 03-11-2009, 03:18 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Tags for this Thread

Posting Permissions

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