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 8 of 8

Thread: help me out from null pointer exception!!!

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help me out from null pointer exception!!!

     
    import java.awt.event.*;
    import java.awt.*;
     
    class ExceptionGui extends Frame implements ActionListener,WindowListener,TextListener
    { 
      int numerator,denominator,result;
      Button b;
      TextField tf1,tf2;
      Label l1,l2,l3;
     
     
     public ExceptionGui()
     { 
        setLayout(new FlowLayout());
        setSize(300,250);
        Button b=new Button("enter");
        Label l1=new Label("enter numerator");
        Label l2=new Label("enter denominator");
        Label l3=new Label();
        TextField tf1=new TextField(15);
        TextField tf2=new TextField(15);
        add(l1);add(tf1);
        add(l2);add(tf2);
        add(b);add(l3);
        b.addActionListener(this);
         tf1.addTextListener(this);
         tf2.addTextListener(this);    
        addWindowListener(this);
        setVisible(true);
     
     
     
      }
      public void textValueChanged(TextEvent te)
      {
        if(te.getSource()==tf1)
                                             //got an Exception over here  
         {
           String x=tf1.getText();
            numerator=Integer.parseInt(x);
          }
       else
         {
           String y=tf2.getText();
           denominator=Integer.parseInt(y);
          }
      }
     
      public void actionPerformed(ActionEvent ae)
      {   
     
        result=numerator/denominator;
        l3.setText(String.valueOf(result));
      }
     public void windowOpened(java.awt.event.WindowEvent we){}
     public void windowClosing(java.awt.event.WindowEvent we){ System.exit(0);
     }
     public void windowClosed(java.awt.event.WindowEvent we){}
     
     public void windowIconified(java.awt.event.WindowEvent we){}
     public void windowDeiconified(java.awt.event.WindowEvent we){}
     public void windowActivated(java.awt.event.WindowEvent we){}
     public void windowDeactivated(java.awt.event.WindowEvent we){}
    }
    class Exception
    {
      public static void main(String args[])
      {
       ExceptionGui ex=new ExceptionGui();
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help me out from null pointer exception!!!

    You forgot to copy and paste here the full text of the error message.
    Look at the statement where the error happened, find what variable had the null value and then backtrack in the code to see why that variable did not have a valid value.

    Make sure the code does not shadow class variables with local variables.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me out from null pointer exception!!!

    can u pls check it out now

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: help me out from null pointer exception!!!

    Norm has already told you what's wrong. Reread his reply. And the words are you and please. We're not limited to 160 characters in here.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me out from null pointer exception!!!

    Quote Originally Posted by Norm View Post
    You forgot to copy and paste here the full text of the error message.
    Look at the statement where the error happened, find what variable had the null value and then backtrack in the code to see why that variable did not have a valid value.

    Make sure the code does not shadow class variables with local variables.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    @Norm i have wrapped it.Can u pls check whats wrong with the code!!
    i am unable to solve exception from past 1hr..... pls help me out

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help me out from null pointer exception!!!

    Copy the full text of the error message and paste it here. It has important info about the problem.

    Make sure there are not variable declarations in a method or constructor that are shadowing class variables.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me out from null pointer exception!!!

    here is the exception i get when i try to insert number(integer) into the text field(both tf1 and tf2)

    *************ERROR************************
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ExceptionGui.textValueChanged(Exception.java:45)
    at java.awt.TextComponent.processTextEvent(TextCompon ent.java:703)
    at java.awt.TextComponent.processEvent(TextComponent. java:672)
    at java.awt.TextField.processEvent(TextField.java:588 )
    at java.awt.Component.dispatchEventImpl(Component.jav a:4860)
    at java.awt.Component.dispatchEvent(Component.java:46 86)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101 )
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)
    ************************************************** ***************************
    Attached Images Attached Images

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help me out from null pointer exception!!!

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ExceptionGui.textValueChanged(Exception.java:45)
    There is a variable with a null value on line 45. Look at line 45 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 45 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need Help with Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 23rd, 2012, 02:20 PM
  2. Help with null pointer exception
    By Dr.HughMan in forum Exceptions
    Replies: 35
    Last Post: November 30th, 2011, 09:00 PM
  3. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM
  4. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM