7 Replies - 700 Views - Last Post: 06 November 2011 - 05:44 AM Rate Topic: -----

#1 Abhinav_BlackOp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-November 11

Null Pointer Exception On Execution(AWT error)

Posted 05 November 2011 - 08:18 AM

I wrote this program and it compiles smoothly but after I execute it I get a null pointer exception error at line 170 i.e. the if Condition.I want that when I click go button i get the data from the two JCombo Boxes and then compare them to display the result in JOptionPane.I think it is cuz im using ".equals"for comparison and so have to compare the objects.Pls help any help regarding the error will be greatly appreciated.Here is the coding

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.Font.*;

class School_Project_Test 
{
JFrame frame;
JButton Go,Done,time_img,station_img,b_from,b_to;
JCheckBox time,station;
String Destination_from[] = {"New Delhi","Mumbai Cent.","Sealdah","H. Nizamuddin","Bandra","KLS","Trivandrum Cent.","Amritsar Jntn","Bangalore City","Chennai Cent","Howrah Jntn","Ahemdabad Jntn","Allahabad"};
String Destination_to[] = {"New Delhi","Mumbai Cent.","Sealdah","H. Nizamuddin","Bandra","KLS","Trivandrum Cent.","Amritsar Jntn","Bangalore City","Chennai Cent","Howrah Jntn","Ahemdabad Jntn","Allahabad"};
ActionEvent ef,et;
JComboBox from , to;
JLabel lab;
String username,password;

Font font = new Font("Algerian",Font.PLAIN,13);
public static void main(String args[])
{
School_Project_Test gui = new School_Project_Test();

gui.go();
}

public void go()
{

frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1024,768);
frame.setLayout(null);

Go = new JButton();
Go.setBounds(384,576,254,50);
Go.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Go.jpg"));
Go.addActionListener(new GoActionListener());
frame.getContentPane().add(Go);

Done = new JButton();
Done.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Done.jpg"));
Done.setBounds(384,640,254,50);
Done.addActionListener(new DoneActionListener());
frame.getContentPane().add(Done);

time_img=new JButton();
time_img.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Time.jpg"));
time_img.setBounds(60,250,100,40);
time_img.addActionListener(new time_imgActionListener());
frame.getContentPane().add(time_img);

station_img=new JButton();
station_img.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Station.jpg"));
station_img.setBounds(220,250,100,40);
station_img.addActionListener(new station_imgActionListener());
frame.getContentPane().add(station_img);

time = new JCheckBox("Time");
time.setSelected(false);
time.setBounds(60,320,60,40);
frame.getContentPane().add(time);

station = new JCheckBox("Station");
station.setSelected(true);
station.setBounds(220,320,100,40);
frame.getContentPane().add(station);

lab = new JLabel();
lab.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\RailWay Welcome\\Slide2.jpg"));
lab.setSize(1024,768);
frame.getContentPane().add(lab);

from = new JComboBox(Destination_from);
from.setEditable(true);
from.setBounds(220,450,145,30);
from.setMaximumRowCount(4);
from.setFont(font);
frame.getContentPane().add(from);

to = new JComboBox(Destination_to);
to.setEditable(true);
to.setMaximumRowCount(4);
to.setBounds(60,450,145,30);
to.setFont(font);
frame.getContentPane().add(to);

b_from = new JButton();
b_from.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\FROM\\From.jpg"));
b_from.setBounds(220,380,100,40);
b_from.addActionListener(new b_fromActionListener());
frame.getContentPane().add(b_from);

b_to = new JButton();
b_to.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\FROM\\To.jpg"));
b_to.setBounds(60,380,100,40);
b_to.addActionListener(new b_toActionListener());
frame.getContentPane().add(b_to);


frame.setVisible(true);
}

 class  time_imgActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
 JOptionPane.showMessageDialog(null,"Pls Select whether to show the train timings or not","Select Value(Time)",JOptionPane.INFORMATION_MESSAGE);
}
}

class station_imgActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
 JOptionPane.showMessageDialog(null,"Pls Select whether to show the Station details or not","Select Value(Station)",JOptionPane.INFORMATION_MESSAGE);
}
}


