Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Use of a parameterized constructor in GUI(Difficulties with using a parameterized constructor from an abstract class in a GUI)
Use of a parameterized constructor in GUI [message #1818977] Sat, 04 January 2020 00:49 Go to next message
Gonzalo Cofre Real is currently offline Gonzalo Cofre RealFriend
Messages: 1
Registered: January 2020
Junior Member
Hello there, I'm Gonzalo from Germany.
I'm releativley new to Java and I'm trying to learn more and more about programming every day. I'm using Eclipse Neon to program. After learning how to combine a parameterized constructor from an abstract class with inheriting classes and how to design a GUI, I started thinking about putting both together,

So I created a new project ment to be used as a registration form to store students and teachers. I wanted to use a parameterized constructor and it did work in a different project, the only problem there was that I needed to initiate a new object for every method I wrote. Back to the current project I designed a GUI with multiple JComboBoxes, JTextFields, JButtons and a JRadioButton. (I will put the code below) To create a object, I had to use the parameterized constructor and the first challange began. JComboBoxes can only give back the value "Object" so I needed to change the type from the attribute from String to Object and it did work out. JTextFields were no problem as well as the buttons and the JRadioButton is still a mistery to me.

At first everything seemed to be working just fine cause Eclipse didn't show any errors in the text. But at the very moment I wanted to create an object from the GUI this message popped up:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

I don't really know what that means, but after that message, various other messages underneath linked me to the line where I created a new object with the parameterized constructor.

How do I solve this problem pls I need help.

Abstract Class:
package package_Fachklassen;

public abstract class Personen {
	
	private Object anrede;
	private String vorname;
	private String nachname;
	private double alter;
	private Object klasse;
	private String ort;
	private String strasse;
	private String plz;
	private Object gbt;
	private Object gbj;
	private Object gbm;
	

// 	Parameterized constructor
	
	public Personen(Object anrede, String vorname, String nachname, double alter, Object klasse, String ort, String strasse, String plz, Object gbt,
			Object gbj, Object gbm) {
		super();
		this.anrede = anrede;
		this.vorname = vorname;
		this.nachname = nachname;
		this.alter = alter;
		this.klasse = klasse;
		this.ort = ort;
		this.strasse = strasse;
		this.plz = plz;
		this.gbt = gbt;
		this.gbj = gbj;
		this.gbm = gbm;
		

	}
	public double getAlter() {
		return alter;
	}
	public void setAlter(double alter) {
		this.alter = alter;
	}
	public String getVorname() {
		return vorname;
	}
	public void setVorname(String vorname) {
		this.vorname = vorname;
	}
	public String getNachname() {
		return nachname;
	}
	public void setNachname(String nachname) {
		this.nachname = nachname;
	}
	public Object getKlasse() {
		return klasse;
	}
	public void setKlasse(String klasse) {
		this.klasse = klasse;
	}
	public String getOrt() {
		return ort;
	}
	public void setOrt(String ort) {
		this.ort = ort;
	}
	public String getStrasse() {
		return strasse;
	}
	public void setStrasse(String strasse) {
		this.strasse = strasse;
	}
	public String getPlz() {
		return plz;
	}
	public void setPlz(String plz) {
		this.plz = plz;
	}
	public Object getGbt() {
		return gbt;
	}
	public void setGbt(int gbt) {
		this.gbt = gbt;
	}
	public Object getGbj() {
		return gbj;
	}
	public void setGbj(Object object) {
		this.gbj = object;
	}
	public Object getGbm() {
		return gbm;
	}
	public void setGbm(int gbm) {
		this.gbm = gbm;
	}
}


1. Inhereting Class
package package_Fachklassen;

public class Lehrer extends Personen {
	
	private Object gehaltklasse;
	private double stunden;
	private boolean titel;
	

	public Lehrer(Object anrede, String vorname, String nachname, double alter, Object klasse, String ort, String strasse,
			String plz, Object gbt, Object gbj, Object gbm, Object gehaltklasse, double stunden) {
		super(anrede, vorname, nachname, alter, klasse, ort, strasse, plz, gbt, gbj, gbm);
		this.gehaltklasse = gehaltklasse;
		this.stunden = stunden;
		this.titel = titel;
		

	}


	public Object getGehaltklasse() {
		return gehaltklasse;
	}


	public void setGehaltklasse(String gehaltklasse) {
		this.gehaltklasse = gehaltklasse;
	}


	public double getStunden() {
		return stunden;
	}


	public void setStunden(double stunden) {
		this.stunden = stunden;
	}


	public boolean getTitel() {
		return titel;
	}


	public void setTitel(boolean titel) {
		this.titel = titel;
	}
}



GUI:

package package_GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;

import package_Fachklassen.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerDateModel;
import java.util.Date;
import java.util.Calendar;
import javax.swing.SpinnerNumberModel;
import javax.swing.JRadioButton;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class LehrerFenster_GUI extends JFrame {

	private JPanel contentPane;
	private JTextField tfName;
	private JTextField tfVorname;
	private JTextField tfStrasse;
	private JTextField tfNr;
	private JTextField tfOrt;
	private JTextField tfPlz;
	private JTextField tfStunden;
	private JTextField tfAlter;
	private JRadioButton rdbtnDrTitel;
	private JComboBox comboAnrede;
	private JComboBox comboKlasse;
	private JComboBox comboTag;
	private JComboBox comboJahr;
	private JComboBox comboMonat;
	private JComboBox comboGehaltsklasse;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LehrerFenster_GUI frame = new LehrerFenster_GUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LehrerFenster_GUI() {
		setTitle("Lehrer Formular");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 700);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblTitel = new JLabel("");
		lblTitel.setIcon(new ImageIcon(Entscheidung_GUI.class.getResource("/package_Icons/KSN.jpg")));
		lblTitel.setBounds(10, 11, 140, 53);
		ImageIcon MyImage = new ImageIcon(Entscheidung_GUI.class.getResource("/package_Icons/KSN.jpg"));
		Image img = MyImage.getImage();
		Image newimg = img.getScaledInstance(lblTitel.getWidth(), lblTitel.getHeight(), Image.SCALE_SMOOTH);
		ImageIcon image = new ImageIcon(newimg);
		lblTitel.setIcon(image);
		contentPane.add(lblTitel);
		
		JLabel lblAnmeldeformular = new JLabel("Anmeldeformular:");
		lblAnmeldeformular.setFont(new Font("Dialog", Font.BOLD, 16));
		lblAnmeldeformular.setHorizontalAlignment(SwingConstants.CENTER);
		lblAnmeldeformular.setBounds(198, 39, 140, 14);
		contentPane.add(lblAnmeldeformular);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblName.setBounds(10, 146, 46, 14);
		contentPane.add(lblName);
		
		JLabel lblVorname = new JLabel("Vorname:");
		lblVorname.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblVorname.setBounds(180, 146, 53, 14);
		contentPane.add(lblVorname);
		
		tfName = new JTextField();
		tfName.setFont(new Font("Dialog", Font.PLAIN, 12));
		tfName.setBounds(10, 165, 140, 20);
		contentPane.add(tfName);
		tfName.setColumns(10);
		
		tfVorname = new JTextField();
		tfVorname.setFont(new Font("Dialog", Font.PLAIN, 12));
		tfVorname.setColumns(10);
		tfVorname.setBounds(180, 165, 140, 20);
		contentPane.add(tfVorname);
		
		JLabel lblStrasse = new JLabel("Strasse:");
		lblStrasse.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblStrasse.setBounds(10, 212, 46, 14);
		contentPane.add(lblStrasse);
		
		JComboBox comboAnrede = new JComboBox();
		comboAnrede.setFont(new Font("Dialog", Font.PLAIN, 12));
		comboAnrede.setModel(new DefaultComboBoxModel(new String[] {"Auswahl...", "Herr", "Frau"}));
		comboAnrede.setBounds(10, 109, 140, 20);
		contentPane.add(comboAnrede);
		
		JLabel lblAnrede = new JLabel("Anrede:");
		lblAnrede.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblAnrede.setBounds(10, 90, 70, 14);
		contentPane.add(lblAnrede);
		
		tfStrasse = new JTextField();
		tfStrasse.setFont(new Font("Dialog", Font.PLAIN, 12));
		tfStrasse.setColumns(10);
		tfStrasse.setBounds(10, 230, 140, 20);
		contentPane.add(tfStrasse);
		
		JLabel lblNr = new JLabel("Nr.:");
		lblNr.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblNr.setBounds(180, 213, 46, 14);
		contentPane.add(lblNr);
		
		tfNr = new JTextField();
		tfNr.setFont(new Font("Dialog", Font.PLAIN, 12));
		tfNr.setColumns(10);
		tfNr.setBounds(180, 231, 31, 20);
		contentPane.add(tfNr);
		
		JLabel lblOrt = new JLabel("Ort:");
		lblOrt.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblOrt.setBounds(10, 274, 46, 14);
		contentPane.add(lblOrt);
		
		tfOrt = new JTextField();
		tfOrt.setFont(new Font("Dialog", Font.PLAIN, 12));
		tfOrt.setColumns(10);
		tfOrt.setBounds(10, 293, 140, 20);
		contentPane.add(tfOrt);
		
		JLabel lblPlz = new JLabel("PLZ:");
		lblPlz.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblPlz.setBounds(180, 275, 46, 14);
		contentPane.add(lblPlz);
		
		tfPlz = new JTextField();
		tfPlz.setFont(new Font("Dialog", Font.PLAIN, 12));
		tfPlz.setColumns(10);
		tfPlz.setBounds(180, 294, 70, 20);
		contentPane.add(tfPlz);
		
		JLabel lblKlasse = new JLabel("Klasse:");
		lblKlasse.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblKlasse.setBounds(180, 90, 46, 14);
		contentPane.add(lblKlasse);
		
		JLabel lblGehaltsklasse = new JLabel("Gehaltsklasse:");
		lblGehaltsklasse.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblGehaltsklasse.setBounds(10, 343, 100, 14);
		contentPane.add(lblGehaltsklasse);
		
		JComboBox comboGehaltsklasse = new JComboBox();
		comboGehaltsklasse.setModel(new DefaultComboBoxModel(new String[] {"Auswahl...", "A12", "A13"}));
		comboGehaltsklasse.setFont(new Font("Dialog", Font.PLAIN, 12));
		comboGehaltsklasse.setBounds(10, 361, 100, 20);
		contentPane.add(comboGehaltsklasse);
		
		JLabel lblStunden = new JLabel("Stunden:");
		lblStunden.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblStunden.setBounds(180, 344, 63, 14);
		contentPane.add(lblStunden);
		
		tfStunden = new JTextField();
		tfStunden.setFont(new Font("Dialog", Font.PLAIN, 12));
		tfStunden.setColumns(10);
		tfStunden.setBounds(180, 362, 70, 20);
		contentPane.add(tfStunden);
		
		JLabel lblBesoldung = new JLabel("Besoldung:");
		lblBesoldung.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblBesoldung.setBounds(10, 410, 100, 14);
		contentPane.add(lblBesoldung);
		
		JLabel lblErgebnis = new JLabel("Ergebnis");
		lblErgebnis.setBounds(180, 411, 70, 14);
		contentPane.add(lblErgebnis);
		
		JButton btnBerechnung = new JButton("Gehalt");
		btnBerechnung.setBounds(285, 361, 89, 23);
		contentPane.add(btnBerechnung);
		
		JComboBox comboKlasse = new JComboBox();
		comboKlasse.setModel(new DefaultComboBoxModel(new String[] {"Auswahl...", "WG 12-1", "WG 12-2", "WG 13-1", "WG 13-2"}));
		comboKlasse.setBounds(180, 110, 100, 20);
		contentPane.add(comboKlasse);
		
		JLabel lblGeburtsdatum = new JLabel("Geburtsdatum(DD.MM.YYYY):");
		lblGeburtsdatum.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblGeburtsdatum.setBounds(10, 453, 166, 14);
		contentPane.add(lblGeburtsdatum);
		
		JComboBox comboTag = new JComboBox();
		comboTag.setModel(new DefaultComboBoxModel(new String[] {"Tag", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
		comboTag.setBounds(10, 478, 89, 20);
		contentPane.add(comboTag);
		
		JRadioButton rdbtnDrTitel = new JRadioButton("Dr. - Titel");
		rdbtnDrTitel.setBackground(Color.WHITE);
		rdbtnDrTitel.setBounds(297, 109, 77, 23);
		contentPane.add(rdbtnDrTitel);
		
		JComboBox comboMonat = new JComboBox();
		comboMonat.setModel(new DefaultComboBoxModel(new String[] {"Monat", "Januar", "Februar", "M\u00E4rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"}));
		comboMonat.setBounds(139, 478, 99, 20);
		contentPane.add(comboMonat);
		
		JComboBox comboJahr = new JComboBox();
		comboJahr.setModel(new DefaultComboBoxModel(new String[] {"Jahr", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020"}));
		comboJahr.setBounds(275, 478, 99, 20);
		contentPane.add(comboJahr);
		
		
		JButton btnAnlegen = new JButton("Anlegen");
		btnAnlegen.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				anlegen();
			
			}
		});
		btnAnlegen.setBounds(10, 564, 89, 23);
		contentPane.add(btnAnlegen);
		
		JButton btnLoeschen = new JButton("L\u00F6schen");
		btnLoeschen.setBounds(144, 564, 89, 23);
		contentPane.add(btnLoeschen);
		

		
		JButton btnSchliessen = new JButton("Schlie\u00DFen");
		btnSchliessen.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
					if(JOptionPane.showConfirmDialog(null, "Sind sie sich sicher das Fenster zu schließen?", "Sicherheitsfrage", JOptionPane.YES_NO_OPTION)== JOptionPane.YES_NO_OPTION)
					{
						System.exit(0);
					}
				}
			});			
		btnSchliessen.setBounds(275, 564, 99, 23);
		contentPane.add(btnSchliessen);
		

		
		tfAlter = new JTextField();
		tfAlter.setBounds(10, 520, 86, 20);
		contentPane.add(tfAlter);
		tfAlter.setColumns(10);
	}
	
	public void anlegen()
	{
// Error line
		Lehrer l1 = new Lehrer(comboAnrede.getSelectedItem(), tfVorname.getText(), tfName.getText(), Double.parseDouble(tfAlter.getText()), comboKlasse.getSelectedItem(), tfOrt.getText(), tfStrasse.getText(), tfPlz.getText(), comboTag.getSelectedItem(), comboJahr.getSelectedItem(), comboMonat.getSelectedItem(), comboGehaltsklasse.getSelectedItem(), Double.parseDouble(tfStunden.getText()));
	
	}
}



Error Text:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at package_GUI.LehrerFenster_GUI.anlegen(LehrerFenster_GUI.java:297)
at package_GUI.LehrerFenster_GUI$2.mouseClicked(LehrerFenster_GUI.java:260)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

  • Attachment: .project
    (Size: 0.38KB, Downloaded 154 times)
Re: Use of a parameterized constructor in GUI [message #1818979 is a reply to message #1818977] Sat, 04 January 2020 02:37 Go to previous message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 4501
Registered: July 2009
Senior Member

This isn't a general programming forum, and would be better asked somewhere like Java Ranch or Stack Overflow, but what you have is a generic NullPointerException: you are calling a method on a variable whose value is null. Put your detective hat on, Debug your application (Debug menu rather than Run menu to launch it) and see what's what.

https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php


_
Nitin Dahyabhai
Eclipse Web Tools Platform
Previous Topic:How to implement item template like MS VS by plugin in Eclipse
Next Topic:CrySL document generation
Goto Forum:
  


Current Time: Sat Nov 09 00:57:07 GMT 2024

Powered by FUDForum. Page generated in 0.08714 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top