Keep getting non-static method errors

I keep getting "non-static method cannot be read from static con

Page 1 of 1

3 Replies - 2697 Views - Last Post: 15 May 2010 - 08:41 PM Rate Topic: -----

#1 kiph  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-May 10

Keep getting non-static method errors

Posted 15 May 2010 - 06:24 AM

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.io.*;

public class AdminSystem
{
	public static void main(String[] args)
	{
		JFrame frame = new AdminSystemFrame();
		frame.setVisible(true);
	}
}

class AdminSystemFrame extends JFrame
{
	public AdminSystemFrame()
	{
		setTitle("Human Resources Graphical Administration System");
		setSize(500,100);
		centerWindow(this);
		setResizable(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.add(new AdminSystemPanel());
		this.pack();
	}
	
	private void centerWindow(Window w)
	{
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension d = tk.getScreenSize();
		setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
	}
}

class AdminSystemPanel extends JPanel
{
	ArrayList<Department> departments = new ArrayList<Department>();
	ArrayList<Employee> employees = new ArrayList<Employee>();
	departmentPanel newDepartmentPanel;
	employeePanel newEmployeePanel;
	bottomPanel newBottomPanel;
	Department newDepartment = null;
	Employee newEmployee = null;
	
	private JButton createReportButton, quitButton;
	
	private GridBagConstraints getConstraints(int gridx, int gridy,
    int gridwidth, int gridheight, int anchor)
    {
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(1, 1, 1, 1);
        c.ipadx = 0;
        c.ipady = 0;
        c.gridx = gridx;
        c.gridy = gridy;
        c.gridwidth = gridwidth;
        c.gridheight = gridheight;
        c.anchor = anchor;
        return c;
    }
	
	public AdminSystemPanel()
	{
		setLayout(new GridBagLayout());
        newDepartmentPanel = new departmentPanel();
        add(newDepartmentPanel, getConstraints(0,0,1,1, GridBagConstraints.WEST));
        newEmployeePanel = new employeePanel();
        add(newEmployeePanel, getConstraints(0,1,1,1, GridBagConstraints.WEST));
        newBottomPanel = new bottomPanel();
        add(newBottomPanel, getConstraints(0,2,1,1, GridBagConstraints.WEST));
        
        employeePanel.showEmployee(employees.get(0));
        employeePanel.selectEmployee(employees.get(0));
	}
	
	class departmentPanel extends JPanel
	{
		private JTextField departmentNameTextField, departmentLocationTextField;
		private JLabel departmentNameLabel, departmentLocationLabel;
		private JButton addNewDepartmentButton, setDepartmentHeadButton, acceptNewDepartmentButton, deleteDepartmentButton;
		private JComboBox departmentComboBox;
		
		boolean filling = false;
		
		public departmentPanel()
		{
			setLayout(new FlowLayout(FlowLayout.LEFT));
		
			departmentComboBox = new JComboBox();
			fillComboBox(departments);
			departmentComboBox.addActionListener(new departmentComboBoxListener());
			add(departmentComboBox);
			
			departmentNameLabel = new JLabel("Department Name: ");
			add(departmentNameLabel);
		
			departmentNameTextField = new JTextField(10);
			add(departmentNameTextField);
		
			departmentLocationLabel = new JLabel("Department Location: ");
			add(departmentLocationLabel);
		
			departmentLocationTextField = new JTextField(10);
			add(departmentLocationTextField);
		
			JPanel buttonPanel = new JPanel();
			buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		
			addNewDepartmentButton = new JButton("Add New Department");
			addNewDepartmentButton.addActionListener(new addNewDepartmentListener());
			add(addNewDepartmentButton);
			
			acceptNewDepartmentButton = new JButton("Accept");
			acceptNewDepartmentButton.addActionListener(new acceptNewDepartmentListener());
			add(acceptNewDepartmentButton);
			
			setDepartmentHeadButton = new JButton("Set Department Head");
			setDepartmentHeadButton.addActionListener(new setDepartmentHeadListener());
			add(setDepartmentHeadButton);
		
			deleteDepartmentButton = new JButton("Delete Department");
			deleteDepartmentButton.addActionListener(new deleteDepartmentListener());
			add(deleteDepartmentButton);
		}
		
		public void fillComboBox(ArrayList<Department> departments)
        {
            filling = true;
            departmentComboBox.removeAllItems();
            for (Department d : departments)
                departmentComboBox.addItem(d);
            filling = false;
        }
        
        public void selectDepartment(Department d)
        {
            departmentComboBox.setSelectedItem(d);
        }
        