class b_fromActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
 JOptionPane.showMessageDialog(null,"Pls Select from where you have to go","Select Value(Destination)",JOptionPane.INFORMATION_MESSAGE);
}
}

class b_toActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
 JOptionPane.showMessageDialog(null,"Pls Select where you have to go","Select Value(Destination)",JOptionPane.INFORMATION_MESSAGE);
}
}

class DoneActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
 JOptionPane.showMessageDialog(null,"Thank You For Using My Program,Regards Abhinav","Appreciation",JOptionPane.INFORMATION_MESSAGE);
 frame.setVisible(false);
}
}

class fromActionListener implements ActionListener
{
public void actionPerformed(ActionEvent ef)
{
from = (JComboBox) ef.getSource();
}
} 

class toActionListener implements ActionListener
{
public void actionPerformed(ActionEvent et)
{
to = (JComboBox) et.getSource();
}
}

class GoActionListener implements ActionListener
{
public void actionPerformed(ActionEvent eg)
{
if((et.equals(Destination_to[0]))&&(ef.equals(Destination_from[0])))
{
JOptionPane.showMessageDialog(null,"Program Running","Congrats!",JOptionPane.INFORMATION_MESSAGE);
}

else
{JOptionPane.showMessageDialog(null,"Program Else Running","Congrats!",JOptionPane.INFORMATION_MESSAGE);
}
}
}
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Exception On Execution(AWT error)

#2 Codey09  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 210
  • Joined: 20-August 09

Re: Null Pointer Exception On Execution(AWT error)

Posted 05 November 2011 - 08:47 AM

it would be nice if you can put the code in [ code ] tags
Was This Post Helpful? 0
  • +
  • -

#3 AMZDeCoder  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 102
  • Joined: 04-November 11

Re: Null Pointer Exception On Execution(AWT error)

Posted 05 November 2011 - 08:52 AM

Well buddy you have forgotten to initialize your action event variables in your code so you are getting the Null Pointer exception.
ActionEvent ef, et;



Below is his code, in code tags for other people,

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

class School_Project_Test {
	JFrame frame;
	JButton Go, Done, time_img, station_img, b_from, b_to;
	JCheckBox time, station;
	String Destination_from[] = { "New Delhi", "Mumbai Cent.", "Sealdah",
			"H. Nizamuddin", "Bandra", "KLS", "Trivandrum Cent.",
			"Amritsar Jntn", "Bangalore City", "Chennai Cent", "Howrah Jntn",
			"Ahemdabad Jntn", "Allahabad" };
	String Destination_to[] = { "New Delhi", "Mumbai Cent.", "Sealdah",
			"H. Nizamuddin", "Bandra", "KLS", "Trivandrum Cent.",
			"Amritsar Jntn", "Bangalore City", "Chennai Cent", "Howrah Jntn",
			"Ahemdabad Jntn", "Allahabad" };
	ActionEvent ef, et;
	JComboBox from, to;
	JLabel lab;
	String username, password;

	Font font = new Font("Algerian", Font.PLAIN, 13);

	public static void main(final String args[]) {
		final School_Project_Test gui = new School_Project_Test();

		gui.go();
	}

	public void go() {

		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(1024, 768);
		frame.setLayout(null);

		Go = new JButton();
		Go.setBounds(384, 576, 254, 50);
		Go
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Go.jpg"));
		Go.addActionListener(new GoActionListener());
		frame.getContentPane().add(Go);

		Done = new JButton();
		Done
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Done.jpg"));
		Done.setBounds(384, 640, 254, 50);
		Done.addActionListener(new DoneActionListener());
		frame.getContentPane().add(Done);

		time_img = new JButton();
		time_img
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Time.jpg"));
		time_img.setBounds(60, 250, 100, 40);
		time_img.addActionListener(new time_imgActionListener());
		frame.getContentPane().add(time_img);

		station_img = new JButton();
		station_img
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Station.jpg"));
		station_img.setBounds(220, 250, 100, 40);
		station_img.addActionListener(new station_imgActionListener());
		frame.getContentPane().add(station_img);

		time = new JCheckBox("Time");
		time.setSelected(false);
		time.setBounds(60, 320, 60, 40);
		frame.getContentPane().add(time);

		station = new JCheckBox("Station");
		station.setSelected(true);
		station.setBounds(220, 320, 100, 40);
		frame.getContentPane().add(station);

		lab = new JLabel();
		lab
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\RailWay Welcome\\Slide2.jpg"));
		lab.setSize(1024, 768);
		frame.getContentPane().add(lab);

		from = new JComboBox(Destination_from);
		from.setEditable(true);
		from.setBounds(220, 450, 145, 30);
		from.setMaximumRowCount(4);
		from.setFont(font);
		frame.getContentPane().add(from);

		to = new JComboBox(Destination_to);
		to.setEditable(true);
		to.setMaximumRowCount(4);
		to.setBounds(60, 450, 145, 30);
		to.setFont(font);
		frame.getContentPane().add(to);

		b_from = new JButton();
		b_from
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\FROM\\From.jpg"));
		b_from.setBounds(220, 380, 100, 40);
		b_from.addActionListener(new b_fromActionListener());
		frame.getContentPane().add(b_from);

		b_to = new JButton();
		b_to
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\FROM\\To.jpg"));
		b_to.setBounds(60, 380, 100, 40);
		b_to.addActionListener(new b_toActionListener());
		frame.getContentPane().add(b_to);

		frame.setVisible(true);
	}

