Sign in to follow this  
natebuckley

[java] Why does JFrame go Null.

Recommended Posts

Hello, sorry about all the questions - I have checked google but have unfortunetly been unable to find a solution. I have the following code EmployeeManager class
public class EmployeeManager{
    public static void main(String[] args){
    	TheManager manager = new TheManager();
    }
}

TheManager class
public class TheManager{
	
	protected MainFrame app;
	private LoginPanel loginScreen;
	private MainScreenPanel mainScreen;
	
	public TheManager(){
		MainFrame app = new MainFrame("Employee Manager");
		loginScreen = new LoginPanel();
		mainScreen = new MainScreenPanel();
		app.add(loginScreen);
		app.setVisible(true);
		run();
	}
	
	public void run(){
		//while(true){
		if (app==null){
			System.out.println("App is null");
		}
		else{
			System.out.println("App isn't null");
		}
		//}
		//app.add(mainScreen);
		//app.remove(loginScreen);
		//app.validate();
	}
}


Was having a problem where it kept crashing saying NullPointerException on the now commented out app.add(mainScreen). Do a test to see if the app was null and it was. Basically I want TheManager to eventually check the app for different states so if app.isLoggedIn() then display the mainScreen (which is a JPanel). P.S MainFrame extends JFrame Thanks once again for any help thrown my way, you don't need to give me a complete fix, to be hoenst I think I'm going about this solution in the wrong way and its illegal to use JFrames the way I am doing. Thanks! [Edited by - natebuckley on September 18, 2008 5:00:37 AM]

Share this post


Link to post
Share on other sites
Hmmm

If I pass app to the method run then it works but I don't understand why it didn't before because MainFrame app's scope was (I thought) global within that object.


public class TheManager{

protected MainFrame app;
private LoginPanel loginScreen;
private MainScreenPanel mainScreen;

public TheManager(){
MainFrame app = new MainFrame("Employee Manager");
loginScreen = new LoginPanel();
mainScreen = new MainScreenPanel();
app.setContentPane(loginScreen);
app.setVisible(true);
run(app);
}

public void run(MainFrame app){
while (true){
if (app==null){
System.out.println("App is null");
}
else{
System.out.println("App isn't null");
}
}

}
}

Share this post


Link to post
Share on other sites
The local Mainframe app inside the initializer hides the class' Mainframe app member variable!

So, instead of

public TheManager(){
MainFrame app = new MainFrame("Employee Manager");
loginScreen = new LoginPanel();
mainScreen = new MainScreenPanel();
app.add(loginScreen);
app.setVisible(true);
run();
}


use

public TheManager(){
app = new MainFrame("Employee Manager");
loginScreen = new LoginPanel();
mainScreen = new MainScreenPanel();
app.add(loginScreen);
app.setVisible(true);
run();
}



Tipp: Ever if you mean a member variable, write this before it:

public TheManager(){
this.app = new MainFrame("Employee Manager");
this.loginScreen = new LoginPanel();
this.mainScreen = new MainScreenPanel();
this.app.add(this.loginScreen);
this.app.setVisible(true);
run();
}


So the compiler is enabled to check your intentions better.

[Edited by - haegarr on September 18, 2008 10:04:31 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this