java.lang.NullPointerException Error Help

Error when passing text from JTextField to a class array

Page 1 of 1

3 Replies - 974 Views - Last Post: 26 March 2009 - 03:44 PM Rate Topic: -----

#1 MasterAchilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-March 09

java.lang.NullPointerException Error Help

Posted 25 March 2009 - 09:42 PM

I am getting a java.lang.NullPointerException everytime I attemp to press the Enter Activity button in my applet.

My first code here is for a class I created.
package PERT_Project;

public class PERT_SETUP 
{
	String activity;
	String description;
	int earliestStart;		
	int duration;
	int earliestFinish;
	String successor1;
	String successor2;
	String successor3;
	String successor4;
	
	public PERT_SETUP()
	{
		activity = "";
		description = "";
		duration = 0;
		successor1 = "";
		successor2 = "";
		successor3 = "";
		successor4 = "";
	}
	
	public PERT_SETUP(String act, String descrip, int time, String success1, String success2, 
					  String success3, String success4)
	{
		setActivity(act);
		setDescription(descrip);
		setDuration(time);
		setSuccessor1(success1);
		setSuccessor2(success2);
		setSuccessor3(success3);
		setSuccessor4(success4);
	}
	
	public void setActivity(String act)
	{
		activity = act;
	}
	
	public void setDescription(String descrip)
	{
		description = descrip;
	}
	
	public void setDuration(int time)
	{
		duration = time;
	}
	
	public void setSuccessor1(String success1)
	{
		successor1 = success1;
	}
	
	public void setSuccessor2(String success2)
	{
		successor2 = success2;
	}
	
	public void setSuccessor3(String success3)
	{
		successor3 = success3;
	}
	
	public void setSuccessor4(String success4)
	{
		successor4 = success4;
	}
	
	public void setEarliestStart(int es)
	{
		earliestStart = es;
	}
	
	public void setEarliestFinish(int ef)
	{
		earliestFinish = ef;
	}
}



This next code is where I am using this class to create the applet. The error occurs when I press the button and attempt to enter the data I wrote in the applets textfield into the array of this class I created at the top of this code:

It is in the actionPerformed() that the error is occurring. I know it must have something to do with me setting the get values to this class array.

You can ignore the public JPanel createPERT_Table() since I am not having problems with it.(Yet)

The problem first occurs at paths[index].setActivity(tempAct);
 
package PERT_Project;
import java.awt.*;

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

public class PERT_Data extends JApplet implements ActionListener
{
	// Initialize Pert_SETUP to store info about each node of the graph.
	public PERT_SETUP[] paths = new PERT_SETUP[26];
	
	
	// Initialize Panels to go through the different screens of the applet.
	// 1: PERT_GetData(): Get info about nodes from each user.
	// 2: PERT_Table(): Print out JTable for user
	// 3: PERT_Graph(): Print out Graph with nodes and lines.
	private JPanel dataPanel;
	private JPanel finalTablePanel;
	private JPanel graphPanel;
	
	// inputPERT_Data
	private JLabel activityLabel;
	private JTextField activityField;
	
	private JLabel descriptionLabel;
	private JTextField descriptionField;
	
	private JLabel durationLabel;
	private JTextField durationField;
	
	private JLabel successorsLabel;
	private JTextField successorField1;
	private JTextField successorField2;
	private JTextField successorField3;
	private JTextField successorField4;

	private JButton enterButton;
	private JButton createTableButton;
	
	// createPERT_Table
	private JButton createGraphButton;
	private JTable PERT_Table;
	
	// Action Performed
	int index = 0;
	
	private boolean tableCreated = false;
	private boolean criticalPathCreated = false;
	
	// Initialize applet and construct the GUI.
	public void init()
	{
		try
		{
			SwingUtilities.invokeAndWait(new Runnable()
			{
				public void run()
				{
					tableCreated = false;
					criticalPathCreated = false;
					
					//Creates a box layout for the JPanels.
					getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
					
					setSize(500, 500);
					
					add(PERT_GetData());
					
					
				}
			});
		} 
		catch(Exception e)
		{
			System.out.println("Can't create because of " + e);
		}
	}
	
	// Initial GUI for User input data
	public JPanel PERT_GetData()
	{
		// Set Layout
		setLayout(new FlowLayout());
		
		// Create a new JPanel to hold all elements.
		dataPanel = new JPanel();
		
		// Set Preferred Size of Panel
		dataPanel.setPreferredSize(new Dimension(300, 300));
		
		dataPanel.setOpaque(true);
		
		dataPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
		
		// Create Labels
		activityLabel = new JLabel("Enter Activity Character: ");
		descriptionLabel = new JLabel("Enter a short Description for this Activity.");
		durationLabel = new JLabel("Enter the time required to perform this Activity.");
		successorsLabel = new JLabel("Enter all of the successors of this Activity.");
		
		// Create Text Fields.
		activityField = new JTextField(5);
		descriptionField = new JTextField(20);
		durationField = new JTextField(15);
		successorField1 = new JTextField(5);
		successorField2 = new JTextField(5);
		successorField3 = new JTextField(5);
		successorField4 = new JTextField(5);
		
		// Create Enter button.
		enterButton = new JButton("Enter Activity Data");
		
		// Create Table Button.
		createTableButton = new JButton("Create Table");
		
		// Add Action Listener to enter button.
		enterButton.addActionListener(this);
		
		// Add Action Listener to create Table Button.
		createTableButton.addActionListener(this);
		
		// Add the components to the JPanel.
		dataPanel.add(activityLabel);
		dataPanel.add(activityField);
		dataPanel.add(descriptionLabel);
		dataPanel.add(descriptionField);
		dataPanel.add(durationLabel);
		dataPanel.add(durationField);
		dataPanel.add(successorsLabel);
		dataPanel.add(successorField1);
		dataPanel.add(successorField2);
		dataPanel.add(successorField3);
		dataPanel.add(successorField4);
		dataPanel.add(enterButton);
		dataPanel.add(createTableButton);
		
		dataPanel.validate();
		
		// Return panel holding data from user to the applet.
		return dataPanel;
	}
	