	class time_imgActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ev) {
			JOptionPane.showMessageDialog(null,
					"Pls Select whether to show the train timings or not",
					"Select Value(Time)", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class station_imgActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ev) {
			JOptionPane.showMessageDialog(null,
					"Pls Select whether to show the Station details or not",
					"Select Value(Station)", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class b_fromActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ev) {
			JOptionPane.showMessageDialog(null,
					"Pls Select from where you have to go",
					"Select Value(Destination)",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class b_toActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ev) {
			JOptionPane.showMessageDialog(null,
					"Pls Select where you have to go",
					"Select Value(Destination)",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class DoneActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ev) {
			JOptionPane.showMessageDialog(null,
					"Thank You For Using My Program,Regards Abhinav",
					"Appreciation", JOptionPane.INFORMATION_MESSAGE);
			frame.setVisible(false);
		}
	}

	class fromActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ef) {
			from = (JComboBox) ef.getSource();
		}
	}

	class toActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent et) {
			to = (JComboBox) et.getSource();
		}
	}

	class GoActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent eg) {
			if ((et.equals(Destination_to[0]))
					&& (ef.equals(Destination_from[0]))) {
				JOptionPane.showMessageDialog(null, "Program Running",
						"Congrats!", JOptionPane.INFORMATION_MESSAGE);
			}

			else {
				JOptionPane.showMessageDialog(null, "Program Else Running",
						"Congrats!", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

#4 Abhinav_BlackOp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-November 11

Re: Null Pointer Exception On Execution(AWT error)

Posted 05 November 2011 - 10:38 AM

AMZDecoder

Thank you for your reply that was very quick but I am still getting that error.I just want to make one thing clear that every other component of the program is working properly except the the Go button and its action event method



After going through the program it seems you have declared the variables as final which again to my dismay is not woking but thanks anyway.I believe their some problem with the way im comparing the values in the if condition.I mean that im using ".equals" method which is more suitable for comparing objects rather than the exact variables.I am unable to create the variables for the two destinations arrays.If I use "==" comparison then I get the error"Incomparable types java.awt.event.ActionEvent and java.lang.String"

here is the coding
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

class School_Project_Test {
	JFrame frame;
	JButton Go, Done, time_img, station_img, b_from, b_to;
	JCheckBox time, station;
	String Destination_from[] = { "New Delhi", "Mumbai Cent.", "Sealdah",
			"H. Nizamuddin", "Bandra", "KLS", "Trivandrum Cent.",
			"Amritsar Jntn", "Bangalore City", "Chennai Cent", "Howrah Jntn",
			"Ahemdabad Jntn", "Allahabad" };
	String Destination_to[] = { "New Delhi", "Mumbai Cent.", "Sealdah",
			"H. Nizamuddin", "Bandra", "KLS", "Trivandrum Cent.",
			"Amritsar Jntn", "Bangalore City", "Chennai Cent", "Howrah Jntn",
			"Ahemdabad Jntn", "Allahabad" };
	ActionEvent ef, et;
	JComboBox from, to;
	JLabel lab;
	String username, password;

	Font font = new Font("Algerian", Font.PLAIN, 13);

	public static void main(final String args[]) {
		final School_Project_Test gui = new School_Project_Test();

		gui.go();
	}

	public void go() {

		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(1024, 768);
		frame.setLayout(null);

		Go = new JButton();
		Go.setBounds(384, 576, 254, 50);
		Go
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Go.jpg"));
		Go.addActionListener(new GoActionListener());
		frame.getContentPane().add(Go);

		Done = new JButton();
		Done
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Done.jpg"));
		Done.setBounds(384, 640, 254, 50);
		Done.addActionListener(new DoneActionListener());
		frame.getContentPane().add(Done);

		time_img = new JButton();
		time_img
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Time.jpg"));
		time_img.setBounds(60, 250, 100, 40);
		time_img.addActionListener(new time_imgActionListener());
		frame.getContentPane().add(time_img);

		station_img = new JButton();
		station_img
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Station.jpg"));
		station_img.setBounds(220, 250, 100, 40);
		station_img.addActionListener(new station_imgActionListener());
		frame.getContentPane().add(station_img);

		time = new JCheckBox("Time");
		time.setSelected(false);
		time.setBounds(60, 320, 60, 40);
		frame.getContentPane().add(time);

		station = new JCheckBox("Station");
		station.setSelected(true);
		station.setBounds(220, 320, 100, 40);
		frame.getContentPane().add(station);

		lab = new JLabel();
		lab
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\RailWay Welcome\\Slide2.jpg"));
		lab.setSize(1024, 768);
		frame.getContentPane().add(lab);

		from = new JComboBox(Destination_from);
		from.setEditable(true);
		from.setBounds(220, 450, 145, 30);
		from.setMaximumRowCount(4);
		from.setFont(font);
		frame.getContentPane().add(from);

		to = new JComboBox(Destination_to);
		to.setEditable(true);
		to.setMaximumRowCount(4);
		to.setBounds(60, 450, 145, 30);
		to.setFont(font);
		frame.getContentPane().add(to);

		b_from = new JButton();
		b_from
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\FROM\\From.jpg"));
		b_from.setBounds(220, 380, 100, 40);
		b_from.addActionListener(new b_fromActionListener());
		frame.getContentPane().add(b_from);

		b_to = new JButton();
		b_to
				.setIcon(new ImageIcon(
						"C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\FROM\\To.jpg"));
		b_to.setBounds(60, 380, 100, 40);
		b_to.addActionListener(new b_toActionListener());
		frame.getContentPane().add(b_to);

		frame.setVisible(true);
	}

	class time_imgActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ev) {
			JOptionPane.showMessageDialog(null,
					"Pls Select whether to show the train timings or not",
					"Select Value(Time)", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class station_imgActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ev) {
			JOptionPane.showMessageDialog(null,
					"Pls Select whether to show the Station details or not",
					"Select Value(Station)", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class b_fromActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ev) {
			JOptionPane.showMessageDialog(null,
					"Pls Select from where you have to go",
					"Select Value(Destination)",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class b_toActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ev) {
			JOptionPane.showMessageDialog(null,
					"Pls Select where you have to go",
					"Select Value(Destination)",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class DoneActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ev) {
			JOptionPane.showMessageDialog(null,
					"Thank You For Using My Program,Regards Abhinav",
					"Appreciation", JOptionPane.INFORMATION_MESSAGE);
			frame.setVisible(false);
		}
	}

	class fromActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent ef) {
			from = (JComboBox) ef.getSource();
		}
	}

	class toActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent et) {
			to = (JComboBox) et.getSource();
		}
	}

	class GoActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent eg) {
			if ((et)==(Destination_to[0])	&& (ef)==(Destination_from[0]))
			{
				JOptionPane.showMessageDialog(null, "Program Running",
						"Congrats!", JOptionPane.INFORMATION_MESSAGE);
			}

			else {
				JOptionPane.showMessageDialog(null, "Program Else Running",
						"Congrats!", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#5 Abhinav_BlackOp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-November 11

Re: Null Pointer Exception On Execution(AWT error)

Posted 05 November 2011 - 11:00 AM

Ok so I solved the problem.Simple just convert the selected item to string and compare them but another problem is that it just displays the else condition not the if condition.I have no idea why is doing so but trying to solve it will let you know if I find a way out untill then this is the program which is atleast giving outputs without errors



import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

class School_Project_Test {
	JFrame frame;
	JButton Go, Done, time_img, station_img, b_from, b_to;
	JCheckBox time, station;
	String Destination_from[] = { "New Delhi", "Mumbai Cent.", "Sealdah",
			"H. Nizamuddin", "Bandra", "KLS", "Trivandrum Cent.",
			"Amritsar Jntn", "Bangalore City", "Chennai Cent", "Howrah Jntn",
			"Ahemdabad Jntn", "Allahabad" };
	String Destination_to[] = { "New Delhi", "Mumbai Cent.", "Sealdah",
			"H. Nizamuddin", "Bandra", "KLS", "Trivandrum Cent.",
			"Amritsar Jntn", "Bangalore City", "Chennai Cent", "Howrah Jntn",
			"Ahemdabad Jntn", "Allahabad" };
	ActionEvent ef, et;
	JComboBox from, to;
	JLabel lab;
	String Dest_from,Dest_to;

	Font font = new Font("Algerian", Font.PLAIN, 13);

	public static void main( String args[])
	{
	   School_Project_Test gui = new School_Project_Test();

	   gui.go();
	}

	public void go() 
	{

		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(1024, 768);
		frame.setLayout(null);

		Go = new JButton();
		Go.setBounds(384, 576, 254, 50);
		Go.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Go.jpg"));
		Go.addActionListener(new GoActionListener());
		frame.getContentPane().add(Go);

		Done = new JButton();
		Done.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Done.jpg"));
		Done.setBounds(384, 640, 254, 50);
		Done.addActionListener(new DoneActionListener());
		frame.getContentPane().add(Done);

		time_img = new JButton();
		time_img.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Time.jpg"));
		time_img.setBounds(60, 250, 100, 40);
		time_img.addActionListener(new time_imgActionListener());
		frame.getContentPane().add(time_img);

		station_img = new JButton();
		station_img.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Station.jpg"));
		station_img.setBounds(220, 250, 100, 40);
		station_img.addActionListener(new station_imgActionListener());
		frame.getContentPane().add(station_img);

		time = new JCheckBox("Time");
		time.setSelected(false);
		time.setBounds(60, 320, 60, 40);
		frame.getContentPane().add(time);

		station = new JCheckBox("Station");
		station.setSelected(true);
		station.setBounds(220, 320, 100, 40);
		frame.getContentPane().add(station);

		lab = new JLabel();
		lab.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\RailWay Welcome\\Slide2.jpg"));
		lab.setSize(1024, 768);
		frame.getContentPane().add(lab);

		from = new JComboBox(Destination_from);
		from.setEditable(true);
		from.setBounds(220, 450, 145, 30);
		from.setMaximumRowCount(4);
		from.setFont(font);
		frame.getContentPane().add(from);

		to = new JComboBox(Destination_to);
		to.setEditable(true);
		to.setMaximumRowCount(4);
		to.setBounds(60, 450, 145, 30);
		to.setFont(font);
		frame.getContentPane().add(to);

		b_from = new JButton();
		b_from.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\FROM\\From.jpg"));
		b_from.setBounds(220, 380, 100, 40);
		b_from.addActionListener(new b_fromActionListener());
		frame.getContentPane().add(b_from);

		b_to = new JButton();
		b_to.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\FROM\\To.jpg"));
		b_to.setBounds(60, 380, 100, 40);
		b_to.addActionListener(new b_toActionListener());
		frame.getContentPane().add(b_to);

		frame.setVisible(true);
	}

	class time_imgActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ev)
		{
			JOptionPane.showMessageDialog(null,
					"Pls Select whether to show the train timings or not",
					"Select Value(Time)", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class station_imgActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ev)
		{
			JOptionPane.showMessageDialog(null,
					"Pls Select whether to show the Station details or not",
					"Select Value(Station)", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class b_fromActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ev)
		{
			JOptionPane.showMessageDialog(null,
					"Pls Select from where you have to go",
					"Select Value(Destination)",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class b_toActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ev)
		{
			JOptionPane.showMessageDialog(null,
					"Pls Select where you have to go",
					"Select Value(Destination)",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class DoneActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ev)
		{
			JOptionPane.showMessageDialog(null,
					"Thank You For Using My Program,Regards Abhinav",
					"Appreciation", JOptionPane.INFORMATION_MESSAGE);
			frame.setVisible(false);
		}
	}

	class fromActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ef)
		{
			from = (JComboBox) ef.getSource();
			Dest_from = (String)from.getSelectedItem();
		}
	}

	class toActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent et)
		{
			to = (JComboBox) et.getSource();
			Dest_to = (String)from.getSelectedItem();
		}
	}

	class GoActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent eg)
		{
			if (((Dest_to)==(Destination_to[0]))	&& ((Dest_from)==(Destination_from[0])))
			{
				JOptionPane.showMessageDialog(null, "Program Running",
						"Congrats!", JOptionPane.INFORMATION_MESSAGE);
			}

			else {
				JOptionPane.showMessageDialog(null, "Program Else Running",
						"Congrats!", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
}
 

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,579
  • Joined: 19-March 11

Re: Null Pointer Exception On Execution(AWT error)

Posted 05 November 2011 - 02:05 PM

You can't compare Strings with ==, you have to use the equals() method. string1 == string2 returns true if the two variables string1 and string2 happen to point to the same area in memory. string1.equals(string2) will return true iff the contents of the two String objects pointed to by the variables string1 and string2 happen to be equal according to the String.equals() logic.
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10780
  • View blog
  • Posts: 40,141
  • Joined: 27-December 08

Re: Null Pointer Exception On Execution(AWT error)

Posted 05 November 2011 - 08:07 PM

You shouldn't need to declare ActionEvents as instance fields. The Java GUI is set up so you won't need them for this type of project if you set your Listeners up correctly.
Was This Post Helpful? 2
  • +
  • -

#8 Abhinav_BlackOp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-November 11

Re: Null Pointer Exception On Execution(AWT error)

Posted 06 November 2011 - 05:44 AM

First of all I would like to thank everybody who helped me out I really appreciate your efforts.I finally got the program to execute without any runtime errors uptill now :bananaman: .I will be editing the program further and will let you people know if I encounter any error.

Now the problem is something which is so silly I cant believe I made it. :whistling: 1)I did not register the Combo Boxes with the actionListener and

2)did not call for their selection by the item state changed method.I know this is very silly but non spotted it.

So here is the program:-



import java.awt.Font;
import java.awt.event.*;
import java.awt.event.ItemListener.*;
import java.awt.event.ItemEvent.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

class School_Project_Test {
	JFrame frame;
	JButton Go, Done, time_img, station_img, b_from, b_to;
	JCheckBox time, station;
    

	
	JComboBox from, to;
	JLabel lab;
	String Dest_from,Dest_to;
   
	Font font = new Font("Algerian", Font.PLAIN, 13);
	

	  

     String Destination_from[] = { "New Delhi", "Mumbai Cent.", "Sealdah",
			"H. Nizamuddin", "Bandra", "KLS", "Trivandrum Cent.",
			"Amritsar Jntn", "Bangalore City", "Chennai Cent", "Howrah Jntn",
			"Ahemdabad Jntn", "Allahabad" };
     String Destination_to[] = { "New Delhi", "Mumbai Cent.", "Sealdah",
			"H. Nizamuddin", "Bandra", "KLS", "Trivandrum Cent.",
			"Amritsar Jntn", "Bangalore City", "Chennai Cent", "Howrah Jntn",
			"Ahemdabad Jntn", "Allahabad" };  

	public static void main( String args[])
	{
	   School_Project_Test gui = new School_Project_Test();

	   gui.go();
	}

	public void go() 
	{

		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(1024, 768);
		frame.setLayout(null);

		Go = new JButton();
		Go.setBounds(384, 576, 254, 50);
		Go.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Go.jpg"));
		Go.addActionListener(new GoActionListener());
		frame.getContentPane().add(Go);

		Done = new JButton();
		Done.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Done.jpg"));
		Done.setBounds(384, 640, 254, 50);
		Done.addActionListener(new DoneActionListener());
		frame.getContentPane().add(Done);

		time_img = new JButton();
		time_img.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Time.jpg"));
		time_img.setBounds(60, 250, 100, 40);
		time_img.addActionListener(new time_imgActionListener());
		frame.getContentPane().add(time_img);

		station_img = new JButton();
		station_img.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\Station.jpg"));
		station_img.setBounds(220, 250, 100, 40);
		station_img.addActionListener(new station_imgActionListener());
		frame.getContentPane().add(station_img);

		time = new JCheckBox("Time");
		time.setSelected(false);
		time.setBounds(60, 320, 60, 40);
		frame.getContentPane().add(time);

		station = new JCheckBox("Station");
		station.setSelected(true);
		station.setBounds(220, 320, 100, 40);
		frame.getContentPane().add(station);

		lab = new JLabel();
		lab.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\RailWay Welcome\\Slide2.jpg"));
		lab.setSize(1024, 768);
		frame.getContentPane().add(lab);

		from = new JComboBox(Destination_from);
		from.setEditable(true);
		from.setBounds(220, 450, 145, 30);
		from.setMaximumRowCount(4);
		from.setFont(font);
		from.addItemListener(new fromActionListener());
		frame.getContentPane().add(from);

		to = new JComboBox(Destination_to);
		to.setEditable(true);
		to.setMaximumRowCount(4);
		to.setBounds(60, 450, 145, 30);
		to.setFont(font);
		to.addItemListener(new toActionListener());
		frame.getContentPane().add(to);

		b_from = new JButton();
		b_from.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\FROM\\From.jpg"));
		b_from.setBounds(220, 380, 100, 40);
		b_from.addActionListener(new b_fromActionListener());
		frame.getContentPane().add(b_from);

		b_to = new JButton();
		b_to.setIcon(new ImageIcon("C:\\Documents and Settings\\Admin\\My Documents\\My Pictures\\Program\\FROM\\To.jpg"));
		b_to.setBounds(60, 380, 100, 40);
		b_to.addActionListener(new b_toActionListener());
		frame.getContentPane().add(b_to);

		frame.setVisible(true);
	}

	class time_imgActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ev)
		{
			JOptionPane.showMessageDialog(null,
					"Pls Select whether to show the train timings or not",
					"Select Value(Time)", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class station_imgActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ev)
		{
			JOptionPane.showMessageDialog(null,
					"Pls Select whether to show the Station details or not",
					"Select Value(Station)", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class b_fromActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ev)
		{
			JOptionPane.showMessageDialog(null,
					"Pls Select from where you have to go",
					"Select Value(Destination)",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class b_toActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ev)
		{
			JOptionPane.showMessageDialog(null,
					"Pls Select where you have to go",
					"Select Value(Destination)",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}

	class DoneActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent ev)
		{
			JOptionPane.showMessageDialog(null,
					"Thank You For Using My Program,Regards Abhinav",
					"Appreciation", JOptionPane.INFORMATION_MESSAGE);
			frame.setVisible(false);
		}
	}

	class fromActionListener implements ItemListener
	{
		public void itemStateChanged(ItemEvent ief)
		{
			from = (JComboBox)ief.getSource();
			Dest_from = (String)ief.getItem();
		}
	}

	class toActionListener implements ItemListener
	{
		public void itemStateChanged(ItemEvent iet)
		{
			
			Dest_to = (String)iet.getItem();
		}
	}

	class GoActionListener implements ActionListener
	{
		public void actionPerformed( ActionEvent eg)
		{
			if ((Dest_to)==("New Delhi")&& (Dest_from)==("New Delhi"))
			{
				JOptionPane.showMessageDialog(null, "Program Running",
						"Congrats!", JOptionPane.INFORMATION_MESSAGE);
			}

			else {
				JOptionPane.showMessageDialog(null, "Program Else Running",
						"Congrats!", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1