        public Department getCurrentDepartment()
        {
            return (Department) departmentComboBox.getSelectedItem();
        }
        
        public void showDepartment(Department d)
        {
            departmentNameTextField.setText(d.getDepartmentName());
            departmentLocationTextField.setText(d.getDepartmentLocation());
        }
        
        public void setAddEditMode(boolean e)
        {
            departmentNameTextField.setEditable(e);
            departmentNameTextField.setFocusable(e);
            departmentNameTextField.requestFocusInWindow();
            departmentLocationTextField.setEditable(e);
            departmentLocationTextField.setFocusable(e);
        }
        
        public void clearFields()
        {
            departmentNameTextField.setText("");
            departmentLocationTextField.setText("");
        }
        
        public void setEditableFields(boolean e)
        {
        	departmentNameTextField.setEditable(e);
			departmentNameTextField.setFocusable(e);
			departmentLocationTextField.setEditable(e);
            departmentLocationTextField.setFocusable(e);
        }
	}
	
	class employeePanel extends JPanel
	{
		
		public JTextField employeeFirstNameTextField, employeeSurnameTextField, employeeAddressTextField;
		public JLabel employeeFirstNameLabel, employeeSurnameLabel, employeeAddressLabel, employeeGenderLabel, employeePayLevelLabel;
		public JButton addNewEmployeeButton, changeEmployeeDetailsButton, listEmployeesButton, deleteEmployeeButton;
		public JPanel genderPanel;
		public JComboBox sexbox, paybox, employeeComboBox;
		
		boolean filling = false;
		
		public employeePanel()
		{
			setLayout(new FlowLayout(FlowLayout.CENTER));
			
			employeeFirstNameLabel = new JLabel("Employee First Name: ");
			add(employeeFirstNameLabel);
		
			employeeFirstNameTextField = new JTextField(10);
			add(employeeFirstNameTextField);
		
			employeeSurnameLabel = new JLabel("Employee Surname: ");
			add(employeeSurnameLabel);
		
			employeeSurnameTextField = new JTextField(10);
			add(employeeSurnameTextField);
		
			employeeAddressLabel = new JLabel("Employee Address: ");
			add(employeeAddressLabel);
		
			employeeAddressTextField = new JTextField(30);
			add(employeeAddressTextField);
			
			addNewEmployeeButton = new JButton("Add New Employee");
			addNewEmployeeButton.addActionListener(new addEmployeeListener());
			add(addNewEmployeeButton);
		
			changeEmployeeDetailsButton = new JButton("Modify Employee Details");
			changeEmployeeDetailsButton.addActionListener(new employeeDetailsListener());
			add(changeEmployeeDetailsButton);
		
			deleteEmployeeButton = new JButton("Delete Employee");
			deleteEmployeeButton.addActionListener(new deleteEmployeeListener());
			add(deleteEmployeeButton);
		
			listEmployeesButton = new JButton("List Employees");
			listEmployeesButton.addActionListener(new listEmployeesListener());
			add(listEmployeesButton);
		
			createReportButton = new JButton("Create Report");
			createReportButton.addActionListener(new createReportListener());
			add(createReportButton);
		
			genderPanel = new JPanel();
			employeeGenderLabel = new JLabel("Employee Gender: ");
			genderPanel.add(employeeGenderLabel);
			sexbox = new JComboBox();
			sexbox.addItem("M");
			sexbox.addItem("F");
			genderPanel.add(sexbox);
			employeePayLevelLabel = new JLabel("Payment Level: ");
		genderPanel.add(employeePayLevelLabel);
		paybox = new JComboBox();
		paybox.addItem("1");
		paybox.addItem("2");
		paybox.addItem("3");
		paybox.addItem("4");
		paybox.addItem("5");
		paybox.addItem("6");
		paybox.addItem("7");
		paybox.addItem("8");
		genderPanel.add(paybox);
		this.add(genderPanel);
	//	set
//		this.add(quitPanel, BorderLayout.SOUTH);*/
		}
		
		public Employee getEmployee()
        {
            Employee emp = new Employee(employeeFirstNameTextField.getText(), employeeSurnameTextField.getText(), employeeAddressTextField.getText(), sexbox.getSelectedIndex(), paybox.getSelectedIndex());
            return emp;
        }
		
		public void clearFields()
        {
            employeeFirstNameTextField.setText("");
            employeeSurnameTextField.setText("");
            employeeAddressTextField.setText("");
        }
        
