Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Java run time error: 'java.lang.NullPointerException"

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java run time error: 'java.lang.NullPointerException"

    Hi everyone. I am a newbie and a working on a mini project. Need Help.

    a)While running one of my program, I encountered this error (marked in red)
    b) As per output problem lies in line number 149, I switched to same but could not trace the error
    even if i comment the same line ,the error is still

    C:\Program Files\Java\jdk1.5.0_06\bin>javac dak.java

    C:\Program Files\Java\jdk1.5.0_06\bin>java dak
    Exception in thread "main" java.lang.NullPointerException
    at dak1.<init>(dak.java:149)
    at dak.main(dak.java:515)
    Last edited by ChandanSharma; June 21st, 2011 at 11:49 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Java run time error: 'java.lang.NullPointerException"

    Look at the code at the line where the NullPointerException occurs. What variable on that line is null?
    If you can't see which one, add a println statement just before the line where the exception occurs and print out the values of all the variables used on the line with the error.
    When you see which variable is null, back track in your code to find out why that variable has the value null.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Java run time error: 'java.lang.NullPointerException"

    I think a moderator should sticky a null pointer exception topic. there must be about 1 quater of this forum of them lol. if im not wrong to solve the problem you solve it from the bottom up am i correct

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Java run time error: 'java.lang.NullPointerException"

    The stack trace is a push down stack with the youngest on top. The location of the error as at the top, the path to it is below.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java run time error: 'java.lang.NullPointerException"

    This line contains font "ok.setFont(o)"
    1)Where
    "ok" is a button on 1 of the frames .
    2)o is object of font class
    "Font o=new Font("Lucida Calligraphy", Font.BOLD,21);"
    3)even after commenting the line error prevails.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Java run time error: 'java.lang.NullPointerException"

    Please post the full text of the error message and the source lines where it occurs. Your last post does not show us what we need to know. We must see the code. Your interpretations of what is happening could miss some critical point.
    Add code to print out all the variables used on the line with the error to show which is null.
    Add this print out before the line with the error.

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java run time error: 'java.lang.NullPointerException"

    Thanks
    i looked at the variables.i got the mistake,was some simple error,i created the object of 'font' but didn't initialize it carelessly using new due to which 'ok' contained null.

    1)if i have to place some button,label etc. on frame on an already existing image on frame using ImageIcon then only the latter exists, either image or buttons.i wasn't able to place buttons etc on background image as previous of the 2 is invisible.

    2)used ImagePanel also.is there some way for sorting out the same.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Java run time error: 'java.lang.NullPointerException"

    Can you write a small simple program that shows what you are trying to do:
    Have an image as background with other components laid out over the image.

  9. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java run time error: 'java.lang.NullPointerException"

    //small program for displaying buttons etc. on image existing on frame

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

    class javalang extends JFrame
    {
    JLabel l1,l2,l3;
    JPanel p;
    javalang(String g)
    {
    super(g);
    l2=new JLabel("hello");
    l3=new JLabel("java");
    Icon i=new ImageIcon("Sunset.JPG");
    l1=new JLabel(i);
    p.setLayout(new FlowLayout());
    p.setBackground(Color.gray);
    p.add(l1);
    p.add(l2);
    p.add(l3);
    add(p);
    }

    class javalang
    {
    public static void main(String args[])
    {

    javalang j=new javalang("Introductory Page");
    j.setVisible(true);
    j.setSize(500,500);

    }
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,619
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Java run time error: 'java.lang.NullPointerException"

    Your code does not execute. NPE because p is null

    Please post your code in code tags. See: BB Code List - Java Forums

    Google: java button overlay image background
    for code samples
    Last edited by Norm; June 20th, 2011 at 01:15 PM.

Similar Threads

  1. Replies: 7
    Last Post: May 30th, 2011, 09:11 AM
  2. Exception in thread "main" java.lang.NullPointerException
    By isun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 09:22 AM
  3. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  4. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 01:25 AM
  5. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM