Jump to content
  • Advertisement
Sign in to follow this  
natebuckley

[java] Why does JFrame go Null.

This topic is 3620 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!