        public void fillComboBox(ArrayList<Employee> employees)
        {
            filling = true;
            employeeComboBox.removeAllItems();
            for (Employee e : employees)
                employeeComboBox.addItem(e);
            filling = false;
        }
        
        public void selectEmployee(Employee e)
        {
            employeeComboBox.setSelectedItem(e);
        }
        
        public Employee getCurrentEmployee()
        {
            return (Employee) employeeComboBox.getSelectedItem();
        }
        
        public void showEmployee(Employee e)
        {
            employeeFirstNameTextField.setText(e.getEmployeeFirstName());
            employeeSurnameTextField.setText(e.getEmployeeSurname());
            employeeAddressTextField.setText(e.getEmployeeAddress());
        }
            	
		public void setAddEditMode(boolean e)
    	{
        	employeeFirstNameTextField.setEditable(e);
        	employeeFirstNameTextField.setFocusable(e);
        	employeeFirstNameTextField.requestFocusInWindow();
        	employeeSurnameTextField.setEditable(e);
        	employeeSurnameTextField.setFocusable(e);
        	employeeAddressTextField.setEditable(e);
        	employeeAddressTextField.setFocusable(e);
    	}
    	
    	public void actionPerformed(ActionEvent e)
        {
            if (!filling)
            {
                Employee currentEmployee = (Employee)employeeComboBox.getSelectedItem();
                employeePanel.showEmployee(currentEmployee);
            }
        }
        
        public void setEditableFields(boolean e)
        {
        	employeeFirstNameTextField.setEditable(e);
        	employeeFirstNameTextField.setFocusable(e);
        	employeeSurnameTextField.setEditable(e);
        	employeeSurnameTextField.setFocusable(e);
        	employeeAddressTextField.setEditable(e);
        	employeeAddressTextField.setFocusable(e);
        }
	}
	
	class bottomPanel extends JPanel
	{
		public bottomPanel()
		{
			JPanel quitPanel = new JPanel();
			quitPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
			quitButton = new JButton("Quit Program");
			quitButton.addActionListener(new quitListener());
			quitPanel.add(quitButton);
			
			setLayout(new BorderLayout());
			add(quitPanel, BorderLayout.SOUTH);
		}
	}

	class addNewDepartmentListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			newDepartment = new Department();
		}
		
		public boolean isValidData()
        {
            return SwingValidator.isPresent(departmentPanel.departmentNameTextField, "Department Name")
                    && SwingValidator.isPresent(departmentPanel.departmentLocationTextField, "Department Location");
        }
	}
	
	class acceptNewDepartmentListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	class departmentComboBoxListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if (!filling)
            {
                Department d = (Department)departmentComboBox.getSelectedItem();
                departmentPanel.showDepartment(d);
            }
		}
	}
	
	class setDepartmentHeadListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	class listEmployeesListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	class quitListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	class acceptNewEmployeeListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if (isValidData())
            {
                if (newEmployee != null)
                {
                    newEmployee = newEmployeePanel.getEmployee();
                    employees.add(newEmployee);
                    employeePanel.fillComboBox(employees);
                    employeePanel.selectEmployee(newEmployee);
                    newEmployee = null;
                }
                else
                {
   /*                 Player p = selectorPanel.getCurrentPlayer();
                    Player newPlayer = playerPanel.getPlayer();
                    p.setLastName(newPlayer.getLastName());
                    p.setFirstName(newPlayer.getFirstName());
                    p.setNumber(newPlayer.getNumber());
                    TeamIO.saveTeam(team);
                    selectorPanel.fillComboBox(team);
                    selectorPanel.selectPlayer(p);
                 playerPanel.showPlayer(selectorPanel.getCurrentPlayer()); */
                }
                employeePanel.setAddEditMode(false);
                buttonPanel.setAddEditMode(false);
                selectorPanel.playerComboBox.requestFocusInWindow();
            }
		}
		
		public boolean isValidData()
        {
            return SwingValidator.isPresent(employeePanel.employeeFirstNameTextField, "First Name")
                    && SwingValidator.isPresent(employeePanel.employeeSurnameTextField, "Surname")
                    && SwingValidator.isPresent(employeePanel.employeeAddressTextField, "Address");
        }
	}
	
	class addEmployeeListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			newEmployee = new Employee();
		}
	}
	
	class deleteDepartmentListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            Department d = departmentPanel.getCurrentDepartment();
            departments.remove(d);
            departmentPanel.fillComboBox(departments);
            departmentPanel.selectDepartment(departments.get(0));
            departmentPanel.showPlayer(team.get(0));
            selectorPanel.playerComboBox.requestFocusInWindow();
        }
    }
    
    class deleteEmployeeListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            Employee e = employeePanel.getCurrentEmployee();
            employees.remove(e);
            employeePanel.fillComboBox(employee);
            employeePanel.selectemployee(employees.get(0));
            employeePanel.showPlayer(employees.get(0));
            employeePanel.playerComboBox.requestFocusInWindow();
        }
    }
	
	class EditListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            employeePanel.setAddEditMode(true);
        }
    }
    
    class employeeDetailsListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    class createReportListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
        }
    }
    
    class departmentCancelListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (newDepartment != null)
            {
                newDepartment = null;
            }
            employeePanel.setAddEditMode(false);
            departmentPanel.setAddEditMode(false);
            departmentPanel.showDepartment(departmentPanel.getCurrentDepartment());
            departmentPanel.departmentComboBox.requestFocusInWindow();
        }
    }
    
    class employeeCancelListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (newEmployee != null)
            {
                newEmployee = null;
            }
            employeePanel.setAddEditMode(false);
            departmentPanel.setAddEditMode(false);
            employeePanel.showEmployee(employeePanel.getCurrentEmployee());
            employeePanel.employeeComboBox.requestFocusInWindow();

        }
    }


