I have no idea whats wrong here, i am fairly new to java..

heres my code....

private JPanel pnlLogin, pnlFinalLogin,pnlUser, pnlPass = new JPanel(new BorderLayout());

                btnLogin = new JButton("Login");
		lblUser = new JLabel("Username:");
		lblPass = new JLabel("Password:");

		pnlUser.add(lblUser, BorderLayout.WEST);
		//pnlUser.add(txtUser);
		pnlPass.add(lblPass, BorderLayout.WEST);
		//pnlPass.add(txtPass);

			
		pnlLogin.add(pnlUser, BorderLayout.WEST);
		pnlLogin.add(pnlPass, BorderLayout.CENTER);
		pnlLogin.add(btnLogin, BorderLayout.EAST);
				
		
		mainFrame.setSize(800, 400);
		mainFrame.getContentPane().setLayout(new BorderLayout());
		
		
		mainFrame.getContentPane().add(pnlLogin, BorderLayout.NORTH);

error

Exception in thread "main" java.lang.NullPointerException
at package1.GUI.show(GUI.java:25)
at package1.Program.main(Program.java:20)


line 25 is

pnlUser.add(lblUser, BorderLayout.WEST);

what am i doing wrong?

note: only added relevant code... reason being why line 25 is different

Thanx
Phoenix911

Recommended Answers

All 2 Replies

You have to declare each variable separately. Putting = new JPanel() at the end only declares the last variable, not all of them. Therefore your pnlUser variable has never been initialized with a "new" statement and is giving a NullPointerException.

wow thanx...

im a c# programmer... and i normally declare like that... thanx worked...

i feel retarded now xD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.