	public JPanel createPERT_Table()
	{
		createGraphButton = new JButton("Create Graph");
		createGraphButton.setMaximumSize(new Dimension(50, 25));
		
		// Press to create graph
		createGraphButton.addActionListener(this);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(createGraphButton, BorderLayout.CENTER);
	
		JPanel headerArea = new JPanel();
		headerArea.setLayout(new BorderLayout());
		headerArea.add(PERT_Table.getTableHeader(), BorderLayout.LINE_START);
		
		// Create area that holds entire Table Panel
		// Initialize each panels place in this main panel.
		JPanel tablePanel = new JPanel();
		tablePanel.setLayout(new BorderLayout());
		
		// Header goes at the top of tablePanel(NORTH)
		tablePanel.add(headerArea, BorderLayout.PAGE_START);
		
		// The actual Table(PERT_Table) goes to the EAST.
		tablePanel.add(PERT_Table, BorderLayout.LINE_START);
		
		// Button goes to the SOUTH.
		tablePanel.add(buttonPanel, BorderLayout.PAGE_END);
		
		JPanel finalTablePanel = new JPanel();
		finalTablePanel.setLayout(new BorderLayout());
		finalTablePanel.add(tablePanel, BorderLayout.LINE_START);
		
		finalTablePanel.validate();
		
		// Put panel holding table onto the applet.
		return finalTablePanel;
	}
	
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getActionCommand().equals("Enter Activity Data"))
		{
			String tempAct = activityField.getText();
			System.out.println(tempAct);
			
			// Set Activity
			paths[index].setActivity(tempAct);
			activityField.setText("");
			
			// Set Description
			paths[index].setDescription(descriptionField.getText());
			descriptionField.setText("");
			
			// Set Duration
			paths[index].setDuration(Integer.parseInt(durationField.getText()));
			durationField.setText("");
			
			// Set Successors
			paths[index].setSuccessor1(successorField1.getText());
			paths[index].setSuccessor2(successorField2.getText());
			paths[index].setSuccessor3(successorField3.getText());
			paths[index].setSuccessor4(successorField4.getText());
			
			// Clear any info in text Fields
			successorField1.setText("");
			successorField2.setText("");
			successorField3.setText("");
			successorField4.setText("");
			
			index = index + 1;
		}
		
		else if(ae.getActionCommand().equals("Create Table"))
		{
			if(index > 0 && !tableCreated)
			{
				int rows = index + 1;
				
				PERT_Table = new JTable(new PERT_TableModel(rows, paths));
				
				add(createPERT_Table());
				
				tableCreated = true;
				
				remove(dataPanel);
			}
		}
	}
}


This post has been edited by MasterAchilles: 25 March 2009 - 09:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: java.lang.NullPointerException Error Help

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: java.lang.NullPointerException Error Help

Posted 25 March 2009 - 09:52 PM

Maybe try using the actual button
   if(ae.getActionCommand().equals(enterbutton));



Edit - also maybe another approach to it could be to Make an array of JButton[0] to JButton[x amount] put 0,1,2,3,..x into them put an ActionListener on them. Then just compare the index.

This post has been edited by Fuzzyness: 25 March 2009 - 10:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 MasterAchilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-March 09

Re: java.lang.NullPointerException Error Help

Posted 25 March 2009 - 10:00 PM

View PostFuzzyness, on 25 Mar, 2009 - 08:52 PM, said:

Maybe try using the actual button
   if(ae.getActionCommand().equals(enterbutton));



Edit - also maybe another approach to it could be to Make an array of JButton[0] to JButton[x amount] put 0,1,2,3,..x into them put an ActionListener on them. Then just compare the index. if(indexOf(enterbutton) ==0 ) then add code. Something like that.


If I did that would I still be able to have a button on the applet itself?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java.lang.NullPointerException Error Help

Posted 26 March 2009 - 03:44 PM

View PostMasterAchilles, on 25 Mar, 2009 - 09:00 PM, said:

View PostFuzzyness, on 25 Mar, 2009 - 08:52 PM, said:

Maybe try using the actual button
   if(ae.getActionCommand().equals(enterbutton));



Edit - also maybe another approach to it could be to Make an array of JButton[0] to JButton[x amount] put 0,1,2,3,..x into them put an ActionListener on them. Then just compare the index. if(indexOf(enterbutton) ==0 ) then add code. Something like that.


If I did that would I still be able to have a button on the applet itself?


That is the more effective way of doing it if the actionPerformed() method has access to the variables (which is your case):

 
public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource() == enterButton)



Any post your trace of the execption.. may be has nothing to do with the way you detect which button was pressed

This post has been edited by pbl: 26 March 2009 - 03:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1