Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree8Likes

Thread: "error: cannot find symbol e"

  1. #1
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Question "error: cannot find symbol e"

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    public class act extends Applet implements ActionListener
    {
    Label l,l1,l2;
    TextField t,t1,t2;
    Button b,b1;


    public void init(){


    Label l=new Label("enter 1st number");
    TextField t=new TextField(20);

    Label l1=new Label("enter 2nd number");
    TextField t1=new TextField(20);

    Label l2=new Label("result");
    TextField t2=new TextField(20);

    Button b=new Button("add");
    Button b1=new Button("sub");

    add(l);
    add(t);
    add(l1);
    add(t1);
    add(l2);
    add(t2);

    add(b);
    add(b1);

    b.addActionListener(this);
    b1.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){

    int a=Integer.parseInt(t.getText());

    int b=Integer.parseInt(t1.getText());
    int c=0;

    if(e.getSource==b){
    c=a+b;
    }
    if(e.getSource==b1){

    c=a-b;

    }
    t2.setText(String.valueOf(c));

    }

    }

    /* <applet code=act height=300 width=300> </applet> */

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: "error: cannot find symbol e"

    getSource() is a method, not a variable.

    kind regards,

    Jos
    adiotrox likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: "error: cannot find symbol e"

    Thanks Jos and now to actually perform addition and substraction what else is required...?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: "error: cannot find symbol e"

    Quote Originally Posted by adiotrox View Post
    Thanks Jos and now to actually perform addition and substraction what else is required...?
    Check if the JTextField(s) really contain a text representation of a valid number?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: "error: cannot find symbol e"

    what do you mean by "Check if the JTextField(s) really contain a text representation of a valid number?" and what else is needed at the above code to perform add and sub operations?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: "error: cannot find symbol e"

    Quote Originally Posted by adiotrox View Post
    what do you mean by "Check if the JTextField(s) really contain a text representation of a valid number?" and what else is needed at the above code to perform add and sub operations?
    Suppose a JTextField contains the text "foo"; would that be a valid number?

    kind regards,

    Jos
    adiotrox likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: "error: cannot find symbol e"

    Jos can you please give me the code for a simple addition and subtraction program using awt? pllease i will really appreciate that or make my above code run to add and sub

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: "error: cannot find symbol e"

    You never even told us what goes wrong in your code now ... maybe we can help you to fix it.

    kind regards,

    Jos
    adiotrox likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: "error: cannot find symbol e"

    The above code is running but when i enter input and click on "add" button nothing happens and in cmd prompt i get this:

    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at act.actionPerformed(act.java:43)
    at java.awt.Button.processActionEvent(Button.java:409 )
    at java.awt.Button.processEvent(Button.java:377)
    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(ProtectionDo
    main.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDo
    main.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(ProtectionDo
    main.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThre
    ad.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.
    java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
    ad.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)

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: "error: cannot find symbol e"

    In your init() method you're defining and using local variables t, t1, t2 etc. this means that your member variables with the same name stay null (you never assigned a reasonable value to them); your actionPerformed( ... ) tries to use those member variables, but the are still equal to null; hence the NullPointerException.

    kind regards,

    Jos
    adiotrox likes this.
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: "error: cannot find symbol e"

    Thanks Jos and also what's wrong with this getting an error as"cannot find symbol...f":
    import javax.swing.*;
    public class Swing{

    JButton j;
    JLabel l;

    public static void main(String args[]){

    Swing x = new Swing();
    x.go();


    }
    public void go(){

    JFrame f = new JFrame();
    j = new JButton("click me");
    l = new JLabel("you did it");
    f.getContentPane.add(j);

    f.getContentPane.add(l);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    f.setSize(300,500);
    f.setVisible(true);






    }



    }

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: "error: cannot find symbol e"

    The compiler also mentioned the line where it found the error; don't paraphrase what the compiler said to you; copy the message verbatim.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: "error: cannot find symbol e"

    this are the errors:
    Swing.java:19: error: cannot find symbol
    f.getContentPane.add(j);
    ^
    symbol: variable getContentPane
    location: variable f of type JFrame
    Swing.java:21: error: cannot find symbol
    f.getContentPane.add(l);
    ^
    symbol: variable getContentPane
    location: variable f of type JFrame
    2 errors

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: "error: cannot find symbol e"

    The compiler is complaining about variable 'getContentPane', not variable 'f'; getContentPane() is a method and you should call it (note the parentheses). Read what the compiler had to say.

    kind regards,

    Jos
    adiotrox likes this.
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: "error: cannot find symbol e"

    Thanks Jos and next time i will be more careful to read the errors..i am a newbie :) !

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: "error: cannot find symbol e"

    Quote Originally Posted by adiotrox View Post
    Thanks Jos and next time i will be more careful to read the errors..i am a newbie :) !
    You'd better ;-) There are a few golden rules w.r.t. the Java compiler:

    1) read what the compiler says to you;
    2) the compiler is always right;
    3) never try to run anything the compiler complained about (also see 2)

    kind regards,

    Jos
    adiotrox likes this.
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: "error: cannot find symbol e"

    And one more thing when the above addition and subtraction program is running then subtraction operation is executing fine but the addition gives the result '0' all the time.

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: "error: cannot find symbol e"

    Quote Originally Posted by adiotrox View Post
    And one more thing when the above addition and subtraction program is running then subtraction operation is executing fine but the addition gives the result '0' all the time.
    What code?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    adiotrox is offline Member
    Join Date
    Feb 2012
    Posts
    43
    Rep Power
    0

    Default Re: "error: cannot find symbol e"

    this one:

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    public class act extends Applet implements ActionListener
    {
    Label l,l1,l2;
    TextField t,t1,t2;
    Button b,b1;


    public void init(){


    l=new Label("enter 1st number");
    t=new TextField(20);

    l1=new Label("enter 2nd number");
    t1=new TextField(20);

    l2=new Label("result");
    t2=new TextField(20);

    b=new Button("add");
    b1=new Button("sub");

    add(l);
    add(t);
    add(l1);
    add(t1);
    add(l2);
    add(t2);

    add(b);
    add(b1);

    b.addActionListener(this);
    b1.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){

    int a=Integer.parseInt(t.getText());

    int b=Integer.parseInt(t1.getText());
    int c=0;

    if(e.getSource() == b){
    c=a+b;
    }
    if(e.getSource() == b1){

    c=a-b;

    }
    t2.setText(String.valueOf(c));

    }

    }

    /* <applet code=act height=300 width=300> </applet> */

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: "error: cannot find symbol e"

    Time for some cheap debugging: add a couple System.out.println( ... ) statements to your code:

    Java Code:
    if(e.getSource() == b){
       c=a+b;
       System.out.println("adding: "+c);
    }
    if(e.getSource() == b1){
       c=a-b;
       System.out.println("subtracting: "+c);
    }
    run it and see what happens.

    kind regards,

    Jos

    ps. you should come up with better names for your member variables (<--- hint)
    Last edited by JosAH; 03-10-2012 at 02:14 PM.
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. "Cannot find symbol" error
    By MBD in forum New To Java
    Replies: 5
    Last Post: 09-27-2011, 02:41 PM
  2. "cannot find symbol" error
    By droidus in forum New To Java
    Replies: 2
    Last Post: 09-07-2011, 07:43 AM
  3. Replies: 5
    Last Post: 04-24-2011, 03:21 PM
  4. Error "can not find symbol variable"
    By FullMetalHollow in forum New To Java
    Replies: 5
    Last Post: 10-04-2009, 09:51 PM
  5. "Cannont find symbol Constructor" error
    By Welsh in forum New To Java
    Replies: 7
    Last Post: 01-25-2008, 12:12 AM

Posting Permissions

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