Whenever I try to compile this program I keep getting "non-static method cannot be referenced from a static context" type errors. For example:

C:\Users\Daniel\Documents\work\java\Assignment 2\AdminSystem.java:74: non-static method showEmployee(Employee) cannot be referenced from a static context
        employeePanel.showEmployee(employees.get(0));
                     ^
C:\Users\Daniel\Documents\work\java\Assignment 2\AdminSystem.java:75: non-static method selectEmployee(Employee) cannot be referenced from a static context
        employeePanel.selectEmployee(employees.get(0));
                     ^
C:\Users\Daniel\Documents\work\java\Assignment 2\AdminSystem.java:309: non-static method showEmployee(Employee) cannot be referenced from a static context
                employeePanel.showEmployee(currentEmployee);
                             ^
C:\Users\Daniel\Documents\work\java\Assignment 2\AdminSystem.java:349: non-static variable departmentNameTextField cannot be referenced from a static context
            return SwingValidator.isPresent(departmentPanel.departmentNameTextField, "Department Name")
                                                           ^
C:\Users\Daniel\Documents\work\java\Assignment 2\AdminSystem.java:350: non-static variable departmentLocationTextField cannot be referenced from a static context
                    && SwingValidator.isPresent(departmentPanel.departmentLocationTextField, "Department Location");


Can someone please tell me what im doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Keep getting non-static method errors

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Keep getting non-static method errors

Posted 15 May 2010 - 07:15 AM

your arraylist in the AdminSystemPanel ckass is not static and you are getting the elements of the list without creating the object of the AdminSystemPanel class
there are two solutions
create the object of the class and then access the list as follows
AdminSystemPanel obj = new AdminSystemPanel();

and for getting the elements or using the methods of the arraylists, use
obj.employees.get(0);

or the best way
simply declare the Arraylist employees as static as follows
static ArrayList<Employee> employees = new ArrayList<Employee>();

hope this hint helps
Was This Post Helpful? 1
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Keep getting non-static method errors

Posted 15 May 2010 - 09:17 AM

You are using the class names, not the instance names:

...
employeePanel newEmployeePanel;
...
newEmployeePanel = new employeePanel();
...
employeePanel.showEmployee(employees.get(0));
employeePanel.selectEmployee(employees.get(0));
...



should be

...
employeePanel newEmployeePanel;
...
newEmployeePanel = new employeePanel();
...
newEmployeePanel.showEmployee(employees.get(0));
newEmployeePanel.selectEmployee(employees.get(0));
...


Was This Post Helpful? 2
  • +
  • -

#4 kiph  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-May 10

Re: Keep getting non-static method errors

Posted 15 May 2010 - 08:41 PM

View Postn8wxs, on 15 May 2010 - 08:17 AM, said:

You are using the class names, not the instance names:

...
employeePanel newEmployeePanel;
...
newEmployeePanel = new employeePanel();
...
employeePanel.showEmployee(employees.get(0));
employeePanel.selectEmployee(employees.get(0));
...



should be

...
employeePanel newEmployeePanel;
...
newEmployeePanel = new employeePanel();
...
newEmployeePanel.showEmployee(employees.get(0));
newEmployeePanel.selectEmployee(employees.get(0));
...



Thanks so much, this fixed it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1