Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Default Why do I get this green text?

    For my computer science class we had to build a calculator. When I click on the sin and square root buttons all this green text shows up. For my square root button all this text will show up and still give me the right answer but nothing happens with sin. How do I fix this?

    Here is my code:

    Java Code:
       import javax.swing.JFrame; // swing is newer graphics package
       import javax.swing.JPanel;
       import java.awt.Container; // abstract windowing toolkit
       import javax.swing.JComponent;
       import java.awt.*; 
       import java.awt.Graphics;
       import java.awt.event.WindowAdapter; // for WindowListener()
       import java.awt.event.WindowEvent;
       import java.awt.event.*;
       import java.awt.Font;
       import java.awt.Color;
       import java.awt.FlowLayout;
       import java.awt.BorderLayout;
       import java.awt.GridLayout;
       import java.awt.Dimension;
       import javax.swing.BoxLayout;
       import javax.swing.JButton;
       import javax.swing.JLabel;
       import javax.swing.JComboBox;
       import javax.swing.JPanel;
       import javax.swing.*;
       import java.io.*;
    
    
       import java.util.Calendar;
    
        class CollinSCalculator
       {
           public static void main (String[] args)
          {
          
             Calculator1 frame = new Calculator1();
          
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          }
       }
    
        class Calculator1 extends JFrame implements ActionListener
       { 
          JTextField textfield = new JTextField(16);
          String str1;
          String str2;
          double numbers;
       
          Double n1;
          Double n2;
          Double n3;
          String op =" ";
          JButton youtube = new JButton();
           public Calculator1()
          {
             super ("This device will help you solve all your mathematical problems.");
          
          // northPanel
          //----------------------------
          
          // northPanel set up
             JPanel northPanel = new JPanel();
             northPanel.setLayout (new GridLayout(2, 4));
             JPanel eastPanel = new JPanel();
             eastPanel.setLayout (new GridLayout (2, 1));
          // set up componenets for exit button
             Icon exitsign = new ImageIcon("exit-signs.JPG"); 
             JButton exit = new JButton (" ");
             exit.addActionListener(this);
             exit.setIcon(exitsign); 
          
             Icon youtube1 = new ImageIcon("youtube.jpeg");
          
             youtube.addActionListener(this);
             youtube.setIcon(youtube1);
          
          
          
             eastPanel.add(exit);
             eastPanel.add(youtube);
          
             textfield.setFont(new Font("comic", Font.BOLD, 48));
             textfield.setEditable(false);
             textfield.setBackground(Color.blue);
             textfield.setHorizontalAlignment(JTextField.CENTER);
          
             northPanel.add(textfield);
          
          // westPanel
          //----------------------------------
          // Instantiating the west panel
             JPanel westPanel = new JPanel();
          //setting the layout for the westPanel
             westPanel.setLayout (new GridLayout (6, 2));
          
          
             JButton one = new JButton("1");
             one.addActionListener(this);
             JButton two = new JButton("2");
             two.addActionListener(this);
             JButton three = new JButton("3");
             three.addActionListener(this);
             JButton four = new JButton("4");
             four.addActionListener(this); 
             JButton five = new JButton("5");
             five.addActionListener(this);
             JButton six = new JButton("6");
             six.addActionListener(this);
             JButton seven = new JButton("7");
             seven.addActionListener(this);
             JButton eight = new JButton("8"); 
             eight.addActionListener(this);
             JButton nine = new JButton("9");
             nine.addActionListener(this);
             JButton zero = new JButton("0");
             zero.addActionListener(this);
             JButton plus = new JButton("+");
             plus.addActionListener(this);
             JButton minus = new JButton("-");
             minus.addActionListener(this);
             JButton divide = new JButton("/");
             divide.addActionListener(this);
             JButton multiply = new JButton("*");
             multiply.addActionListener(this);
             JButton equals = new JButton("=");
             equals.addActionListener(this);
             JButton dot = new JButton(".");
             dot.addActionListener(this);
             JButton sin = new JButton("sin");
             sin.addActionListener(this);
             JButton pi = new JButton("pi");
             pi.addActionListener(this);
             JButton negative = new JButton("+/-");
             negative.addActionListener(this);
             JButton backspace = new JButton("clear"); 
             backspace.addActionListener(this);
             JButton power = new JButton("^"); 
             power.addActionListener(this);
             JButton squareroot = new JButton("sqrt"); 
             squareroot.addActionListener(this);
          
          
             northPanel.add(one);
             northPanel.add(two);
             northPanel.add(three);
             northPanel.add(four);
             northPanel.add(five);
             northPanel.add(six);
             northPanel.add(seven);
             northPanel.add(eight);
             northPanel.add(nine);
             northPanel.add(zero);
             westPanel.add(plus);
             westPanel.add(minus);
             westPanel.add(divide);
             westPanel.add(multiply);
             westPanel.add(equals);
             westPanel.add(dot);
             westPanel.add(sin);
             westPanel.add(pi);
             westPanel.add(negative);
             westPanel.add(backspace);
             westPanel.add(power);
             westPanel.add(squareroot);
          
          // contentPane
          // -----------
          // create and define layout
             JPanel pane = (JPanel)getContentPane();
             pane.setLayout(new BorderLayout());
          
          
          // add components to panel
             pane.add(northPanel, BorderLayout.NORTH);
             pane.add(westPanel, BorderLayout.WEST);
             pane.add(eastPanel, BorderLayout.EAST);
             pane.add(textfield, BorderLayout.CENTER);
          // make it visible
             this.setSize(1500, 350);
             this.setVisible(true);
          
          
          
          }
       
           public void actionPerformed (ActionEvent e)
          {
          //setting up exit button
             if (e.getActionCommand().equals(" "))
             { 
                System.exit(0);
             }
             
             else if (e.getSource() == youtube)
             {
                try
                {
                   Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "start", "http://www.youtube.com"});
                }
                    catch(IOException ioException)
                   {
                      System.err.print(ioException);
                   }
             } 
          
          
             if (e.getActionCommand().equals("1"))
             
                textfield.setText(textfield.getText() + "1");
          
          
             if (e.getActionCommand().equals("2"))
             
                textfield.setText(textfield.getText()+ "2");
          
          
             if (e.getActionCommand().equals("3"))
             
                textfield.setText(textfield.getText() + "3");
          
             if (e.getActionCommand().equals("4"))
             
                textfield.setText(textfield.getText() + "4");
          
             if (e.getActionCommand().equals("5"))
             
                textfield.setText(textfield.getText() + "5");
          
             if (e.getActionCommand().equals("6"))
             
                textfield.setText(textfield.getText() + "6");
          
             if (e.getActionCommand().equals("7"))
             
                textfield.setText(textfield.getText() + "7");
          
             if (e.getActionCommand().equals("8"))
             
                textfield.setText(textfield.getText() + "8");
          
             if (e.getActionCommand().equals("9"))
             
                textfield.setText(textfield.getText() + "9");
          
             if (e.getActionCommand().equals("0"))
             { 
                textfield.setText(textfield.getText() + "0");
             
             }
             if (e.getActionCommand().equals("."))
             { 
             
                textfield.setText(textfield.getText() + ".");
             }
             if (e.getActionCommand().equals("pi"))
             { 
                textfield.setText(textfield.getText() + "3.1415926535");
             } 
             if (e.getActionCommand().equals("+"))
             { 
             
                str1 = textfield.getText();
                n1 = (Double.valueOf(str1)).doubleValue();
                op = "+";
             
                textfield.setText("");
             }
             if (e.getActionCommand().equals("-"))
             { 
                str1 = textfield.getText();
                n1 = (Double.valueOf(str1)).doubleValue();
                op = "-";
             
                textfield.setText("");
             }
             if (e.getActionCommand().equals("/"))
             { 
                str1 = textfield.getText();
                n1 = (Double.valueOf(str1)).doubleValue();
                op = "/";
             
                textfield.setText("");
             }
             if (e.getActionCommand().equals("*"))
             { 
                str1 = textfield.getText();
                n1 = (Double.valueOf(str1)).doubleValue();
                op = "*";
             
                textfield.setText("");
             } 
             if (e.getActionCommand().equals("sin"))
             { 
                str1 = textfield.getText();
                n1 = (Double.valueOf(str1)).doubleValue();
                op = "sin";
                n3 = (Math.sin(n2));
                textfield.setText("");
             } 
             if (e.getActionCommand().equals("sqrt"))
             { 
                str1 = textfield.getText();
                n1 = (Double.valueOf(str1)).doubleValue();
                op = "sqrt";
             
                textfield.setText("");
             } 
             if (e.getActionCommand().equals("sin"))
             { 
                str1 = textfield.getText();
                n1 = (Double.valueOf(str1)).doubleValue();
                op = "sin";
             
                textfield.setText("");
             } 
             if (e.getActionCommand().equals("^"))
             { 
                str1 = textfield.getText();
                n1 = (Double.valueOf(str1)).doubleValue();
                op = "^";
             
                textfield.setText("");
             }
          
             if (e.getActionCommand().equals("+/-"))
             { 
                textfield.setText("-");
             } 
             if (e.getActionCommand().equals("clear"))
             { 
                textfield.setText("");
             }
             if (e.getActionCommand().equals("="))
             { 
                str2 = textfield.getText();
                n2 = (Double.valueOf(str2)).doubleValue();
             
                if (op=="+")
                {
                   n3 = n1 + n2;
                
                   textfield.setText(n3.toString());
                }
                if (op=="-")
                {
                   n3 = n1-n2;
                   textfield.setText(n3.toString());
                }
                if(op=="/")
                {
                   n3 = n1/n2;
                   textfield.setText(n3.toString());
                }
                if(op=="*")
                {
                   n3 = n1*n2;
                   textfield.setText(n3.toString());
                } 
                if (e.getSource() == "sin")
                {
                   n3 = Math.sin(n2);
                   textfield.setText(n3.toString());
                   System.out.println(n2);
                } 
             
                if(op=="+/-")
                {
                   n3 = -n2;
                   textfield.setText(n3.toString());
                }
                if(op=="^")
                {
                   n3 = Math.pow(n2, n1);
                   textfield.setText(n3.toString());
                }
             
                if(op=="sqrt")
                {
                   n3 = Math.sqrt(n2);
                   textfield.setText(n3.toString());
                }
             }
          }
       }


    and here is the green text:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:994)
    at java.lang.Double.valueOf(Double.java:475)
    at Calculator1.actionPerformed(CollinSCalculator.java :267)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6263)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:602 8)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    Moderator Edit: Code tags added
    Last edited by thestubbsinator; 06-05-2010 at 07:49 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    4

    Default

    1. Please use the code brackets
    2. Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I edited your code and added code tags which usually helps to make your posted code retain its formatting and be more readable. Unfortunately, the code that you posted isn't formatted well being all left-justified, and so it is still hard to read. If you are still serious about requesting our help, I suggest that you edit this post above and replace the poorly formatted code with well-formatted code copied from your IDE (if you are using one).

    Also, be sure to continue to use code tags. To do this, highlight your pasted code and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

    Best of luck

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Take each error one at a time, and use the error codes to help you figure out what is not working. For instance when sin is pressed you get a NullPointerException at this line:

    Java Code:
        if (e.getActionCommand().equals("sin")) {
          str1 = textfield.getText();
          n1 = (Double.valueOf(str1)).doubleValue();
          op = "sin";
          n3 = (Math.sin(n2));  // [color="red"]*** NPE occurs here[/color]
          textfield.setText("");
        }
    That should tell you that you are trying to use an object on that line that is null, and in fact just looking at that line, there is only one object that could be causing the problem, the Double object n2. So now look back in your program where you use n2 and ask yourself, where do you initiate this object?

    As an aside, there is no reason to use the Double wrapper class here but rather you should use the primitive "double". Regardless, you still have to give it a value before you try to use it.

    Suerte.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:994)
    at java.lang.Double.valueOf(Double.java:475)
    at Calculator1.actionPerformed(CollinSCalculator.java :267)
    Look at line 267 in your program. What is wrong with the value there that is being converted? Add a System.out.println(..) before the statement to see the value of the variable.

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

    Default

    No need for the println()...the error already says it's an empty String.
    :)

Similar Threads

  1. Replies: 8
    Last Post: 11-06-2009, 06:52 AM
  2. [SOLVED] Expected Range: Red Green Blue
    By AndrewM16921 in forum New To Java
    Replies: 1
    Last Post: 04-03-2009, 07:59 PM
  3. Replies: 2
    Last Post: 01-23-2009, 04:09 PM
  4. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  5. Replies: 0
    Last Post: 05-26-2008, 04:26 PM

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
  •