Results 1 to 11 of 11
  1. #1
    KSUliz is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default java.lang.nullPointerException

    at Transfer.actionPerformed(Transfer.java.128)

    I am confused as to why I am getting this.

    Here is my code

    Java Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.util.*;
    
    public class Transfer extends JFrame implements ActionListener
    {
    	//Declare output stream
    	DataOutputStream output;
    
    	//Construct a panel for the fields and buttons
    	JPanel fieldPanel = new JPanel();
    	JPanel buttonPanel = new JPanel();
    
    	//Construct labels and text boxes
    	JLabel nameLabel = new JLabel("Name:                    ");
    		JTextField name = new JTextField(30);
    	JLabel idLabel = new JLabel ("Student ID");
    		JTextField id = new JTextField(30);
    	JLabel courseNumLabel = new JLabel("Transfer Course Number:			");
    		JTextField courseNum = new JTextField(30);
    	JLabel localCourseLabel = new JLabel("Local Course Number:");
    		JTextField localCourse = new JTextField(30);
    
    	//Construct button
    	JButton submitButton = new JButton("Submit");
    	JButton exitButton = new JButton("Exit");
    
    	public static void main(String[] args)
    	{
    		Transfer f = new Transfer();
    		f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    		f.setSize(450,300);
    		f.setTitle("Transfer Course Substitutions");
    		f.setVisible(true);
    	} //ends main method
    
    	public Transfer()
    	{
    		Container c = getContentPane();
    		c.setLayout((new BorderLayout()));
    		fieldPanel.setLayout(new GridLayout(4,2));
    		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    
    		//Add fields to rows
    		fieldPanel.add(nameLabel);
    		fieldPanel.add(name);
    
    		fieldPanel.add(idLabel);
    		fieldPanel.add(id);
    
    		fieldPanel.add(courseNumLabel);
    		fieldPanel.add(courseNum);
    
    
    		fieldPanel.add(localCourseLabel);
    		fieldPanel.add(localCourse);
    
    		//Add button to panel
    		buttonPanel.add(submitButton);
    		buttonPanel.add(exitButton);
    
    		//Add Panels to frame
    		c.add(fieldPanel, BorderLayout.CENTER);
    		c.add(buttonPanel, BorderLayout.SOUTH);
    
    		//add functionality to buttons
    		submitButton.addActionListener(this);
    		exitButton.addActionListener(this);
    
    		try
    		{
    			new DataOutputStream(new FileOutputStream("Transfer.dat"));
    		} //ends try statement
    		catch (IOException io)
    		{
    			System.exit(1);
    		} //ends catch statement
    
    		addWindowListener(
    			new WindowAdapter()
    			{
    				public void windowClosing(WindowEvent e)
    				{
    					int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submittions", JOptionPane.YES_NO_OPTION);
    					if (answer == JOptionPane.YES_OPTION)
    						System.exit(0);
    				} //ends windowClosing statement
    			} //ends window adpater
    		); //ends window listener
    }//ends transfer constructor
    	public void actionPerformed(ActionEvent e)
    	{
    		String arg = e.getActionCommand();
    		if (arg == "Submit")
    		{
    			try
    			{
    				output.writeUTF(name.getText());
    				output.writeUTF(id.getText());
    				output.writeUTF(courseNum.getText());
    				output.writeUTF(localCourse.getText());
    			} //ends try statement
    			catch(IOException ex)
    			{
    				System.exit(1);
    			} //ends catch statement
    			clearFields();
    		} //ends if statement
    		else //code to execute if the user clicks Exit
    		{
    			try
    			{
    				output.close();
    			} //ends try statement
    			catch(IOException c)
    			{
    				System.exit(1);
    			}//ends catch statement
    			System.exit(0);
    		} //ends else statement
    	}//ends action performed method
    	public void clearFields()
    	{
    		//Clear fields and reset the focus
    		name.setText("");
    		id.setText("");
    		courseNum.setText("");
    		localCourse.setText("");
    		name.requestFocus();
    	} //ends clear fields method
    } //ends transfer class

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by KSUliz View Post
    Java Code:
    		String arg = e.getActionCommand();
    		if (arg == "Submit")
    Don't compare Strings like that; use the equals( ... ) method instead; also read a tutorial on Java to see what the == operator actually does. Java != Basic.

    kind regards,

    Jos

  3. #3
    KSUliz is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    Well I am in a begining class on java, and using a Shelly Cashman book for Java. Comprehensive Concepts and Techniques third edition.
    We are on chapter 8.

    That arg == "Submit" is what we are taught to use, in fact it gives us that line of code to use. So you are saying it should be arg..."Submit"?

    We are taught in class that the == means that it is exactly equal to...whatever...and to use that when comparing, call it bad teaching, bad publishing, I am just trying to get to know this well enough to get into graduate school...
    Thanks for yoru help,
    My main question is your saying that that arg == "Submit" should be arg..."Submit" instead right?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by KSUliz View Post
    Well I am in a begining class on java, and using a Shelly Cashman book for Java. Comprehensive Concepts and Techniques third edition.
    We are on chapter 8.

    That arg == "Submit" is what we are taught to use, in fact it gives us that line of code to use. So you are saying it should be arg..."Submit"?

    We are taught in class that the == means that it is exactly equal to...whatever...and to use that when comparing, call it bad teaching, bad publishing, I am just trying to get to know this well enough to get into graduate school...
    Thanks for yoru help,
    My main question is your saying that that arg == "Submit" should be arg..."Submit" instead right?
    It should be: arg.equals("Submit") and if that book teaches you otherwise throw it in the fire place because that's where it belongs then; if your teacher also teaches the same thing throw him/her in there too.

    kind regards,

    Jos

  5. #5
    KSUliz is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Transfer.actionPerformed(Transfer.java:111)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:18
    49)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.jav
    a:2169)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
    .java:420)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:258
    )
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
    istener.java:234)
    at java.awt.Component.processMouseEvent(Component.jav a:5488)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3093)
    at java.awt.Component.processEvent(Component.java:525 3)
    at java.awt.Container.processEvent(Container.java:196 6)
    at java.awt.Component.dispatchEventImpl(Component.jav a:3955)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2024)
    at java.awt.Component.dispatchEvent(Component.java:38 03)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4212
    )
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3892)

    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3822)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2010)
    at java.awt.Window.dispatchEventImpl(Window.java:1766 )
    at java.awt.Component.dispatchEvent(Component.java:38 03)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 463)
    at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchTh
    read.java:234)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
    ad.java:163)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:157)

    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:149)

    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:110)

    There is the list of errors I am getting in the command prompt window, and after looking at my book more I added a couple more lines of code...
    so here it is too
    Java Code:
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.util.*;
    
    public class Transfer extends JFrame implements ActionListener
    {
    	//Declare output stream
    	DataOutputStream output;
    
    	//Construct a panel for the fields and buttons
    	JPanel fieldPanel = new JPanel();
    	JPanel buttonPanel = new JPanel();
    
    	//Construct labels and text boxes
    	JLabel nameLabel = new JLabel("Name:");
    		JTextField name = new JTextField();
    	JLabel idLabel = new JLabel ("Student ID:");
    		JTextField id = new JTextField();
    	JLabel courseNumLabel = new JLabel("Transfer Course Number:");
    		JTextField courseNum = new JTextField();
    	JLabel localCourseLabel = new JLabel("Local Course Number:");
    		JTextField localCourse = new JTextField();
    
    	//Construct button
    	JButton submitButton = new JButton("Submit");
    	JButton exitButton = new JButton("Exit");
    
    	public static void main(String[] args)
    	{
    		Transfer f = new Transfer();
    		f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    		f.setSize(450,300);
    		f.setTitle("Transfer Course Substitutions");
    		f.setVisible(true);
    	} //ends main method
    
    	public Transfer()
    	{
    		Container c = getContentPane();
    		c.setLayout((new BorderLayout()));
    		fieldPanel.setLayout(new GridLayout(4,2));
    		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    
    		//Add fields to rows
    		fieldPanel.add(nameLabel);
    		fieldPanel.add(name);
    
    		fieldPanel.add(idLabel);
    		fieldPanel.add(id);
    
    		fieldPanel.add(courseNumLabel);
    		fieldPanel.add(courseNum);
    
    
    		fieldPanel.add(localCourseLabel);
    		fieldPanel.add(localCourse);
    
    		//Add button to panel
    		buttonPanel.add(submitButton);
    		buttonPanel.add(exitButton);
    
    		//Add Panels to frame
    		c.add(fieldPanel, BorderLayout.CENTER);
    		c.add(buttonPanel, BorderLayout.SOUTH);
    
    		//add functionality to buttons
    		submitButton.addActionListener(this);
    		exitButton.addActionListener(this);
    
    		try
    		{
    			new DataOutputStream(new FileOutputStream("Transfer.dat"));
    		} //ends try statement
    		catch (IOException io)
    		{
    			System.exit(1);
    		} //ends catch statement
    
    		addWindowListener(
    			new WindowAdapter()
    			{
    				public void windowClosing(WindowEvent e)
    				{
    					int answer = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit and submit the file?", "File Submittions", JOptionPane.YES_NO_OPTION);
    					if (answer == JOptionPane.YES_OPTION)
    						System.exit(0);
    				} //ends windowClosing statement
    			} //ends window adpater
    		); //ends window listener
    }
    	public void actionPerformed(ActionEvent e)
    	{
    		String arg = e.getActionCommand();
    
    		if (checkFields())
    		{
    			if (arg.equals("Submit"))
    
    			try
    			{
    				output.writeUTF(name.getText());
    				output.writeUTF(id.getText());
    				output.writeUTF(courseNum.getText());
    				output.writeUTF(localCourse.getText());
    
    				JOptionPane.showMessageDialog(null,"The transfer credit infromation has been saved.","Submission Successful", JOptionPane.INFORMATION_MESSAGE);
    			} //ends try statement
    			catch(IOException ex)
    			{
    				System.exit(1);
    			} //ends catch statement
    			clearFields();
    		} //ends if statement
    		else //code to execute if the user clicks Exit
    		{
    			try
    			{
    				output.close();
    				System.out.println();
    			} //ends try statement
    			catch(IOException c)
    			{
    				System.exit(1);
    			}//ends catch statement
    			System.exit(0);
    		} //ends else statement
    	}//ends action performed method
    
    	public boolean checkFields()
    	{
    		if((name.getText().compareTo("")<1)			||
    			(id.getText().compareTo("")<1)			||
    			(courseNum.getText().compareTo("")<1)	||
    			(localCourse.getText().compareTo("")<1))
    			{
    				JOptionPane.showMessageDialog(null, "You must complete all fields.", "Data Entry Error", JOptionPane.WARNING_MESSAGE);
    				return false;
    			}
    			else
    			{
    				return true;
    			}
    		}
    	public void clearFields()
    	{
    		//Clear fields and reset the focus
    		name.setText("");
    		id.setText("");
    		courseNum.setText("");
    		localCourse.setText("");
    		name.requestFocus();
    	} //ends clear fields method
    } //ends transfer class
    Appreciate your help, for some reason that didn't make a difference for me.
    Liz

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by KSUliz View Post
    Appreciate your help, for some reason that didn't make a difference for me.
    Liz
    My guess is that your 'output' variable is null and my guess is also that I'm right because you're not assigning a value to it anywhere.

    kind regards,

    Jos

  7. #7
    KSUliz is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    Java Code:
    				output.writeUTF(name.getText());
    				output.writeUTF(id.getText());
    				output.writeUTF(courseNum.getText());
    				output.writeUTF(localCourse.getText());
    what does this do if it doesnt assign the values from name, id, courseNUm, and localCOurse to the output? to write it to the file?

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by KSUliz View Post
    Java Code:
    				output.writeUTF(name.getText());
    				output.writeUTF(id.getText());
    				output.writeUTF(courseNum.getText());
    				output.writeUTF(localCourse.getText());
    what does this do if it doesnt assign the values from name, id, courseNUm, and localCOurse to the output? to write it to the file?
    What is the value of the 'output' object? Have you tested it:

    Java Code:
    System.out.println("output: "+output);
    I bet it is null.

    kind regards,

    Jos

  9. #9
    KSUliz is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    It doesnt get to that line of code if i put it after those other four. Looking at the exception, it starts on line 111, which is where those 4 lines of code begin. However, I check before it gets there to ensure that there are in fact values there, and if I dont put values in, I get the error message, but once there are values in I get this exception.
    So whhy is it not getting anything from name in line 111 when there is clearly information in there, or it would give me the error message I created in my "checkFields"....I am really confused, and appreciate your help!

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by KSUliz View Post
    It doesnt get to that line of code if i put it after those other four. Looking at the exception, it starts on line 111, which is where those 4 lines of code begin. However, I check before it gets there to ensure that there are in fact values there, and if I dont put values in, I get the error message, but once there are values in I get this exception.
    So whhy is it not getting anything from name in line 111 when there is clearly information in there, or it would give me the error message I created in my "checkFields"....I am really confused, and appreciate your help!
    I'm not talking about any data values, I'm talking about the value of your 'output' variable. Read your own code: you are not assigning any output stream to it so the 'output' variable is still null. That's why that exception is thrown.

    kind regards,

    Jos

  11. #11
    KSUliz is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. java.lang.NullPointerException
    By Travis in forum AWT / Swing
    Replies: 7
    Last Post: 09-18-2009, 06:49 PM
  2. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 06:41 AM
  3. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 01:36 PM
  4. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 2
    Last Post: 02-27-2009, 11:11 AM
  5. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:39 PM

Posting Permissions

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