Results 1 to 4 of 4
  1. #1
    2wyked is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Null Pointer Exception HELP!?

    I have cannot figure out what is causing the following program to keep coming up with " Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" error when I click the enter button. Can anybody point me in the right direction?!

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    
    public class IncomeTax extends JFrame
    {
    	
    	Color black = new Color(0, 0, 0);
    	Color white = new Color(255, 255, 255);
    	
    	
    	
    	JLabel grossWageJLabel;
    	JTextField grossWageJTextField;
    	
    	JLabel taxJLabel;
    	JTextField taxJTextField;
    	
    	JLabel deductionsJLabel;
    	JTextField deductionsJTextField;
    	
    	JLabel incomeJLabel;
    	JTextField incomeJTextField;
    	
    	JButton enterJButton;
    	JButton clearJButton;
    	
    	DecimalFormat currency;
    	
    	double grossWage;
    	double taxAmount;
    	double totalDeductions;
    	double netIncome;
    	final double TAX_RATE = 0.20;
    	
    	public IncomeTax()
    	{
    		createUserInterface();
    	}
    	
    	public void createUserInterface()
    	{
    	Container contentPane = getContentPane();
    	contentPane.setBackground(white);
    	contentPane.setLayout(null);
    	
    	JLabel grossWageJLabel;
    	grossWageJLabel = new JLabel();
    	grossWageJLabel.setBounds(50, 20, 150, 20);
    	grossWageJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    	grossWageJLabel.setText("Enter Gross Wage");
    	grossWageJLabel.setForeground(black);
    	grossWageJLabel.setHorizontalAlignment(JLabel.CENTER);
    	contentPane.add(grossWageJLabel);
    	
    	JTextField grossWageJTextField;
    	grossWageJTextField = new JTextField();
    	grossWageJTextField.setBounds(200, 20, 100, 20);
    	grossWageJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    	grossWageJTextField.setHorizontalAlignment(JTextField.CENTER);
    	grossWageJTextField.setForeground(black);
    	grossWageJTextField.setBackground(white);
    	grossWageJTextField.setEditable(true);
    	contentPane.add(grossWageJTextField);
    	
    	JLabel taxJLabel;
    	taxJLabel = new JLabel();
    	taxJLabel.setBounds(50, 50, 150, 20);
    	taxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    	taxJLabel.setText("Tax Amount");
    	taxJLabel.setForeground(black);
    	taxJLabel.setHorizontalAlignment(JLabel.CENTER);
    	contentPane.add(taxJLabel);
    	
    	JTextField taxJTextField;
    	taxJTextField = new JTextField();
    	taxJTextField.setBounds(200, 50, 100, 20);
    	taxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    	taxJTextField.setHorizontalAlignment(JTextField.CENTER);
    	taxJTextField.setForeground(black);
    	taxJTextField.setBackground(white);
    	taxJTextField.setEditable(false);
    	contentPane.add(taxJTextField);
    	
    	JLabel deductionsJLabel;
    	deductionsJLabel = new JLabel();
    	deductionsJLabel.setBounds(50, 80, 150, 20);
    	deductionsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    	deductionsJLabel.setText("Deductions");
    	deductionsJLabel.setForeground(black);
    	deductionsJLabel.setHorizontalAlignment(JLabel.CENTER);
    	contentPane.add(deductionsJLabel);
    	
    	JTextField deductionsJTextField;
    	deductionsJTextField = new JTextField();
    	deductionsJTextField.setBounds(200, 80, 100, 20);
    	deductionsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    	deductionsJTextField.setHorizontalAlignment(JTextField.CENTER);
    	deductionsJTextField.setForeground(black);
    	deductionsJTextField.setBackground(white);
    	deductionsJTextField.setEditable(false);
    	contentPane.add(deductionsJTextField);
    	
    	JLabel incomeJLabel;
    	incomeJLabel = new JLabel();
    	incomeJLabel.setBounds(50, 110, 150, 20);
    	incomeJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    	incomeJLabel.setText("Net Income");
    	incomeJLabel.setForeground(black);
    	incomeJLabel.setHorizontalAlignment(JLabel.CENTER);
    	contentPane.add(incomeJLabel);
    	
    	JTextField incomeJTextField;
    	incomeJTextField = new JTextField();
    	incomeJTextField.setBounds(200, 110, 100, 20);
    	incomeJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    	incomeJTextField.setHorizontalAlignment(JTextField.CENTER);
    	incomeJTextField.setForeground(black);
    	incomeJTextField.setBackground(white);
    	incomeJTextField.setEditable(false);
    	contentPane.add(incomeJTextField);
    	
    	JButton enterJButton;
    	enterJButton = new JButton();
    	enterJButton.setBounds(50, 210, 100, 20);
    	enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
    	enterJButton.setText("Process");
    	enterJButton.setForeground(black);
    	enterJButton.setBackground(white);
    	contentPane.add(enterJButton);
    	enterJButton.addActionListener(
    	
    	new ActionListener()
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			enterJButtonActionPerformed(event);
    		}
    	}
    	);
    	
    	clearJButton = new JButton();
    	clearJButton.setBounds(200, 210, 100, 20);
    	clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
    	clearJButton.setText("Clear");
    	clearJButton.setForeground(black);
    	clearJButton.setBackground(white);
    	contentPane.add(clearJButton);
    	clearJButton.addActionListener(
    	
    	new ActionListener()
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			clearJButtonActionPerformed(event);
    		}
    	}
    	);
    	
    	setTitle("IncomeTax");
    	setSize(400, 400);
    	setVisible(true);
    	}
    	
    	public static void main(String[] args)
    	{
    		IncomeTax application = new IncomeTax();
    		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	
    	public void enterJButtonActionPerformed(ActionEvent event)
    	{
    		currency = new DecimalFormat("$0.00");
    		grossWage = Double.parseDouble(grossWageJTextField.getText());
    		
    		taxAmount = grossWage * TAX_RATE;
    		totalDeductions = grossWage - taxAmount;
    		netIncome = grossWage * TAX_RATE;
    		
    		taxJTextField.setText("" + currency.format(taxAmount));
    		deductionsJTextField.setText("" + currency.format(totalDeductions));
    		incomeJTextField.setText("" + currency.format(netIncome));
    	}
    	
    	public void clearJButtonActionPerformed(ActionEvent event)
    	{
    		grossWageJTextField.setText("");
    		grossWageJTextField.requestFocusInWindow();
    		taxJTextField.setText("");
    		deductionsJTextField.setText("");
    		incomeJTextField.setText("");
    	}
    }
    Last edited by Fubarable; 04-03-2011 at 08:24 PM. Reason: code tags added

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

    Default

    Look at the line that throws the exception, the object being dereferenced on that line is null. Then look through your code and you'll see why -- you declare the variable twice, once in the class (and never initialized) and once in the constructor (and initialized, but completely unrelated to the class field). This is called variable shadowing.

    For example, see the comments in this sample program that's demonstrating your problem:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class VariableShadowing extends JFrame {
       private JTextField myField; // needs to be initialized
       
       public VariableShadowing() {
          super("variable shadowing");
          setLayout(new FlowLayout());
          JTextField myField = new JTextField(10); // redeclared and initialized. incorrect
          // myField = new JTextField(10); // not redeclared. the correct way to do this
          add(myField);
          
          JButton myButton = new JButton("Button");
          myButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                myButtonActionPerformed();
             }
          });
          add(myButton);
       }
    
       private void myButtonActionPerformed() {
          System.out.println("Text in myField is: " + myField.getText());
       }
       
       private static void createAndShowUI() {
          JFrame frame = new VariableShadowing();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    Last edited by Fubarable; 04-03-2011 at 08:33 PM.

  3. #3
    2wyked is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default ?

    Thank you for your help but Im not sure if I understand. I changed the names of the variables and tryed to redeclare them but Im still getting the same error on line 175.

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

    public class IncomeTax extends JFrame
    {

    Color black = new Color(0, 0, 0);
    Color white = new Color(255, 255, 255);



    JLabel grossJLabel;
    JTextField grossJTextField;

    JLabel taxJLabel;
    JTextField taxJTextField;

    JLabel deductionsJLabel;
    JTextField deductionsJTextField;

    JLabel incomeJLabel;
    JTextField incomeJTextField;

    JButton enterJButton;
    JButton clearJButton;

    DecimalFormat currency;

    double wageTotal;
    double taxAmount;
    double totalDeductions;
    double netIncome;
    final double TAX_RATE = 0.20;

    public IncomeTax()
    {
    createUserInterface();
    }

    public void createUserInterface()
    {
    Container contentPane = getContentPane();
    contentPane.setBackground(white);
    contentPane.setLayout(null);

    JLabel grossJLabel;
    grossJLabel = new JLabel();
    grossJLabel.setBounds(50, 20, 150, 20);
    grossJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    grossJLabel.setText("Enter Gross Wage");
    grossJLabel.setForeground(black);
    grossJLabel.setHorizontalAlignment(JLabel.CENTER);
    contentPane.add(grossJLabel);

    JTextField grossJTextField;
    grossJTextField = new JTextField();
    grossJTextField.setBounds(200, 20, 100, 20);
    grossJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    grossJTextField.setHorizontalAlignment(JTextField. CENTER);
    grossJTextField.setForeground(black);
    grossJTextField.setBackground(white);
    grossJTextField.setEditable(true);
    contentPane.add(grossJTextField);

    JLabel taxJLabel;
    taxJLabel = new JLabel();
    taxJLabel.setBounds(50, 50, 150, 20);
    taxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    taxJLabel.setText("Tax Amount");
    taxJLabel.setForeground(black);
    taxJLabel.setHorizontalAlignment(JLabel.CENTER);
    contentPane.add(taxJLabel);

    JTextField taxJTextField;
    taxJTextField = new JTextField();
    taxJTextField.setBounds(200, 50, 100, 20);
    taxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    taxJTextField.setHorizontalAlignment(JTextField.CE NTER);
    taxJTextField.setForeground(black);
    taxJTextField.setBackground(white);
    taxJTextField.setEditable(false);
    contentPane.add(taxJTextField);

    JLabel deductionsJLabel;
    deductionsJLabel = new JLabel();
    deductionsJLabel.setBounds(50, 80, 150, 20);
    deductionsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    deductionsJLabel.setText("Deductions");
    deductionsJLabel.setForeground(black);
    deductionsJLabel.setHorizontalAlignment(JLabel.CEN TER);
    contentPane.add(deductionsJLabel);

    JTextField deductionsJTextField;
    deductionsJTextField = new JTextField();
    deductionsJTextField.setBounds(200, 80, 100, 20);
    deductionsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    deductionsJTextField.setHorizontalAlignment(JTextF ield.CENTER);
    deductionsJTextField.setForeground(black);
    deductionsJTextField.setBackground(white);
    deductionsJTextField.setEditable(false);
    contentPane.add(deductionsJTextField);

    JLabel incomeJLabel;
    incomeJLabel = new JLabel();
    incomeJLabel.setBounds(50, 110, 150, 20);
    incomeJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    incomeJLabel.setText("Net Income");
    incomeJLabel.setForeground(black);
    incomeJLabel.setHorizontalAlignment(JLabel.CENTER) ;
    contentPane.add(incomeJLabel);

    JTextField incomeJTextField;
    incomeJTextField = new JTextField();
    incomeJTextField.setBounds(200, 110, 100, 20);
    incomeJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    incomeJTextField.setHorizontalAlignment(JTextField .CENTER);
    incomeJTextField.setForeground(black);
    incomeJTextField.setBackground(white);
    incomeJTextField.setEditable(false);
    contentPane.add(incomeJTextField);

    JButton enterJButton;
    enterJButton = new JButton();
    enterJButton.setBounds(50, 210, 100, 20);
    enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
    enterJButton.setText("Process");
    enterJButton.setForeground(black);
    enterJButton.setBackground(white);
    contentPane.add(enterJButton);
    enterJButton.addActionListener(

    new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    enterJButtonActionPerformed(event);
    }
    }
    );

    clearJButton = new JButton();
    clearJButton.setBounds(200, 210, 100, 20);
    clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
    clearJButton.setText("Clear");
    clearJButton.setForeground(black);
    clearJButton.setBackground(white);
    contentPane.add(clearJButton);
    clearJButton.addActionListener(

    new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    clearJButtonActionPerformed(event);
    }
    }
    );

    setTitle("IncomeTax");
    setSize(400, 400);
    setVisible(true);
    }

    public static void main(String[] args)
    {
    IncomeTax application = new IncomeTax();
    application.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE);
    }

    public void enterJButtonActionPerformed(ActionEvent event)
    {
    currency = new DecimalFormat("$0.00");
    wageTotal = Double.parseDouble(grossJTextField.getText());

    taxAmount = wageTotal * TAX_RATE;
    totalDeductions = wageTotal - taxAmount;
    netIncome = wageTotal * TAX_RATE;

    taxJTextField.setText("" + currency.format(taxAmount));
    deductionsJTextField.setText("" + currency.format(totalDeductions));
    incomeJTextField.setText("" + currency.format(netIncome));
    }

    public void clearJButtonActionPerformed(ActionEvent event)
    {
    grossJTextField.setText("");
    grossJTextField.requestFocusInWindow();
    taxJTextField.setText("");
    deductionsJTextField.setText("");
    incomeJTextField.setText("");
    }
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    No, it wasn't the name that was wrong. As already mentioned you declared 2 variables with the same name. Look at this example:
    Java Code:
    class Foo {
        int bar = 10;
    
        Foo() {
            int bar = 40; // different variable than the instance variable above
            System.out.println(bar); // output 40
            doStuff();
        }
    
        private void doStuff() {
            System.out.println(bar); // output 10
        }
    }

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null Pointer Exception
    By musasabi in forum New To Java
    Replies: 3
    Last Post: 05-12-2010, 03:52 AM
  3. Help with Null Pointer Exception
    By Beginner in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 04:41 PM
  4. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 11:27 AM
  5. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 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
  •